使用@Component,@PostConstruct和@Autowired注解
举例如下
@Component
public class TestUtil {
@Autowired
private IOrderFormService orderFormService;
public static TestUtil testUtil;
@PostConstruct
public void init() {
testUtil= this;
}
public static void test(){
//调用service接口
test.orderFormService.selectList();
}
}
如果直接在普通类使用注解@Autowired获取服务,会报空指针异常。用上述方式既可成功调用到服务接口的方法。使用@Component注解的时候需要注意包扫描<context:component-scan base-package = “”/>