Autowire Jdbc template

Amit picture Amit · Sep 27, 2013 · Viewed 17k times · Source

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>

Answer

MariemJab picture MariemJab · Apr 17, 2014

Instead of this code:

@Autowired
template JdbcTemplate;

You need:

@Autowired
JdbcTemplate template;