zoukankan      html  css  js  c++  java
  • Spring详解(十)加载配置文件

    在项目中有些参数经常需要修改,或者后期可能会有改动时,那我们最好把这些参数放到properties文件中,在源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源码。下面讨论spring两种加载方式,基于xml和基于注解的加载方式。

    1. 通过xml方式加载properties文件

    以Spring实例化dataSource为例,先在工程目录的src下新建一个conn.properties文件,里面写上上面dataSource的配置:

    dataSource=com.mchange.v2.c3p0.ComboPooledDataSource  
    driverClass=com.mysql.jdbc.Driver  
    jdbcUrl=jdbc:mysql://localhost:3306/shop 
    username=root  
    password=root 

    然后在只需要在beans.xml中做如下修改即可:

     <context:property-placeholder location="classpath:conn.properties"/><!-- 加载配置文件 -->  
      
     <!-- com.mchange.v2.c3p0.ComboPooledDataSource类在c3p0-0.9.5.1.jar包的com.mchange.v2.c3p0包中 -->  
     <bean id="dataSource" class="${dataSource}"> <!-- 这些配置Spring在启动时会去conn.properties中找 -->  
        <property name="driverClass" value="${driverClass}" />  
        <property name="jdbcUrl" value="${jdbcUrl}" />  
        <property name="user" value="${user}" />  
        <property name="password" value="${password}" />  
     </bean>  

    标签也可以用下面的标签来代替,可读性更强:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations"> <!-- PropertyPlaceholderConfigurer类中有个locations属性,接收的是一个数组,即我们可以在下面配好多个properties文件 -->  
            <array>  
                <value>classpath:conf.properties</value>  
            </array>  
        </property>  
    </bean>

    2. 通过注解方式加载properties文件

    首先新建一个资源文件:public.properties

    shop.url=http://magic/shop

    第一种配置方式:

        <!-- 确保可在@Value中, 使用SeEL表达式获取资源属性 -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="properties" ref="configProperties" />
        </bean>
    
        <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <list>
                    <value>classpath:config/*.properties</value>
                </list>
            </property>
        </bean>

    在java代码中用@Value获取配置属性值

        @Value("${shop.url}")
        private String url;

    还有一种方式更简洁:

        <bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
            <property name="fileEncoding" value="UTF-8"/> 
            <property name="locations"><!-- 这里是PropertiesFactoryBean类,它也有个locations属性,也是接收一个数组,跟上面一样  -->
                <array>  
                    <value>classpath:public.properties</value>  
                </array>  
            </property>  
        </bean> 
        <!--或者-->
        <context:property-placeholder location="classpath:public.properties" />  
        
        //注意,这种表达式要有set方法才能被注入进来,注解写在set方法上即可
        private String url;  
        @Value("#{prop.shop.url}")   
        //@Value表示去beans.xml文件中找id="prop"的bean,它是通过注解的方式读取properties配置文件的,然后去相应的配置文件中读取key=shop.url的对应的value值  
        public void setUrl(String url) {  
            this.token= url;  
        }

    3.通过 @PropertySource和@Value 来读取配置文件

    @Component
    @PropertySource(value = {"classpath:common.properties", "classpath:abc.properties"})
    public class Configs {
    
        @Value("${connect.api.apiKeyId}")
        public String apiKeyId;
    
        @Value("${connect.api.secretApiKey}")
        public String secretApiKey;
    
        public String getApiKeyId() {
            return apiKeyId;
        }
    
        public String getSecretApiKey() {
            return secretApiKey;
        }
    }

    我们来具体分析下:

    1、@Component注解说明这是一个普通的bean,在Component Scanning时会被扫描到并被注入到Bean容器中;我们可以在其它引用此类的地方进行自动装配。@Autowired这个注解表示对这个bean进行自动装配。 比如:

    @Controller
    public class HomeController {
    
        @Autowired
        private Configs configs;
    }

    2、@PropertySource注解用来指定要读取的配置文件的路径从而读取这些配置文件,可以同时指定多个配置文件;

    3、@Value("${connect.api.apiKeyId}")用来读取属性key=connect.api.apiKeyId所对应的值并把值赋值给属性apiKeyId;

    在项目中有些参数经常需要修改,或者后期可能会有改动时,那我们最好把这些参数放到properties文件中,在源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源码。下面讨论spring两种加载方式,基于xml和基于注解的加载方式。

  • 相关阅读:
    披萨
    扩展gcd
    LOJ6276 果树
    BZOJ 2038: [2009国家集训队]小Z的袜子(hose) | 莫队
    BZOJ 3052: [wc2013]糖果公园 | 树上莫队
    BZOJ 1878: [SDOI2009]HH的项链 | 莫队
    BZOJ 2453 维护队列 | 分块
    BZOJ 2821: 作诗(Poetize) | 分块
    BZOJ 2653 middle | 主席树
    BZOJ 1901: Zju2112 Dynamic Rankings | 带修改主席树
  • 原文地址:https://www.cnblogs.com/deityjian/p/11445845.html
Copyright © 2011-2022 走看看