【问题标题】:Bean Context in SpringSpring 中的 Bean 上下文
【发布时间】:2013-03-12 17:48:55
【问题描述】:

该应用程序与以下配置文件完美配合:

我的Web.XML如下

<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/classes/spring/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

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

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


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

我的 mvc-dispatcher-servlet

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>


<context:component-scan base-package="biz.canisrigel.slapMe" />

<!-- enable autowire -->
<context:annotation-config />

我的 spring-context.xml 是

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<context:component-scan base-package="biz.canisrigel.slapMe" />

<!-- enable autowire -->
<context:annotation-config />


<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/slapMe" />
    <property name="username" value="root" />
    <property name="password" value="adminadmin" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="biz.canisrigel.slapMe.bean" />
</bean>

<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="biz.canisrigel.slapMe.mapper" />
</bean>

我无法正确运行 spring security,因为之前的 spring-context.xml 是 MVC 的调度程序 servlet 的 xml 配置文件。所以我将 spring-context 移动到 contextConfigLocation。但后来我不得不为调度员 servlet 提供一些东西。

我的问题是 mvc-dispatcher-servlet.xml 和 spring-context 具有相同的数据。如果我删除 mvc-dispatcher 它是一个错误。如果我没有将 mvc-dispatcher 的内容放在 spring 上下文中,那么也会发生错误。

我对概念的理解哪里出了问题。

【问题讨论】:

    标签: spring jakarta-ee servlets web.xml


    【解决方案1】:

    您的 MVC 上下文应该只有与 MVC 相关的配置,通常包括 ViewResolvers、FileUpload、PropertyFiles、消息/主题解析器等。您的 applicationContext 将包含与 DAO、Service 和其他实用程序相关的 bean。安全文件应该有安全配置。要更好地了解和了解良好/推荐的做法,请查看spring greehouse 代码。

    【讨论】:

      【解决方案2】:

      一些事情:

      • 您的 web.xml 看起来正确
      • InternalResourceViewResolver 应该只存在于您的 mvc-dispatcher-servlet.xml 中,因为它与 MVC 直接相关——从您的 spring-context.xml 中删除它
      • 您可以在两个配置文件中使用context:component-scan,但它们应该扫描不同的包。 servlet xml 中的一个通常是组件扫描您的控制器包(以及与 MVC 直接相关的任何其他内容),而父 spring 上下文 xml 中的一个通常是扫描您的服务、DAO 等。通常,您的组件扫描包应该非常具体; 你不应该将它指向整个应用程序的基础包!
      • 您可以删除context:annotation-config -- 如果您有context:component-scan,则它是多余的

      【讨论】:

      • 谢谢。只是怀疑你说我永远不应该将组件扫描指向基础包。但我有类似 biz.canisrigel.slapMe.dao 和 biz.canisrigel.slapMe.bo biz.canisrigel.slapMe.controllers 的东西。所以我必须使用 biz.canisrigel.slapMe 进行扫描
      • 你应该有一个单独的context:component-scan 为每个这些包,然后...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多