2012-01-13 71 views
2

当我尝试自动装载我的DAO对象时,出现空指针异常。让我告诉你的代码(我使用maven +休眠+弹簧+ JPA):JPA不是自动装配

@Repository 
public class DAOFriendImp implements DAOFriend{ 

    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence"); 
    private EntityManager entityManager = emf.createEntityManager(); 

    @PersistenceContext(unitName = "persistence") 
    public void setEntityManager(EntityManager entityManager) { 
     this.entityManager = entityManager; 
    } 

    @Override 
    public boolean save(Person person, Person friend) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 

    @Override 
    public List<Person> getAllFriendOfPersonWithId(int id) { 
     Person person = entityManager.find(Person.class, id); 
     return person.getFriends(); 
    } 

    @Override 
    public boolean delete(Person person, Person friend) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 

} 

正如你可以看到我手动初始化我的EntityManager。那是因为如果我使用persistenceContext,我也会得到一个空指针异常。也许是因为DAOriendImp没有正确获取自动装配?我不知道。

现在让我告诉你,我得到空指针:

public class FriendListLogic { 

@Autowired 
DAOFriendImp dao; 

public List<Person> getEntrys(int userId) { 
    List<Person> result = dao.getAllFriendOfPersonWithId(userId); //here 
    return result; 
} 
} 

我不知道为什么它没有得到注入!在配置XML中,一切看起来都不错。我将添加文件(POM,持久性的ApplicationContext和servlet.xml中)

persistence.xml中的其余部分:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
    version="1.0"> 

<persistence-unit name="persistence" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <class>com.divux.onex.model.Person</class> 
    <class>com.divux.onex.model.CircleOfConnection</class> 
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 
     <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
     <property name="javax.persistence.jdbc.url"  value="jdbc:mysql://localhost:3306/onex" /> 
     <property name="javax.persistence.jdbc.user"  value="root" /> 
     <property name="javax.persistence.jdbc.password" value="bterra" /> 
    </properties> 
</persistence-unit> 
</persistence> 

注:我这里有我的JDBC用户密码驱动程序和URL,因为我有问题在ApplicationContext中的数据源bean ...它只是没有,如果我有这个信息存在工作(不知道为什么)

的applicationContext.xml

<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:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

<context:annotation-config /> 
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
</bean> 

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="persistenceUnitName" value="persistence" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="database" value="MYSQL" /> 
      <property name="showSql" value="false" /> 
      <property name="generateDdl" value="false" /> 
      <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /> 
     </bean> 
    </property> 
    <property name="loadTimeWeaver"> 
     <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"></bean> 
    </property> 
</bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"></bean> 

<tx:annotation-driven transaction-manager="transactionManager" /> 


</beans> 

的servlet .XML

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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.divux.onex.controller"></context:component-scan> 
<context:component-scan base-package="com.divux.onex.daoImp"></context:component-scan> 

<context:property-placeholder location="classpath*:resources/properties/*.properties"/> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 

<mvc:annotation-driven/> 
<mvc:view-controller path="/" view-name="index"/> 
<mvc:resources mapping="/resources/**" location="/resources/"/> 

    <import resource="applicationContext.xml" /> 

</beans> 

最后pom.xml中(只是其中的一部分)

<properties> 
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version> 
    <aspectj.version>1.6.11</aspectj.version> 
    <org.slf4j-version>1.5.8</org.slf4j-version> 
    <hibernate.version>3.6.7.Final</hibernate.version> 
    <hibernate-search.version>4.0.0.Final</hibernate-search.version> 
</properties> 
<!-- JPA with Hibernate Persistence -->    
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.16</version> 
    </dependency> 
    <!-- Persistance --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>${hibernate.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>${hibernate.version}</version> 
     <exclusions> 
      <exclusion> 
       <groupId>cglib</groupId> 
       <artifactId>cglib</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>dom4j</groupId> 
       <artifactId>dom4j</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-ehcache</artifactId> 
     <version>${hibernate.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-search</artifactId> 
     <version>${hibernate-search.version}</version> 
    </dependency> 
<!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jmx</artifactId> 
<version>${hibernate-jmx.version}</version> </dependency> --> 
    <dependency> 
     <groupId>org.hibernate.javax.persistence</groupId> 
     <artifactId>hibernate-jpa-2.0-api</artifactId> 
     <version>1.0.0.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-jpamodelgen</artifactId> 
     <version>1.1.1.Final</version> 
     <scope>provided</scope> 
<!-- only need at compile time --> 
    </dependency> 
    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib-nodep</artifactId> 
     <version>2.2</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.transaction</groupId> 
     <artifactId>jta</artifactId> 
     <version>1.1</version> 
    </dependency> 


<!-- validation --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-validator</artifactId> 
     <version>4.1.0.Final</version> 
     <exclusions> 
      <exclusion> 
       <groupId>javax.xml.bind</groupId> 
       <artifactId>jaxb-api</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>com.sun.xml.bind</groupId> 
       <artifactId>jaxb-impl</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
    <dependency> 
     <groupId>javax.validation</groupId> 
     <artifactId>validation-api</artifactId> 
     <version>1.0.0.GA</version> 
    </dependency> 

<!-- AspectJ --> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
     <version>${aspectj.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
     <version>${aspectj.version}</version> 
    </dependency> 

</dependencies> 

让我知道如果你需要了解更多信息!我不会添加错误输出,因为我认为这是不必要的。如果你需要它,让我知道。

+0

PLease可以发布显示NPE的堆栈跟踪。 – 2012-01-13 16:51:22

+0

我不知道如何获得该信息@亚历克斯!你能告诉我该怎么做吗? – BrunoX 2012-01-13 18:28:49

+0

我认为'PersistenceAnnotationBeanPostProcessor'和'AutowiredAnnotationBeanPostProcessor'不应该在上下文中明确提及:它们应该被XML指令自动添加。我可能是错的,但添加它们是不正常的。 – 2012-01-13 22:17:05

回答

4

啊,你需要使用接口DAOFriend自动装配:因为Spring使用Java代理mechanizm到你的仓库豆包装到事务感知层

@Autowired 
DAOFriend dao; 

这是。通过这种方法,只有其他bean可以看到接口。

另外@Autowired具有“必需”的默认行为。这意味着您的FriendListLogic实例不受Spring管理:您需要使用@Component注释此类(并确保它在组件扫描路径中),或通过String上下文显式创建它。

+0

DONE,但仍然不工作!我将这两个选项结合在一起...将autowire更改为接口并创建指向接口imp的bean,但它仍然为空...它罕见... – BrunoX 2012-01-13 19:38:47

+0

我已经更新了答案:不清楚你如何创建'FriendListLogic'。它应该是一个负载时织入的实体吗? – 2012-01-13 22:24:57

+0

就是这样!我添加组件到我的FriendListLogic,现在它的工作(现在嘿嘿)我有其他问题与proxys ...需要添加代理目标类=“真”到我的交易管理器,但它解决了所有其他问题我已经像“笔记”一样列在上面了!非常感谢@dma_k – BrunoX 2012-01-16 16:39:01

0

尝试将FriendImpl作为bean添加到您的app-context.xml中。您将需要用完全限定的类名替换xxx.yyy.zzz.FriendImpl。

​​

+0

它力气工作!仍然有同样的问题!我不认为这是一个解决方案,因为如果即时通讯使用组件扫描,我将它们作为@Repositorys进行了排列......我想我不需要直接在我的应用上下文中声明这个bean!或者至少那是我相信的! – BrunoX 2012-01-13 18:26:23