[英]Spring and Hibernate messed up configuration files
From some time now I'm building an Spring 4.1.7.RELEASE/Hibernate 4.2.7.Final/Mysql 5.1.27/Tomcat7 webapp and all the time face some problems that are not really java code but rather configuration related. 从现在开始,我正在构建一个Spring 4.1.7.RELEASE / Hibernate 4.2.7.Final/Mysql 5.1.27 / Tomcat7 webapp,并且一直都遇到一些问题,这些问题并不是真正的Java代码,而是与配置相关的。 I just can't seem to understand those xml files and beans definitions... Today I was trying to use JPA to create repositories for my entities and I can't get rid of the problems although it's very simple code. 我似乎似乎无法理解那些xml文件和bean定义...今天,我试图使用JPA为我的实体创建存储库,尽管它是非常简单的代码,但我无法摆脱这些问题。
I'd really appreciate some help with understanding beans definition and in cleaning my .xml files. 我非常感谢您在理解bean定义和清理.xml文件方面的帮助。
The errors I'm getting and getting again: 我越来越多的错误:
**SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/root-context.xml]; nested exception is java.lang.NoSuchMethodError: org.springframework.aop.config.AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(Lorg/springframework/beans/factory/xml/ParserContext;Ljava/lang/Object;)V**
Caused by: java.lang.NoSuchMethodError: org.springframework.aop.config.AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(Lorg/springframework/beans/factory/xml/ParserContext;Ljava/lang/Object;)V**
SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext**
Here are my xml files for the project: 这是我的项目XML文件:
dispatcher-servlet.xml dispatcher-servlet.xml
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<bean:annotation-driven />
<context:component-scan base-package="com.runninglife.controller" />
<!-- Handles HTTP GET requests for /web-resources/** by efficiently serving
up static resources in the ${webappRoot}/resources/ directory -->
<bean:resources mapping="/web-resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Apache Tiles Resolver & Config -->
<beans:bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.tiles3.TilesViewResolver">
</beans:bean>
<beans:bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<beans:property name="definitions">
<beans:list>
<beans:value>/WEB-INF/tiles/tiles-definitions.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>
root-context.xml root-context.xml
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- JPA Context for Repository -->
<jpa:repositories base-package="com.runninglife.repository" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.runninglife.entity.User</value>
</list>
</property>
<property name="packagesToScan">
<list>
<value>com.runninglife.*</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.runninglife" />
web.xml web.xml
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!-- We can also use url-pattern like: *.html / *.htm / *.json etc. -->
<url-pattern>/</url-pattern>
</servlet-mapping>
I can't find anywhere a solution to this problem. 我找不到任何解决此问题的方法。
Regards, Matt 问候,马特
After a horrible amount of research, I've got to the bottom of the problem. 经过大量的研究,我终于找到了问题的根源。
Although the spring versions were kind of 'interfered' by the jpa-data dependency, the main confusion started in root-context.xml where I've misunderstood actual idea of the entityManagerFactory and used the hibernate sessionFactory whereas I tried to implement JPA-Hibernate approach with the: " org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean ". 尽管春季版本受到jpa-data依赖的“干扰”,但主要的混乱始于root-context.xml,在这里我误解了entityManagerFactory的实际概念,并使用了hibernate sessionFactory,而我尝试实现JPA-Hibernate的方法:“ org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean”。
Nevertheless I'd put the correct root-context.xml content for those who might face the similar problem: 不过,对于那些可能遇到类似问题的人,我将放置正确的root-context.xml内容:
Starting with the component-scan with Controllers exclude (as they've should been in dispatcher servlet): 从使用Controllers排除组件扫描开始(因为它们应该在分派器servlet中):
<context:component-scan base-package="GroupId of your Api">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
Using eg HSQL for developer stadium usage: 使用例如HSQL进行开发人员使用:
<jdbc:embedded-database type="HSQL" id="dataSource"/>
And the rest and the most important jpa entity manager: 剩下的和最重要的jpa实体经理:
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="packagesToScan" value="PATH_TO_ENTITIES"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
</property>
</bean>
<!-- Transaction manager settings -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- JPA Context for Repository -->
<jpa:repositories base-package="PATH_TO_REPOSITORIES"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"/>
Also there is a possible solution to the spring-version interference because of data-jpa. 由于data-jpa,也有可能解决弹簧变形干扰。 If someone does still have the problem put a: 如果仍然有问题,请输入:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.6.RELEASE</version>
</parent>
Before your dependencies stack in pom.xml, and then of course don't forget to add the spring-boot-starter-data dependency. 在将依赖项堆栈放入pom.xml中之前,当然不要忘记添加spring-boot-starter-data依赖项。
That would be all. 仅此而已。
Hope anyone find it useful. 希望任何人都觉得有用。 Cheers! 干杯!
This looks like a problem with your dependencies. 这似乎与您的依赖项有关。 If you're not using a dependency management tool such a Maven I suggest you do. 如果您不使用像Maven这样的依赖管理工具,建议您这样做。 It's almost impossible to pull in the correct dependencies if you try to do it yourself. 如果您自己尝试这样做,几乎不可能获得正确的依赖关系。
If you are using a dependency management tool then make sure that all of the Spring jars on your classpath are the same version. 如果您使用的是依赖项管理工具,请确保您的类路径上的所有Spring jar都是相同的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.