spring 和 springmvc 的父子容器关系浅析

以###问题###
在工程项目中,一开始将所有的附件路径定义在工具类中。使用 static String 的形式;于是就在项目的开发过程中产生不便,也使得配置属性不灵活,不利于模块化的管理信息和提高项目的可扩展性以及维护性。因此就将项目的属性常量抽取 出来,配置的属性文件中。但在此过程中产生了在@controller 注解的模块中无法加载在属性文件配置的常量。 同时也使用了@service 注解层注入常量的方法。但均已失败告终。
最后在分析了spring 和 spring mvc 的源码和属性文件之间在web中的加载顺序和相互之间的关系。解决了此问题。以下是分析解决的过程。

代码

web.xml

    <display-name>demo-manager</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 初始化spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--解决post乱码-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- springmvc的前端控制器 -->
    <servlet>
        <servlet-name>demo-manager</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>demo-manager</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  • 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

spring-mvc.xml

        <!--配置包扫描器-->       
        <context:component-scan base-package="com.demo.controller"></context:component-scan>
        <!--配置注解驱动-->
        <mvc:annotation-driven/>
        <!--试图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean> 
        <!--静态资源映射-->
        <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
        <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

application-dao.xml

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:properties/*.properties"/>    
    <!-- 数据路连接池 --> 
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="maxActive" value="10"/>
        <property name="minIdle" value="5"/>
    </bean>
    <!-- spring管理sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据库连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载mybatis的全局配置文件-->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />       
    </bean> 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.demo.mapper"/>
    </bean>      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

以上的配置文件可以解决@service 中@value(“${ATTPATH}’) .
同时要想解决在@controller 层中注入属性配置文件中的值,经过自己测试,存在三种方式:

1.get set 方法

(1)在@service 中 注入:

@Value("${ATT_PATH}")
private String attPath;
  • 1
  • 2

(2)然后get set

(3)在@controller 中获取值。

2,公有化 @service中注入的值
@Value("${ATT_PATH}")
public  String attPath;
  • 1
  • 2
3.在spring-mvc.xml 中加载属性配置文件
<!-- 加载文件上传路径配置文件 解决@controller 中无法注入配置文件中的属性 -->
<context:property-placeholder location="classpath*:file.properties"/>
  • 1
  • 2

这样可以直接在@controller 中注入配置的属性文件

@Value("${ATT_PATH}")
private String attPath;
  • 1
  • 2

关于spring 和spring MVC的父子容器关系

在web.xml 中只加载spring-mvc.xml 则是没有问题的。但是只加载spring.xml 是错误的。
spring 是父容器, springmvc 是子容器,子容器可以使用父容器的对象,但是父容器是不能使用子容器的对象的。注意,这里是对象。在实际的项目中。我们通常是在spring.xml 中加载我们在资源包下定义的属性文件的,于是,我们也只有在@service 层中通过@value(“

http://m.blog.csdn.net/article/details?id=51088414

..)@controller使springmvc.xml@contrller@value(

猜你喜欢

转载自m635674608.iteye.com/blog/2318967