直接上代码
// 加 @Component 注解 先把自己创建的类 MyClass 在Spring 中 注册成 组件
@Component
public class MyClass {
// 注入 需要 调用的 service 类
@Autowired
protected MyService myService;
// 创建 自己类 的 私有 属性
private static MyClass myClass ;
// 新增init 方法 @PostConstruct 注解 作用 在启动项目时 执行该方法
@PostConstruct
public void init() {
// 把 类本身 赋予变量 myClass
myClass = this;
// 把 初始化加载的 nettyMeterDataService 属性 赋值给本类
myClass.MyService = this.MyService;
}
// 测试方法
public static void main(String[] args) {
// 调用方式 本类名.service类.service类中具体方法
myClass.MyService.insertData(null);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30