spring boot service注入失敗

今天遇到了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;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章