【问题标题】:Best configuration for spring 3.2.3spring 3.2.3 的最佳配置
【发布时间】:2013-08-20 18:40:55
【问题描述】:

我正在尝试了解弹簧配置。我读了两篇文章:

  1. http://www.onjava.com/pub/a/onjava/2006/03/22/advanced-spring-configuration.html?page=1
  2. http://syntx.io/difference-between-loading-context-via-dispatcherservlet-and-contextloaderlistener/

这些建议有 2 个配置文件:“Application Context”和“Web Application Context”。

如果您曾经尝试使用 Spring MVC 框架开发 Web 应用程序,您就会知道应该使用两个配置文件:

/WEB-INF/applicationContext.xml 允许你配置你的 bean,或者 指示您的应用程序的上下文。这是地方 您定义业务逻辑 bean、资源和所有其他 bean 与网络层没有直接关系。

/WEB-INF/[servlet-name]-servlet.xml 用于配置Web层 并查看解析器、控制器、验证器和所有其他 你需要在 MVC 框架中。 [servlet-name] 指的是名称 在 web.xml 部署中定义的 Spring 的调度程序 servlet 描述符。

据此,我将 web.xml 编写如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">


    <!-- Application Context -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,
            /WEB-INF/spring-security.xml</param-value>
    </context-param>


    <!-- Spring MVC -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</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>


    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Fin Spring Security -->

</web-app>

这是我的 applicationContext.xml:

<?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:jee="http://www.springframework.org/schema/jee"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">


  <!-- Look in tom cats context -->
  <jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/rhcimax"/>

  <!-- Hibernate Session Factory -->
  <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
      <array>
        <value>com.blah.baseProject</value>
      </array>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.MySQLDialect
      </value>
    </property>
  </bean>

  <!-- Hibernate Transaction Manager -->
  <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
  </bean>

  <!-- Activates annotation based transaction management -->
  <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

这是我的 mvc-dispatcher-servlet.xml:

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

  <!-- Enable @Controller annotation support -->
  <mvc:annotation-driven />

  <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
  <mvc:resources mapping="/resources/**" location="/resources/" />

  <!-- Map simple view name such as "test" into /WEB-INF/views/test.jsp -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/view/" />
    <property name="suffix" value=".jsp" />
  </bean>

  <!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
  <context:component-scan base-package="com.blah.baseProject"/>


</beans>

我想知道这个配置是否几乎正确。此配置运行,但我觉得 applicationContext.xml 没有被调用,因为我得到了这个异常:

org.hibernate.HibernateException: No Session found for current thread

我的目的是在春季保持良好实践并学习正确的配置。

“最好的做法是在中间层之间保持清晰的分隔 业务逻辑组件和数据访问类等服务 (通常在 ApplicationContext 中定义)和 web- 相关组件,例如控制器和视图解析器(即 在每个 Dispatcher Servlet 的 WebApplicationContext 中定义)。”

