首页 文章

Spring中Autowired对象的空指针异常

提问于
浏览
0

我正在为SOAP Web服务构建一个新项目 . 之前我使用JDBC层来打开和关闭连接 . 现在,我正在使用JDBC模板将其转换为Spring . 我已经配置了所有图层并注释了组件 . 当我尝试在我的服务impl类中使用dao bean时,它会抛出空指针异常

@Service
@WebService
public interface Transaction { // Web methods here for SOAP Web service
}

Impl类

@Component
@WebService
public class TransactionImpl implements Transaction{        

    @Autowired
    BBDao dao;  --> This is coming as null when I use it in the method

}

BBDao界面如下

public interface BBDao { /* Methods in it */ }

实现BBDao接口的实现类是

public class BBDaoImpl extends JdbcDaoSupport implements BBDao {    

@Autowired
ServletContext ctx; 

@Autowired
DataSource dataSource;

// Methods overriding here 

}

Servlet在web.xml中定义

<servlet>
    <servlet-name>spring-web</servlet-name>
    <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring-web</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

最后是spring-web-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="com.bb.controller,com.bb.dao,com.bb.service" />
<context:property-placeholder location="classpath:datasource-cfg.properties" />


<bean id="bbDAO" class="net.bb.dao.BBDaoImpl">
    <property name="dataSource" ref="dataSource" />
</bean> 

<!-- AS400 Data source Bean -->
<bean id="dataSource"
     class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.ibm.as400.access.AS400JDBCDriver" />
    <property name="url" value="${as400.url}" />
    <property name="username" value="${as400.username}" />
    <property name="password" value="${as400.password}" />
</bean>

<mvc:annotation-driven />

</beans>

BBDao bean对象将为null .

我的配置有什么错误吗?任何建议将不胜感激 .

P.S:我也关注过其他帖子,因为大多数帖子都只讨论组件扫描,而且包是正确的

1 回答

  • 1

    由于我们无法为接口创建实例,因此我们无法自动装配没有实现的接口 . 但你可以尝试下面的那些,看看它是否有效 .

    几乎没有什么可以做的 .

    添加到spring-webservlet.xml

    并且还请参考this

    用户注释,如@ConditionalOnMissingBean(BBDao.class)

相关问题