Talk about Spring injection properties file summary

  • 2020-06-01 09:57:38
  • OfStack

spring provides a variety of ways to inject properties files, and this article provides a simple summary.

Introduced in the Spring configuration file

Method 1

through < context:property-placeholder / > The label


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

  <context:property-placeholder location="classpath:mysql.properties" ignore-unresolvable="true"/>

  <!--  Configure data source  -->
  <bean abstract="true" name="parentDatasource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${ds1.jdbc.driverClassName}" />
    <!--  Initializes the connection size  -->
    <property name="initialSize" value="1" />
    <!--  Connection pool maximum number of connections used  -->
    <property name="maxActive" value="100" />
    <!--  Connection pool minimum idle  -->
    <property name="minIdle" value="20" />
    <!--  Gets the maximum connection wait time  -->
    <property name="maxWait" value="30000" />
    <!-- <property name="poolPreparedStatements" value="true" /> -->
    <!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
    <property name="validationQuery" value="SELECT 1" />
    <property name="testOnBorrow" value="true" />
    <property name="testOnReturn" value="true" />
    <property name="testWhileIdle" value="true" />
    <!--  How long is the configuration interval 1 A secondary detection that detects idle connections that need to be closed in milliseconds  -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <!--  configuration 1 The minimum time a connection can live in a pool, in milliseconds  -->
    <property name="minEvictableIdleTimeMillis" value="25200000" />
    <!--  Open the removeAbandoned function  -->
    <property name="removeAbandoned" value="true" />
    <!-- 1800 Seconds, which is equal to 30 minutes  -->
    <property name="removeAbandonedTimeout" value="1800" />
    <!--  Shut down abanded Output error log while connecting  -->
    <property name="logAbandoned" value="true" />
    <!--  Monitoring database  -->
    <!-- <property name="filters" value="stat" /> -->
    <property name="filters" value="mergeStat" />
  </bean>

  <!--  Configure data source  -->
  <bean name="dataSource1" init-method="init" destroy-method="close" parent="parentDatasource">
    <property name="url" value="${ds1.jdbc.url}" />
    <property name="username" value="${ds1.jdbc.username}" />
    <property name="password" value="${ds1.jdbc.password}" />
  </bean>


  <!--  Configure data source  -->
  <bean name="dataSource2" init-method="init" destroy-method="close" parent="parentDatasource">
    <property name="url" value="${ds2.jdbc.url}" />
    <property name="username" value="${ds2.jdbc.username}" />
    <property name="password" value="${ds2.jdbc.password}" />
  </bean>

  <!--  Configure the transaction manager  -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource1" />
  </bean>

  <!--  Annotate way to configure things  -->
  <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

Way 2

through < util:properties / >

1, MySQL. properties


#
ds1.jdbc.driverClassName=com.mysql.jdbc.Driver
ds1.jdbc.url=jdbc:mysql://localhost:3306/process?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
ds1.jdbc.username=root
ds1.jdbc.password=root

ds2.jdbc.driverClassName=com.mysql.jdbc.Driver
ds2.jdbc.url=jdbc:mysql://localhost:3306/process?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
ds2.jdbc.username=root
ds2.jdbc.password=root

2, applicationContext xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd"
    default-lazy-init="false">

  <util:properties id="db" location="classpath:mysql.properties"/>

<!--  Configure data source  -->
  <bean name="dataSource1" init-method="init" destroy-method="close" parent="parentDatasource">
    <property name="url" value="#{db['ds1.jdbc.url']}" />
    <property name="username" value="#{db['ds1.jdbc.username']}" />
    <property name="password" value="#{db['ds1.jdbc.password']}" />
  </bean>
</beans>

Inject in code

Method 1

1, config. properties


name=ricky
age=27
password=root

2, applicationContext xml


<!--  Injection with annotations properties The values in the  -->
  <bean id="config"
     class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
      <list>
        <value>classpath:config.properties</value>
      </list>
    </property>
    <!--  Set the encoding format  -->
    <property name="fileEncoding" value="UTF-8"></property>
  </bean>

3. Use the @Value annotation


package com.ricky.codelab.springmvc.domain;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 * @create 2016-08-08 15:49
 */
@Component("userService")
public class UserServiceImpl implements IUserService {
  private final Logger logger = LoggerFactory.getLogger(getClass());

  @Value("#{config[name]}")
  private String name;

  @Value("#{config[age]}")
  private Integer age;

  @Value("#{config[password]}")
  private String password;

  @Override
  public void login(String username){
    System.out.println("name:"+name+",age="+age+",password="+password);
  }
}


Related articles: