spring read configuration file


1. Read the configuration file from the xml file [take the configuration of the database as an example]

1.1 配置:
  方法一:<context:property-placeholder location="classpath:jdbc.properties"/>
  方法二:
  <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
    <list>
     <value>jdbc.properties</value>
    </list>
   </property>
  </bean>
 
1.2 使用【${propertyKey}:propertyKey为配置文件的key值】:
  <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="${jdbc.maxActive}"/>
   <property name="minIdle" value="${jdbc.minIdle}"/>
  </bean>

1.3 Attention

       There can only be one bean definition in the <context:property-placeholder /> tag or PropertyPlaceholderConfigurer. If there are more than one, the first definition will be read, and the later definition will be ignored.

2. Read the configuration file in the Java code [classpath: the root directory of the compiled code after the project is compiled, eg: myProject/WEB-INF/classes/]
 2.1 Configuration:
  Method 1: <util:properties id="propertiesReader" location= "classpath:commons.properties"/>
  Method 2:
   <bean id="refProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
     <list>
      <value>classpath :commons.properties</value>
     </list>
    </property>
   </bean>
 2.2 Use [@Value(" # {PropertiesFactoryBeanId['propertyKey']}"): PropertiesFactoryBeanId is the id of PropertiesFactoryBean (or <util: properties> tag id), propertyKey is the configuration file (*.properties)的key值】:
  @Component 
  public class SysConf { 
   
   @Value("#{propertiesReader['jdbc.ur']}") 
   private String jdbcUrl; 
   
   @Value("#{propertiesReader['jdbc.ur']}") 
   public void setTest(String jdbcUrl){ 
    jdbcUrl = jdbcUrl; 
   } 
   
   @Value("#{propertiesReader}") 
   public void setSysConf(Properties propertiesReader){ 
    jdbcUrl= propertiesReader.getProperty("jdbc.ur"); 
   } 
  } 

3. Pay attention to the difference between the usage of xm and Java annotations, xml uses $, and Java annotations use #


1. Read the configuration file from the xml file [taking the configuration of the database as an example] 1.1 Configuration:
  Method 1: <context:property-placeholder location="classpath:jdbc.properties"/>
  Method 2:
  <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
    <list>
     <value>jdbc.properties</value>
    </list>
   </property>
  </bean>
 
1.2 Use [ $ {propertyKey}: propertyKey is the key value of the configuration file]:
  <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="${ jdbc.maxActive}"/>
   <property name="minIdle" value="${jdbc.minIdle}"/>
  </bean> 1.3 Note that there can only be one <context:property-placeholder /> tag or bean definition of PropertyPlaceholderConfigurer , if there are more than one, the first definition will be read, and the later definition will be ignored 2. Read the configuration file in the Java code [classpath: the compiled code root directory for the project, eg: myProject/WEB-INF/classes /]
 2.1 Configuration:
  Method 1: <util:properties id="propertiesReader" location="classpath:commons.properties"/>
  Method 2:
   <bean id="refProperties" class="org.springframework.beans.factory.config .PropertiesFactoryBean">
    <property name="locations">
     <list>
      <value>classpath:commons.properties</value>
     </list>
    </property>
   </bean>
 2.2 使用【@Value(" # {PropertiesFactoryBeanId['propertyKey']}"):PropertiesFactoryBeanId为PropertiesFactoryBean的id(或者是<util:properties>标签的id),propertyKey为配置文件(*.properties)的key值】:
  @Component 
  public class SysConf { 
   
   @Value("#{propertiesReader['jdbc.ur']}") 
   private String jdbcUrl; 
   
   @Value("#{propertiesReader['jdbc.ur']}") 
   public void setTest(String jdbcUrl){ 
    jdbcUrl = jdbcUrl; 
   } 
   
   @Value("#{propertiesReader}") 
   public void setSysConf(Properties propertiesReader){ 
    jdbcUrl= propertiesReader.getProperty("jdbc.ur"); 
   } 
  } 3. Pay attention to the difference between the usage of xm and Java annotations, use $ for xml and # for Java annotations

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326264360&siteId=291194637