Use variable ${} for parameter configuration in Spring

Use variable ${} for parameter configuration in Spring

When using Spring, in some cases, in the configuration file, you need to use variables to configure bean-related property information . For example, the following database connection uses ${} to configure, as shown below:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

So where does the specific value of the variable in the above ${} come from? I am sure that many friends have the same questions as me at the beginning, but it is actually very simple. There are two ways to configure:

Method 1: Use bean injection to introduce configuration files:

<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath*:jdbc.properties" />
</bean>

Method 2: Spring3 provides a simpler way, using automatic scanning:

<!-- 配置文件 -->
<context:property-placeholder location="classpath*:jdbc.properties" />

Both of the above two methods can load the specified configuration file, and then through the reference of the ${} symbol, you can modify the variable externally to switch, without changing the internal value every time!

The content format of the jdbc.properties file is also very simple, in the form of key-value pairs, # indicates a comment, such as:

Copy code

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true
username=root
password=root
#定义初始连接数 
initialSize=5
#定义最大连接数  
maxActive=20
#定义最大空闲  
maxIdle=20
#定义最小空闲  
minIdle=1
#定义最长等待时间  
maxWait=60000

In addition, the ${} variable is used to indicate the name of the file to be loaded, and then specify the file to load the configuration in different environments

1. Use variables to indicate the name of the file to be loaded

<context:property-placeholder location="classpath:${config}_base.properties" file-encoding="UTF-8"/>

2. Set the tomcat JAVA_OPTS parameter, specify the config variable value when tomcat is started, dev_base.properties can be automatically loaded when tomcat starts, so that different configuration files can be loaded in different environments without modifying the file name.

set JAVA_OPTS= -Dconfig=dev

The function of -D is to set the system attribute value, please refer to:
https://blog.csdn.net/u012345283/article/details/40823637

Windows/linux setting JAVA_OPTS can refer to:
https://blog.csdn.net/yjxandsp/article/details/53082519

Guess you like

Origin blog.csdn.net/u011582840/article/details/108060076