Spring Autowired对象始终为Null

时间:2017-03-23 10:34:29

标签: java spring maven java-8 spring-data

package plantSiteMinder.user.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import plantSiteMinder.dao.UserDAO;
import plantSiteMinder.model.User;
import plantSiteMinder.user.UserAuthenticator;
@Service
@Transactional(readOnly = true)
public class UserAuthenticatorImpl implements UserAuthenticator {
   @Autowired
   private UserDAO userDAO;
   public List<User> findAllUsers() {
       return userDAO.findAllUsers();
   }
}

============================ Spring.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:p="http://www.springframework.org/schema/p"
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.xsd">
<!-- <tx:annotation-driven /> -->
<bean id="userDAO" class="plantSiteMinder.dao.impl.UserDAOImpl" />
<bean id="userAuth"    
class="plantSiteMinder.user.impl.UserAuthenticatorImpl"/>
<!--    <context:component-scan base-package="plantSiteMinder.dao.impl" />
<context:component-scan base-package="plantSiteMinder.user.impl" /> -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/poc_data" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="myPU" />
    <property name="dataSource" ref="dataSource" />
</bean>

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

=======================主类====================== =

package plantSiteMinder.plantSiteMinder;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import plantSiteMinder.model.User;
import plantSiteMinder.user.impl.UserAuthenticatorImpl;
public class App 
{
   public static void main( String[] args )
   {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
            "spring.xml");

       UserAuthenticatorImpl userManager = (UserAuthenticatorImpl)   
       ctx.getBean("userAuth");
       List<User> list = userManager.findAllUsers();
       System.out.println("User count: " + list.size());
   }

}

UserDAO始终为null,如何解决此问题无法找到问题。

=========================用户DAO Impl =================== ======

package plantSiteMinder.dao.impl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.springframework.stereotype.Repository;
import plantSiteMinder.dao.UserDAO;
import plantSiteMinder.model.User;
@Repository
public class UserDAOImpl implements UserDAO{
  @PersistenceContext
  private EntityManager entityManager;
  public List<User> findAllUsers() {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> cq = builder.createQuery(User.class);
    Root<User> root = cq.from(User.class);
    cq.select(root);
    return entityManager.createQuery(cq).getResultList();
  }
}

=======================的pom.xml ===================== ===

<project xmlns="http://maven.apache.org/POM/4.0.0"     
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0    
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>plantSiteMinder</groupId>
<artifactId>plantSiteMinder</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>plantSiteMinder</name>
<url>http://maven.apache.org</url>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.0.3.RELEASE</spring.version>
    <hibernate.version>4.3.5.Final</hibernate.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.30</version>
    </dependency>
</dependencies>

  

我在这个项目中使用Java 8

1 个答案:

答案 0 :(得分:0)

Xml配置始终会覆盖注释配置。

其中一个解决方案是删除

<bean id="userDAO" class="plantSiteMinder.dao.impl.UserDAOImpl" />
<bean id="userAuth"    
class="plantSiteMinder.user.impl.UserAuthenticatorImpl"/>

从xml配置并取消注释这些行:

<context:annotation-config />
<context:component-scan base-package="plantSiteMinder.user.impl" />

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

你必须记住,你必须在你的DAo中@Autowire SessionFactory。