@Component //使用component标签在启动时把类加载到spring容器内
public class TestUtil {
@Autowired
private TestService testService;
private static TestThrough testThrough; //转接对象
//被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
@PostConstruct
public void initAutowired() {
testThrough = this; //将转接对象赋值为此类
testThrough. testService = this. testService; //将转接对象的@Autowired属性赋值为此类的@Autowired
}
//测试
public static void test() {
testThrough.testService.insert(); //调用时使用转接对象内的@Autowired属性
}
}