@Component //申明为spring组件
public class TestUtils {
@Autowired
private TestService testService; //添加所需service的私有成员
//关键点一 静态初使化一个工具类,这样是为了在spring初使化之前
private static TestUtils testUtils ;
public void setTestService(TestService testService) {
this.testService = testService;
}
//关键点二 通过@postconstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
@postconstruct
public void init() {
testUtils = this;
testUtils.testService = this.testService; // 初使化时将已静态化的testService实例化
}
这样下面的代码中就可以通过 testUtils.testService 来调用service处理。
鸣谢:all_forone
原文地址:https://www.jb51.cc/javaschema/283469.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。