【问题讨论】:

    标签: spring hibernate spring-mvc


    【解决方案1】:

    您可以将 web.xml 上下文参数更改为:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
    </context-param>
    

    并创建任意数量的上下文文件而不将其导入根上下文

    当你有多模块项目并将配置文件放入每个模块时非常有用,将其命名为applicationContext-.xml,它将被自动扫描。

    在 mvc-dispatcher-servlet.xml 中声明 mvc 组件是一种很好的做法:拦截器(语言环境、主题拦截器和您自己的)、视图解析器、资源、异常处理程序、模板引擎配置和其他与看法。

    另外,在 servlet 配置中为控制器声明组件扫描也很有用:

    mvc-dispatcher-servlet.xml:

    <context:component-scan base-package="by.company.app" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>
    

    在 applicationContext 中排除 @Controller 扫描时:

    applicationContext.xml:

    <context:component-scan base-package="by.company.app">
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>
    

    这有助于避免重复的 bean 定义

    单独的 applicationContexts 示例(省略命名空间声明):

    applicationContext.xml:

    <beans>    
        <context:property-placeholder location="classpath*:META-INF/spring/a-*.properties" />
    
        <task:annotation-driven/>
    
        <context:spring-configured/>
    
        <context:component-scan base-package="by.company.app">
            <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
        </context:component-scan>
    </beans>
    

    applicationContext-db.xml

    <beans>
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="url" value="jdbc:mysql://${database.host}:${database.port}/${database.db-path}" />
            <property name="driverClassName" value="${database.driverClassName}" />
            <property name="username" value="${database.username}" />
            <property name="password" value="${database.password}" /> 
        </bean>
    
        <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
    
        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
    
        <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
            <property name="persistenceUnitName" value="persistenceUnit"/>
            <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
            </property>
            <property name="jpaProperties"> 
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.max_fetch_depth">3</prop> 
                    <prop key="hibernate.jdbc.fetch_size">50</prop> 
                    <prop key="hibernate.jdbc.batch_size">10</prop> 
                    <prop key="hibernate.show_sql">false</prop>
                    <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                    <prop key="hibernate.connection.charSet">UTF-8</prop>
                    <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
                </props> 
            </property>
        </bean>
    
        <jpa:repositories base-package="by.company.app" />
    
    </beans>
    

    【讨论】:

    • 他非常感谢!!正如您所提到的,我已经为 servlet 配置中的控制器声明了组件扫描,现在一切正常!!我没有得到例外:)。最后一件事:你能分享你的 application-context.xml 和 application-context-db.xml 吗?
    【解决方案2】:

    根据我的经验,只要您没有任何特定理由将核心配置与 webapp 配置分开,最好的方法是将 DispatcherServlet 的上下文保持为空,并将所有内容放入根应用程序上下文 (@987654322 @等)。

    这样做可以避免许多可能出现的问题,尤其是:

    • 应该在每个上下文的基础上声明诸如&lt;tx:annotation-driven&gt; 之类的后处理器(这是您的问题)。如果您在 servlet 的上下文中没有 bean,则不需要在那里复制这些声明
    • 在两种上下文中为同一个包声明 &lt;context:component-scan&gt; 可能会导致重复 bean 定义的严重问题

    如果您担心单个整体上下文的可管理性,请记住您仍然可以将其拆分为多个文件,就像您已经这样做的那样。

    请注意,如果DispatcherServlet 从 XML 文件中读取上下文配置(因为它是默认配置的),您仍然必须创建一个具有有效根元素的 XML 文件。但是,有一个小技巧:如果你将DispatcherServlet 配置为使用基于注解的配置,默认情况下它会为空,无需任何额外的努力:

    <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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    

    【讨论】:

    • “最好的方法是保持 servlet 的上下文为空,并将所有内容放入根上下文”,这意味着将 mvc-dispatcher-servlet.xml (“WebApplicationContext”) 留空?我假设这是因为““WebApplicationContext”是应用程序上下文的子上下文。”。
    • 我非常不同意您的方法,因为这会导致不必要的担忧混合。与 MVC(表示层)相关的所有内容都将在调度程序 servlet 配置中定义(并且您可以拥有多个调度程序 servlet),与核心域相关的所有内容都将包含在根 webapp 上下文中(即任何调度程序特定上下文的父级)。
    • @CostiCiudatu:我理解你的意思,但我更喜欢遵循“按功能打包,而不是按层原则”。我相信将单个上下文分解为特定于功能的部分比将配置分解为特定于层的上下文更有意义。
    • 我明白了;然而,我发现了几个场景,其中核心业务配置(数据源、存储库、服务和东西)不仅需要 webapp 配置。因此,将其作为单独的上下文使其可重用。但我确实同意,情况可能并非总是如此。
    猜你喜欢
    • 2014-11-05
    • 2010-10-29
    • 2014-03-30
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2012-02-08
    • 1970-01-01
    相关资源
    最近更新 更多