• 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"/>-->
  • 相关阅读:
    vue中处理过内存泄露处理方法
    Nuxt2.x项目实战相关问题记录
    Android 7.0+模拟器Fiddler抓包详细教程 fiddler443问题解决办法
    inspeckage
    yile接口
    入坑Vue(二)Node.js环境安装
    入坑Vue(一)简介
    入库Vue(三) vuecli全局安装,创建Vue项目
    入坑vue(三)安装VSCode
    入坑Vue(四) 引用element ui组件,配置登录
  • 原文地址:https://www.cnblogs.com/zjj1996/p/9139377.html
Copyright © 2020-2023  润新知