Spring Autowire空指针异常 [英] Spring autowire null pointer exception

查看:421
本文介绍了Spring Autowire空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

xml配置-

<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());
   }
}

数据源DS.getConnection获取空指针异常.

datasource DS.getConnection getting null pointer exception.

自动装配不起作用.

有什么解决办法吗?

推荐答案

ABC不受spring管理.

ABC is not managed by spring.

要使@Autowired批注起作用,您必须使用以下任一方法批注该类:

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

@Component
@Service
@Controller
@Repository

或在XML配置中定义它

or define it in the XML configuration

这篇关于Spring Autowire空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