- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Spring + Hibernate + JPA 和 Tomcat 7 进行 REST 服务。当我启动应用程序时,我得到以下信息:
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
下面我展示了我的 spring 配置类
@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() {}
}
我还有用于访问数据的服务和 dao 类。为了在我的 dao 中注入(inject) EntityManager,我使用 @PersistenceContext
注释,为了为服务类注入(inject) dao,我使用简单的 spring 注释。下面你可以看到我的 DAO 布局的 spring xml 配置。 一个重要的评论,当我对 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:
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:
@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-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>
最佳答案
IOC 容器似乎无法注入(inject) EntityManagerFactory
到CarDaoImp。我查看了您的代码,似乎您定义了所有必要的配置,我认为问题在于您的 XML 配置文件加载。
尝试使用classpath:your_xml_config.xml
,不带文件夹结构前缀。像这样:
@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() {}
}
关于java - 未定义 [javax.persistence.EntityManagerFactory] 类型的合格 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39815038/
我的 webapp 包含许多 entityManagerFactories (DB)。每个都有自己独特的名称,例如 entityManagerFactoryApp、entityManagerFacto
这是来自堆栈跟踪的(底部)错误: Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory
I'm working on a basic application using Java Spring Boot, I'm stuck on this error:我正在使用Java Spri
我需要在测试之间重新创建 EntityManagerFactory,以确保我的序列在 JPA 序列生成器中重置以匹配数据库。 我需要重新创建 EMF,或者根据当前设置重新连接到数据源。 尽管 spri
我有一个使用hibernate的项目,启动时间很长,我想知道是否可以序列化EntitiyManagerFactory,这样只有第一次启动会很慢。 最佳答案 它可以在hibernate中序列化(在其他J
在查询对象和/或删除/创建时,我无法理解实体管理器的正确用法。现在,对于任何数据库事务,我有几种打开和关闭新实体管理器的服务方法,如下所示: public static Long getCountAp
我刚刚开始开发一个旨在支持现有 Web 项目的应用程序,因此业务层和持久层已经存在,并且在该 Web 应用程序中运行良好。现在,当我在控制台应用程序中使用两个层的库时,我收到一个错误,指出 JpaTr
我想要两个 EntityManagerFactories(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean 在这种情
我使用 eclipselink 作为 JPA 的实现,我想知道是否可以检查我的 EntityManagerFactory 是否“活着” 例如,如果连接参数无效,当我创建 EMF 时我不会有任何异常,直
我正在尝试 Autowiring 对不受 Spring 管理的类的依赖关系。为此,我使用以下内容 try (ClassPathXmlApplicationContext context = new C
我是 JPA 和 hibernate 的新手。在我的网络应用程序中,我已将 JDBC 代码更改为 JPA。在运行网络应用程序时,我收到了一大堆错误。但根据我对 JPA 和 Hibernate 的了解,
我正在开发一个需要执行一些数据库操作的应用程序。 我为 EntityManagerFactory 创建了一个静态变量,并在应用程序调用的方法中对其进行了初始化 if (emf == null) {
persistence.xml 文件 org.hibernate.ejb.HibernatePersistence com...... ApplicationContext.xm
有谁知道如何手动创建一个EntityManagerFactory?当我说手动时,我的意思是让它使用一个特殊的 persistence.xml 文件?这是我尝试过的,但都失败了。 Configurati
我是 ORM 的新手。我刚开始阅读有关使用 Hibernate 的 Java Persistence API 的书籍和文档。 我只是想知道,关闭EntityManagerFactory与关闭jdbc数
我的应用无法 Autowiring entityManagerFactory。 我的applicationContext.xml: classpath:jpa-pers
因此,尝试使用JPA实现运行第一个sprinboot应用程序,并得到以下错误: Description: Field personneDAO in com.example.demo.controlle
我可以在 bean 外部从 EntityManagerFactory 创建一个 EntityManager 吗?如果是这样,我该怎么做? 最佳答案 在非托管环境中(这就是您所说的 bean 外部的意思
我正在开发一个带有 MySql 数据库、JPA 对象和 EntityManagerFactory 的 java 应用程序,并使用 EclipseLink 来管理数据库。一切正常,但我有一个问题。 我的
我的 spring 代码出现以下错误,这是由实体类之一引起的。由于 Spring/JPA 没有提供任何关于错误来源的提示,我将不得不删除每个实体,直到找到罪魁祸首。通常会给出线索,但这里的情况并非如此
我是一名优秀的程序员,十分优秀!