为什么Spring tc localhost服务器抛出nullPointerException而不是自动装配?

问题描述:

我正在努力寻找一个似乎很愚蠢但我无法理解的问题。我正在使用SpringSource提供的localhost服务器测试一个非常简单的servlet。 这是servlet的有意义的部分:为什么Spring tc localhost服务器抛出nullPointerException而不是自动装配?

@ContextConfiguration("configuration.xml") 
public class Refresh extends HttpServlet { 
private static final long serialVersionUID = 1L; 

@Autowired 
private IUserDao uDao; 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    User a = uDao.getUser(1); 
} 

} 

我敢肯定,道是正确的,因为从JUnit测试类调用一切正常工作。 奇怪的是我期望找到类似“FailedToLoadApplicationContext”异常的东西,但是错误只是一个NullPointerException,比如@Autowired和@ContextConfiguration不存在。这应该是我使用的服务器的问题吗?

这是我configuration.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Spring configuration for data access tier --> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

<context:component-scan base-package="com.firststepteam.dao"> 
    <context:include-filter type="annotation" 
     expression="org.springframework.stereotype.Repository"/> 
</context:component-scan> 

<context:component-scan base-package="com.firststepteam.services"> 
    <context:include-filter type="annotation" 
     expression="org.springframework.stereotype.Service"/> 
</context:component-scan> 

<!-- View resolver -> JSP --> 
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
    <property name="prefix" value="/WEB-INF/jsp/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 

<!-- PropertyPlaceholderConfigurer, serve a recuperare le impostazioni 
per il dataSource nel file firststep.conf --> 
<bean id="placeHolder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations" value="classpath:hostingData.conf" /> 
</bean> 

<!-- DataSource, sorgente del db, i value sono in firststep.conf --> 
<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="${jdbc.db.driverClassName}" /> 
    <property name="url" value="${jdbc.db.url}" /> 
    <property name="username" value="${jdbc.db.username}" /> 
    <property name="password" value="${jdbc.db.password}" /> 
</bean> 

<!-- JPA E HIBERNATE --> 
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
    <property name="database" value="MYSQL" /> 
    <property name="generateDdl" value="true" /> 
</bean> 

<!-- ENTITY MANAGER FACTORY --> 
<bean id="dbEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="persistenceUnitName" value="fs_db" /> 
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> 
</bean> 

<tx:annotation-driven /> 
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="dbEntityManagerFactory" /> 
</bean> 

<bean class = "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 

</beans> 

正如你所看到的,我使用JPA和Hibernate

+0

可能重复[为什么我的Spring @Autowired字段为空?](http://*.com/questions/19896870/why-is-my-spring-autowired-field-null) – chrylis

您应该添加一个setter为:

private IUserDao uDao; 
+0

一个setter?为什么?哪里? – Fabio

+0

你的uDao是一个私有变量,Spring如何在没有setter的情况下设置它? – Tristan

+1

我认为这不是问题...私人IUserDao的自动装配工作在测试类 – Fabio