Spring JPA (Hibernate) No qualifying bean of type: javax.persistence.EntityManagerFactory
我正在使用 Spring-boot,因此在 Tomcat 7 中部署了一场 war 。当我启动应用程序时,我得到以下信息:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
Dec 30, 2013 7:41:06 PM org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring FrameworkServlet ‘dispatcherServlet’ 2013–12–30 19:41:06 INFO DispatcherServlet:461 – FrameworkServlet ‘dispatcherServlet’: initialization started 2013–12–30 19:41:06 INFO SimpleUrlHandlerMapping:315 – Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2013–12–30 19:41:06 INFO RequestMappingHandlerMapping:181 – Mapped"{[/user],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.cloudfordev.controlpanel.orm.User com.cloudfordev.controlpanel.GetController.getUser(int) 2013–12–30 19:41:07 INFO SimpleUrlHandlerMapping:315 – Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2013-12-30 19:41:07 INFO SimpleUrlHandlerMapping:315 – Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2013-12-30 19:41:08 INFO DispatcherServlet:480 – FrameworkServlet ‘dispatcherServlet’: initialization completed in 1957 ms Dec 30, 2013 7:41:08 PM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-nio-8080"] Dec 30, 2013 7:41:08 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector INFO: Using a shared selector for servlet write/read Dec 30, 2013 7:41:08 PM org.apache.coyote.AbstractProtocol stop INFO: Stopping ProtocolHandler ["http-nio-8080"] Dec 30, 2013 7:41:08 PM org.apache.coyote.AbstractProtocol pause INFO: Pausing ProtocolHandler ["http-nio-8080"] Dec 30, 2013 7:41:08 PM org.apache.catalina.core.StandardService stopInternal INFO: Stopping service Tomcat Dec 30, 2013 7:41:08 PM org.apache.coyote.AbstractProtocol stop INFO: Stopping ProtocolHandler ["http-nio-8080"] Dec 30, 2013 7:41:08 PM org.apache.coyote.AbstractProtocol destroy INFO: Destroying ProtocolHandler ["http-nio-8080"] 2013-12-30 19:41:08 INFO AutoConfigurationReportLoggingInitializer$AutoConfigurationReportLogger:108 – ========================= Positive matches: MessageSourceAutoConfiguration PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer DataSourceAutoConfiguration DataSourceTransactionManagerAutoConfiguration JpaBaseConfiguration.JpaWebConfiguration DispatcherServletAutoConfiguration DispatcherServletAutoConfiguration#dispatcherServlet EmbeddedServletContainerAutoConfiguration EmbeddedServletContainerAutoConfiguration.EmbeddedTomcat ServerPropertiesAutoConfiguration#serverProperties WebMvcAutoConfiguration WebMvcAutoConfiguration#hiddenHttpMethodFilter WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#defaultViewResolver Negative matches: RabbitAutoConfiguration AopAutoConfiguration BatchAutoConfiguration JpaRepositoriesAutoConfiguration MongoRepositoriesAutoConfiguration DataSourceAutoConfiguration.DbcpConfiguration DataSourceAutoConfiguration.EmbeddedConfiguration DataSourceAutoConfiguration.JdbcTemplateConfiguration DataSourceAutoConfiguration.TomcatConfiguration DataSourceTransactionManagerAutoConfiguration#transactionManager JmsTemplateAutoConfiguration DeviceResolverAutoConfiguration HibernateJpaAutoConfiguration ReactorAutoConfiguration ThymeleafAutoConfiguration EmbeddedServletContainerAutoConfiguration.EmbeddedJetty MultipartAutoConfiguration WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#beanNameViewResolver WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#viewResolver WebSocketAutoConfiguration java.lang.reflect.InvocationTargetException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1180) … 22 more |
这就是我的应用程序的启动方式:
1
2 3 4 5 6 7 8 9 |
@Configuration @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { |
当我的 Spring 控制器处理连接时:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@Controller public class GetController { private UserService userService; @Autowired @RequestMapping(value ="/user", method = RequestMethod.GET) user = userService.getUser(id); return user; |
它在 userService 上执行 getUser:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@Component public class UserService { private UserDAO UserDao; public UserDAO getUserDao() { @Autowired public User getUser(int id) { |
使用userDao查找实体:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Repository("userDao") @Transactional(propagation=Propagation.REQUIRED) public class UserDAO { @PersistenceContext public void insert(User user) { public User load(int id) { |
我在 /src/main/resources 中有以下 spring-config.xml:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-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"> <context:component–scan base–package="com.mydomain.orm" /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <tx:annotation–driven transaction–manager="transactionManager" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> |
最后但并非最不重要的是以下 /src/main/resources/persistence.xml:
1
2 3 4 5 6 7 |
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence–unit name="userPersistenceUnit" transaction–type="RESOURCE_LOCAL"> <class>com.mydomain.orm.User</class> </persistence–unit> </persistence> |
我需要做什么来解决这个错误?
你想要一个
回答已编辑的问题:您的 XML 文件未包含在内,因为您从
1
|
@ImportResource("classpath:spring-config.xml")
|
或者,您可以将 XML 配置迁移到 JavaConfig 格式,Spring 将对其进行组件扫描。
您的问题是您正在使用注释配置,该配置已经在类路径中搜索带注释的 bean,这会尝试实例化
您可以通过
将 JavaConfig 与 XML 配置结合起来
1
2 3 4 5 6 7 8 9 10 |
@Configuration @ComponentScan @EnableAutoConfiguration @ImportResource("classpath:spring-config.xml") public class Application { public static void main(String[] args) { |
1 2 3 4 5 6 7 8 9 10 11 | @Controller public class GetController { @Autowired UserService userService; @RequestMapping(value ="/user", method = RequestMethod.GET) return userService.getUser(id); |
1 2 3 4 5 6 7 8 9 10 | @Component public class UserService { @Autowired public User getUser(int id) { |
我是否可以建议将 SpringData 用于 DAO,您将节省大量样板代码。
查看崩溃产生的自动配置报告,我可以看到没有定义
顺便说一句,如果你以正确的方式使用 Spring Boot,XML 文件是多余的。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/267534.html