记录优化程序过程中遇见的一个问题~
//问题代码
@Component
public class MethodTest{
@Autowired
private static JnMapper jnFeppMapper;//静态方法调用内部类必须定义为静态类
@Autowired
JnMsgMapper msgMapper;
public static void staticMethod(){
jnFeppMapper.selectById(null); //此时jnFeppMapper为null,报空指针
}
public void exampleMethod(){
msgMapper.selectById(null);
}
}
解决方案【@PostConstruct】:
使用Java EE 5中引入注解@PostConstruct,位于javax.annotation包下。
//解决问题
@Component
public class MethodTest{
@Autowired
private JnMapper jnFeppMapper;//静态方法调用内部类必须定义为静态类
@Autowired
JnMsgMapper msgMapper;
public static MethodTest methodTestUtils;
@PostConstruct
public void init(){
methodTestUtils = this;
methodTestUtils.jnFeppMapper = this.jnFeppMapper;
}
public static void staticMethod(){
methodTe