Spring-Bean configuration - using external property files (transfer)

Spring-Bean Configuration - Using External Properties File

So you can get the key-value of the configuration file through the @value annotation to generate a configuration file bean. A way to use beans directly in code.

 

• When configuring beans in configuration files, it is sometimes necessary to mix system deployment details (eg file paths, data source configuration information, etc.) into the bean configuration. These deployment details actually need to be separated from the bean configuration
• Spring provides a BeanFactory post-processor for PropertyPlaceholderConfigurer, which allows users to move part of the Bean configuration to the properties file. Variables in the form of ${var} can be used in the Bean configuration file, and the PropertyPlaceholderConfigurer is derived from the property Load properties in the file, and use those properties to replace variables.
• Spring also allows the use of ${propName} in property files to enable mutual references between properties.
Case: Use db.properties to configure the information for connecting to the database, obtain the information through the bean configuration file, and then create a data source;
The db.properties configuration information is as follows:
[plain]  view plain copy  
 
  1. user=scott  
  2. password=tiger  
  3. dirverClass=oracle.jdbc.driver.OracleDriver  
  4. jdbcUrl=jdbc\:oracle\:thin\:@localhost\:1521\:oracl  
beans configuration file: applicationContext_properties.xml
[html]  view plain copy  
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
  7.       
  8.     <!-- Import properties file -->  
  9.     <context:property-placeholder location="classpath:db.properties"/>  
  10.       
  11.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  12.         <!-- Use properties from external properties file -->  
  13.         <property name="user" value="${user}"></property>  
  14.         <property name="password" value="${password}"></property>  
  15.         <property name="driverClass" value="${dirverClass}"></property>  
  16.         <property name="jdbcUrl" value="${jdbcUrl}"></property>  
  17.     </bean>  
  18. </beans>  
Create the data source code as follows:
[java]  view plain copy  
 
  1. ApplicationContext axt = new ClassPathXmlApplicationContext("applicationContext_properties.xml");  
  2. DataSource dataSource = (DataSource)axt.getBean("dataSource");  
  3. System.out.println(dataSource.getConnection());  

Guess you like

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