【问题标题】:Null pointer exception for Autowired object in SpringSpring中Autowired对象的空指针异常
【发布时间】:2017-11-19 10:36:04
【问题描述】:

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

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

实现类

@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 

}

在 web.xml 中定义的 Servlet

<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 对象为空。

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

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

【问题讨论】:

  • BBDAoImpl 是spring bean吗?我的意思是你有 @Component@Service 在这堂课上吗?
  • 不,Kiran 不存在。我现在添加了@Component 并进行了测试。还是一样
  • Kiran,我刚刚发现当我使用某个测试控制器对其进行测试时,dao 不为空。但是当我尝试使用 SOAPUI 工具时,它给出了 null。

标签: java spring spring-mvc jdbc autowired


【解决方案1】:

我们无法在没有实现的情况下自动装配接口,因为我们无法为接口创建实例。但是你可以试试下面的方法,看看它是否有效。

你能做的事情很少。

添加到你的 spring-webservlet.xml

也请参考this

@ConditionalOnMissingBean(BBDAo.class) 等用户注解

【讨论】:

  • 当我从测试控制器调用 impl 方法之一时,dao 对象具有值。现在,当我将 SOAP UI 中的方法调用到 Impl 类中的方法之一时,它会给出 null
猜你喜欢
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 2021-04-22
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多