Spring和Hibernate混淆了配置文件

时间:2015-09-20 21:00:14

标签: java xml spring hibernate spring-mvc

从现在开始,我正构建一个Spring 4.1.7.RELEASE / Hibernate 4.2.7.Final/Mysql 5.1.27 / Tomcat7 webapp,并且一直面临着一些不是真正的java代码的问题而是配置相关。我似乎无法理解那些xml文件和bean定义......今天我试图使用JPA为我的实体创建存储库,但我无法摆脱问题,尽管它已经解决了非常简单的代码。

我非常感谢您在理解bean定义和清理我的.xml文件方面的一些帮助。

我越来越多的错误:

**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**

以下是项目的xml文件:

调度-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>

根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

<!-- 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>

我无法找到解决此问题的任何地方。

此致 马特

2 个答案:

答案 0 :(得分:1)

经过大量的研究,我已经解决了问题的根源。

尽管spring版本受到了jpa-data依赖的“干扰”,但主要的混淆是在root-context.xml中开始的,我误解了entityManagerFactory的实际构思并使用了hibernate sessionFactory,而我试图实现JPA-Hibernate方法: “org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean”。

尽管如此,我还是为那些可能遇到类似问题的人提供了正确的root-context.xml内容:

从控制器排除组件扫描开始(因为它们应该在调度程序servlet中):

@Data
@NoArgsConstructor
public class ValidationError {
    ...
}

使用例如HSQL用于开发者体育场使用:

<context:component-scan base-package="GroupId of your Api">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

其余的和最重要的jpa实体经理:

<jdbc:embedded-database type="HSQL" id="dataSource"/>

由于data-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"/>

在pom.xml中依赖堆栈之前,当然不要忘记添加spring-boot-starter-data依赖。

这就是全部。

希望有人觉得它很有用。 干杯!

答案 1 :(得分:0)

这看起来像是依赖项的问题。如果您没有使用像Maven这样的依赖管理工具,我建议您这样做。如果你自己尝试这样做,几乎不可能引入正确的依赖关系。

如果您使用的是依赖关系管理工具,请确保类路径上的所有Spring jar都是相同的版本。