简体   繁体   English

没有定义类型为[javax.persistence.EntityManagerFactory]的合格bean

[英]No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined

I using Spring + Hibernate + JPA and Tomcat 7 for REST service. 我使用Spring + Hibernate + JPA和Tomcat 7进行REST服务。 When I start the app I get the following: 启动应用程序时,我得到以下信息:

 org.apache.catalina.core.ApplicationContext log org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'carService': Error creating bean with name 'carServiceImpl': Unsatisfied dependency expressed through field 'carDao': Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carServiceImpl': Unsatisfied dependency expressed through field 'carDao': Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
   Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carServiceImpl': Unsatisfied dependency expressed through field 'carDao': Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
   Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined

Below i show my spring config class 下面我显示我的弹簧配置类

@Configuration
@Import(DispatcherServletConfig.class)
@ComponentScan(basePackages = "org.parkingTracker.controller, org.parkingTracker.service, org.parkingTracker.dao")
@ImportResource("classpath*:/dao/src/main/resources/spring/dao-context.xml")
public class RootConfig {
    public RootConfig() {}
}

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.parkingTracker.controller, org.parkingTracker.service")
public class DispatcherServletConfig {
    public DispatcherServletConfig() {}
}

I also have service and dao classes for access to data. 我还具有用于访问数据的service和dao类。 To inject EntityManager in my dao i am using @PersistenceContext annotation, for inject dao for service class i am using simple spring annotations. 为了在dao中注入EntityManager,我正在使用@PersistenceContext批注,对于服务类注入dao,我正在使用简单的spring批注。 Below you can see my spring xml config for DAO layout. 在下面,您可以看到DAO布局的spring xml配置。 One important comments, when i run test for dao classes all passing right, and i don't have any Exceptions, and i am obtaining valid data 一个重要的评论,当我运行对dao类的测试都通过时,并且没有任何异常,并且我正在获取有效数据

<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:jpa = "http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd ">
<context:component-scan base-package="org.parkingTracker.dao"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://localhost:5432/parking"/>
    <property name="username" value="postgres"/>
    <property name="password" value="a1f10g"/>
    <property name="initialSize" value="20"/>
    <property name="maxActive" value="100"/>
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false"/>
        </bean>
    </property>
    <property name="packagesToScan" value="org.parkingTracker.model"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL94Dialect</prop>
        </props>
    </property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<jpa:repositories  base-package="org.parkingTracker"
                   entity-manager-factory-ref="emf"
                   transaction-manager-ref="transactionManager"/></beans>

