今天遇到了service注入失败,遇到空指针异常
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
ConnDB类使用service注入,jdbc连接数据库,但在主程序中将其实例化后无法调用函数,报空指针异常,检查后发现实例化的需要添加Autowired注解
@Service
public class ConnDB {
@Autowired
JdbcTemplate jdbcTemplate;
public void insert(String serverAd, String chaincodeId, String invokeId, String invokeTime){
String sql = "insert into tran_data values(\'"+serverAd+"\',\'"+chaincodeId+"\',\'"+invokeId+"\',\'"+invokeTime+"\',"+0+")";
System.out.println(sql);
jdbcTemplate.execute(sql);
}
}
Controller:
@Autowired
private ConnDB connDB;