Spring autowire null pointer exception [duplicate]

2019-09-29 11:08发布

This question already has an answer here:

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?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-09-29 11:45

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

查看更多
登录 后发表回答