JPA不是自动装配 [英] JPA is not Autowiring

查看:114
本文介绍了JPA不是自动装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试自动装载我的DAO对象时,出现空指针异常。让我向你展示代码(我使用maven + hibernate + spring + jpa):

  @Repository 
公共类DAOriendImp实现DAOFriend {

私有EntityManagerFactory emf = Persistence.createEntityManagerFactory(持久性);
private EntityManager entityManager = emf.createEntityManager();
$ b $ @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();

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

}

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



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

  public class FriendListLogic {

@Autowired
DAOFriendImp dao;

公共列表< Person> getEntrys(int userId){
List< Person> result = dao.getAllFriendOfPersonWithId(userId); //这里
返回结果;
}
}

我不知道为什么它没有被注入!在配置XML中,一切看起来都不错。我将添加剩余的文件(pom,persistence,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 =persistencetransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< class> com.divux.onex.model.Person< / class>
< class> com.divux.onex.model.CircleOfConnection< / class>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =javax.persistence.jdbc.drivervalue =com.mysql.jdbc.Driver/>
< property name =javax.persistence.jdbc.urlvalue =jdbc:mysql:// localhost:3306 / onex/>
< property name =javax.persistence.jdbc.uservalue =root/>
< property name =javax.persistence.jdbc.passwordvalue =bterra/>
< / properties>
< / persistence-unit>
< /余辉>

注意:我有我的jdbc用户密码驱动程序和url,因为我在dataSource bean applicationContext ...它只是没有工作,如果我有这方面的信息(不知道为什么)
$ b $ 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 =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< / bean>

< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =persistenceUnitNamevalue =persistence/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =databasevalue =MYSQL/>
< property name =showSqlvalue =false/>
< property name =generateDdlvalue =false/>
< property name =databasePlatformvalue =org.hibernate.dialect.MySQLDialect/>
< / bean>
< / property>
< property name =loadTimeWeaver>
< bean class =org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver>< / bean>
< / property>
< / bean>

< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>

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

< tx:注解驱动的事务管理器=transactionManager/>


< / beans>

servlet.xml

 <?xml version =1.0encoding =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 =prefixvalue =/ WEB-INF / jsp //>
< property name =suffixvalue =。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(只是其中的一部分)

 <属性> 
< 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>
<! - 具有Hibernate持久性的JPA - >
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.16< / version>
< /依赖关系>
<! - Persistance - >
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> $ {hibernate.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> $ {hibernate.version}< / version>
<排除项>
<排除>
< groupId> cglib< / groupId>
< artifactId> cglib< / artifactId>
< /排除>
<排除>
< groupId> dom4j< / groupId>
< artifactId> dom4j< / artifactId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-ehcache< / artifactId>
< version> $ {hibernate.version}< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-search< / artifactId>
< version> $ {hibernate-search.version}< / version>
< /依赖关系>
<! - <依赖关系> <&的groupId GT; org.hibernate作为< /&的groupId GT; < artifactId的>冬眠-JMX< / artifactId的>
< version> $ {hibernate-jmx.version}< / version> < /依赖性> - >
< dependency>
< groupId> org.hibernate.javax.persistence< / groupId>
< artifactId> hibernate-jpa-2.0-api< / artifactId>
< version> 1.0.0.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-jpamodelgen< / artifactId>
< version> 1.1.1.Final< / version>
< scope>提供< / scope>
<! - - 仅在编译时需要 - >
< /依赖关系>
< dependency>
< groupId> cglib< / groupId>
< artifactId> cglib-nodep< / artifactId>
< version> 2.2< / version>
< /依赖关系>
< dependency>
< groupId> javax.transaction< / groupId>
< artifactId> jta< / artifactId>
< version> 1.1< / version>
< /依赖关系>


<! - 验证 - >
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-validator< / artifactId>
< version> 4.1.0.Final< / version>
<排除项>
<排除>
< groupId> javax.xml.bind< / groupId>
< artifactId> jaxb-api< / artifactId>
< /排除>
<排除>
< groupId> com.sun.xml.bind< / groupId>
< artifactId> jaxb-impl< / artifactId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> javax.validation< / groupId>
< artifactId> validation-api< / artifactId>
< version> 1.0.0.GA< / version>
< /依赖关系>

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

< /依赖关系>

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

  @Autowired 
DAOFriend dao;

这是因为Spring使用Java代理mechanizm将存储库bean包装到事务感知层中。使用这种方法,只有其他bean可以看到接口。



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


I'm getting a null pointer exception when I try to autowire my DAO object. Let me show you the code (I'm using maven + hibernate + spring + 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.");
    }

}

As you can see I'm initializing my entityManager manually. That's because if I use the persistenceContext I get a null pointer exception too. Perhaps because the DAOFriendImp is not getting autowires properly? I don't really know.

Now let me show you where I get the null pointer:

public class FriendListLogic {

@Autowired
DAOFriendImp dao;

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

I don't know why it's not getting injected! Everything seems to be alright in the configuration XML. I will add the rest of the files (pom, persistence, applicationcontext and 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>

Note: I have my jdbc user password driver and url here because I had problems in the dataSource bean in the applicationContext ... it just didn't work if I had this info there (don't know why)

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>

and finally the pom.xml (just a part of it)

<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>

Let me know if you need more information! I will not add the error output because I think it's unnecessary. If you need it let me know.

解决方案

Ah, you need to autowire using the interface DAOFriend:

@Autowired
DAOFriend dao;

This is because Spring uses Java proxy mechanizm to wrap your repository bean into transaction-aware layer. With this approach only interfaces can be visible for other beans.

Also @Autowired has "required" default behaviour. That means your FriendListLogic instances are not managed by Spring: you need to annotate this class with @Component (and make sure it is in component scan path) or create it explicitly via String context.

这篇关于JPA不是自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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