Spring autowire null pointer exception [duplicate]

丶灬走出姿态 提交于 2019-12-13 10:01:26

问题


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:


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



来源:https://stackoverflow.com/questions/44112572/spring-autowire-null-pointer-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!