Autowire Jdbc template
18,980
Solution 1
Instead of this code:
@Autowired
template JdbcTemplate;
You need:
@Autowired
JdbcTemplate template;
Solution 2
- The bean you try to inject in is not in the spring context;
- No setter for the JdbcTemplate
- You try to use the template in the constructor before the template is injeted
Solution 3
One reason for this error is to mix autowiring with manual creation of beans.
For instance, you have a service class that autowires a bean.
@Service
public class CarService {
@Autowired
public JdbcTemplate jdbcTemplate;
// service code
}
But later intead of
@Autowired
private CarService carService;
you do:
CarService carService = new CarService();
Author by
Amit
Updated on June 21, 2022Comments
-
Amit almost 2 years
I am trying to auto-wire JDBC template and I'm getting a null pointer exception (template is null). What could be the problem?
@Autowired template JdbcTemplate;
This is my application context xml:
<bean ..> <mvc:annotation-driven /> <context:component-scan base-package="igate.dto" /> <context:component-scan base-package="igate.dao" /> <context:component-scan base-package="igate.service" /> <context:component-scan base-package="igate.controller" /> <context:component-scan base-package="igate.logs" /> <context:component-scan base-package="igate.testcases" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp" /> </bean> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@172.21.17.5:1521:oraten" /> <property name="username" value="lab01trg21" /> <property name="password" value="lab01oracle" /> </bean> <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="ds"/> </bean> </beans>