问题描述
当我尝试自动装载我的DAO对象时,出现空指针异常。让我向你展示代码(我使用maven + hibernate + spring + jpa):
/ 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不是自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!