Search code examples
javaspringdatasourcejavabeansautowired

Spring autowire null pointer exception


xml configuration -

<bean id="DS" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >

        <property name="driverClassName" value="${DB.DRIVERCLASS}" />
        <property name="url" value="${TABLEMAINT.URL}" />       
        <property name="username" value="${TABLEMAINT.USER}" />
        <property name="password" value="${TABLEMAINT.PASSWORD}" />
    </bean>

@Component
class AbcDAO{
 @Autowired
private DriverManagerDataSource DS;
   public void getConnection(){
      System.out.println("DS - "+DS..getConnection());
   }
}

datasource DS.getConnection getting null pointer exception.

Autowiring not working.

Is there any solution?


Solution

  • ABC is not managed by spring.

    For @Autowired annotation to work you have to annotate that class with either of the following:

    @Component
    @Service
    @Controller
    @Repository
    

    or define it in the XML configuration