zoukankan      html  css  js  c++  java
  • spring扫描配置文件

    spring配置文件对应的是父容器,springMVC配置文件产生的是子容器,前者一般配置数据源,事务,注解等,当然还可以进一步将一些配置细化到其他xml中;后者一般配置控制层相关的,如静态资源,视图解析器等。系统启动的时候,先初始化父容器,然后初始化子容器。这里会涉及一个问题,如果配置组件扫描时都配置全组件扫描,就会导致service组件会被扫描两次,造成事务无法处理。所以最好在springMVC配置文件中只做controller的扫描,在spring配置文件中扫描其他组件。 
    在spring的配置文件中配置:

    <context:component-scan base-package="com"/>
    • 1

    在springMVC的配置文件中配置:

    <context:component-scan base-package="com.**.controller"/>
    • 1

    这样就能各司其职了。 
    在使用中,这两个配置文件作用不同。如果要使用@Value注入一些系统配置文件中的变量时要注意:如果要在controller中使用注入的变量,需要在springMVC的配置文件中配置:

    <context:property-placeholder location="classpath:{your variable file}.properties"/>
    • 1

    如果只在spring的配置文件中配置,那么在controller中是不会注入成功的。 
    测试demo如下:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:applicationContext.xml",
    "classpath:servlet-dispatcher.xml"})
    public class InjecTest {
    
        @Value("${ly.key}")
        private String key;
    
        @Test
        public void test(){
            System.out.println("注入的key为:"+key);
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    基于@Value进行注入时有两种方式,占位符和spel表达式

        //占位符方式
        @Value("${jdbc.url}")
        private String url;
    • 1
    • 2
    • 3
        //SpEL表达方式,其中代表xml配置文件中的id值configProperties
        @Value("#{configProperties['jdbc.username']}")
        private String userName;
    • 1
    • 2
    • 3

    这两种方式需要在xml中配置时也是不一样的

        <!--基于占位符方式 配置单个properties -->
        <!--<context:property-placeholder location="conf/jdbc.properties"/>-->
        <!--基于占位符方式 配置多个properties -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:config/resource/dev/application.properties</value>
                    <value>classpath:config/resource/dev/lyframework.properties</value>
                    <value>classpath:config/resource/dev/common.properties</value>
                </list>
          </property>
        </bean>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
        <!--基于SpEL表达式 配置多个properties id值为configProperties 提供java代码中使用 -->
        <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <list>
                    <value>classpath:/conf/jdbc.properties</value>
                </list>
            </property>
        </bean>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
        <!--基于SpEL表达式 配置单个properties -->
        <!--<util:properties id="configProperties" location="classpath:conf/jdbc.properties"/>-->
  • 相关阅读:
    什么是看板方法?
    瓶颈法则
    累积流图——你还没有用过吗?
    为什么我们关注看板方法?
    蒟蒻报道
    博客更换通知
    浅谈树套树(线段树套平衡树)&学习笔记
    浅谈FFT(快速博立叶变换)&学习笔记
    题解 洛谷P1903/BZOJ2120【[国家集训队]数颜色 / 维护队列】
    题解 洛谷P4550/BZOJ1426 【收集邮票】
  • 原文地址:https://www.cnblogs.com/zjj1996/p/9139377.html
Copyright © 2011-2022 走看看