CarServiceImpl: CarServiceImpl:

 public class CarServiceImpl implements CarService {
@Autowired
private CarDao carDao;
@Override
public void saveCar(Car car) throws EntityAlreadyExistException {
    carDao.saveCar(car);
}
@Override
public Car getCarById(int id) {
    return carDao.getCarById(id);
}
@Override
public Car getCarByIdWithTimeSpend(int id) {
    return carDao.getCarByIdWithTimeSpend(id);
}
@Override
public Car getCarByNumber(String number) {
    return carDao.getCarByNumber(number);
}
@Override
public Car getCarByNumberWithTimeSpend(String number) {
    return carDao.getCarByNumberWithTimeSpend(number);
}

CarDao: CarDao:

@Transactional
@Repository("carDao")
public class CarDaoImp implements CarDao{
    private final String EXIST_SQL = "SELECT 1 FROM car WHERE car_num = :num";
    @PersistenceContext
    private EntityManager entityManager;
    @Transactional()
    public void saveCar(Car car) throws EntityAlreadyExistException {
        if(entityManager.createNativeQuery(EXIST_SQL).setParameter("num", car.getNumber()).getSingleResult()!=null){
            throw new  EntityAlreadyExistException();
        }else {
            entityManager.persist(car);
        }
    } 
    @Transactional(readOnly = true)
    public Car getCarById(int id) {
        return entityManager.find(Car.class, id);
    }
    @Transactional(readOnly = true)
    public Car getCarByIdWithTimeSpend(int id) {
        Car car = entityManager.find(Car.class, id);
        Hibernate.initialize(car.getTimeSet());
        return car;
    }
    @Override
    @Transactional(readOnly = true)
    public Car getCarByNumber(String number) throws NoResultException{
        CriteriaBuilder builder  = entityManager.getCriteriaBuilder();
        CriteriaQuery<Car> carCriteriaQuery = entityManager.getCriteriaBuilder().createQuery(Car.class);
        Root<Car> root = carCriteriaQuery.from(Car.class);
        carCriteriaQuery.select(root);
        carCriteriaQuery.where(builder.equal( root.get(Car_.number), number ));
        return entityManager.createQuery(carCriteriaQuery).getSingleResult();
    }
    @Override
    @Transactional(readOnly = true)
    public Car getCarByNumberWithTimeSpend(String number){
        CriteriaBuilder builder  = entityManager.getCriteriaBuilder();
        CriteriaQuery<Car> carCriteriaQuery = entityManager.getCriteriaBuilder().createQuery(Car.class);
        Root<Car> root = carCriteriaQuery.from(Car.class);
        carCriteriaQuery.select(root);
        carCriteriaQuery.where(builder.equal( root.get(Car_.number), number ));
        Car car = entityManager.createQuery(carCriteriaQuery).getSingleResult();
        Hibernate.initialize(car.getTimeSet());
        return car;
    }
}

web.xml : web.xml:

<web-app
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">    
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            org.parkingTracker.controller.config.RootConfig
        </param-value>
    </context-param>    
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                org.parkingTracker.controller.config.DispatcherServletConfig
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

It seems that IOC container is failing to inject the EntityManagerFactory 看来IOC容器无法注入EntityManagerFactory
to CarDaoImp . CarDaoImp I looked at your code and it seems that you defined all the necessary configurations, I think the problem lies down with your XML configuration file loading. 我查看了您的代码,似乎您定义了所有必需的配置,我认为问题在于XML配置文件的加载。

Try to use classpath:your_xml_config.xml , with out the folder structure prefix. 尝试使用classpath:your_xml_config.xmlclasspath:your_xml_config.xml带文件夹结构前缀。 like this: 像这样:

@Configuration
@Import(DispatcherServletConfig.class)
@ComponentScan(basePackages = "org.parkingTracker.controller, org.parkingTracker.service, org.parkingTracker.dao")
@ImportResource("classpath:dao-context.xml")
public class RootConfig {
    public RootConfig() {}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有定义类型为[javax.persistence.EntityManagerFactory]的合格Bean。“}} - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined"}} 使用Spring 4未定义类型为[javax.persistence.EntityManagerFactory]的合格Bean - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined Using Spring 4 MultitenantConfiguration:未定义类型为[javax.persistence.EntityManagerFactory]的合格Bean:预期的单个匹配Bean,但找到2 - MultitenantConfiguration: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2 没有定义类型为[javax.persistence.EntityManagerFactory]的合格Bean ::期望单个匹配的Bean,但找到2 - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined:: expected single matching bean but found 2 错误:没有定义类型为[javax.persistence.EntityManagerFactory]的合格bean:期望的单个匹配bean,但找到2 - error No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2 Spring JPA(Hibernate)没有类型的限定bean:javax.persistence.EntityManagerFactory - Spring JPA (Hibernate) No qualifying bean of type: javax.persistence.EntityManagerFactory 没有定义类型为[javax.persistence.EntityManagerFactory]的合格Bean:期望单个匹配的Bean,但找到2 - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2 没有可用于OSGi应用程序的&#39;javax.persistence.EntityManagerFactory&#39;类型的合格Bean - No qualifying bean of type 'javax.persistence.EntityManagerFactory' available for OSGi Application NoSuchBeanDefinitionException:没有可用的“javax.persistence.EntityManagerFactory”类型的合格 bean - NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available Spring MVC测试中未定义类型为[javax.persistence.EntityManagerFactory]的合格Bean - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined issue in Spring MVC test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM