在開發的過程中,配置文件往往就是那些屬性(properties)文件,比如使用properties文件配置數據庫文件,又如database-config.properties
代碼清單:database-config.properties
jdbc.database.driver=com.mysql.cj.jdbc.Driver jdbc.database.url=jdbc:mysql://localhost:3306/springmvc?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true jdbc.database.username=root jdbc.database.password=123456
使用屬性文件可以有效地減少硬編碼,很多時候修改環境只需要修改配置文件就可以了,這樣能夠有效提高運維人員的操作便利性,所以使用properties文件是十分常見的場景。在Spring中也可以通過注解或者XML的方式進行加載屬性文件
使用注解方式加載屬性文件
Spring提供了注解@PropertySource來加載屬性文件,了解它的配置項。
•name:字符串,配置這次屬性配置的名稱。
•value:字符串數組,可以配置多個屬性文件。
•ignoreResourceNotFound:boolean值,默認為false,其含義為如果找不到對應的屬性文件是否進行忽略處理,由於默認值為false,所以在默認的情況下找不到對應的配置文件會拋出異常。
•encoding:編碼,默認為""。
代碼清單:在Spring環境中使用屬性文件Java配置
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration // @ComponentScan(basePackages = {"com.ssm.chapter10.annotation"}) @ImportResource({"classpath:ssm/chapter10/spring-dataSource.xml"}) @PropertySource(value = {"classpath:ssm/chapter10/database-config.properties"}) public class ApplicationConfig2 { }
代碼清單:測試加載屬性
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig2.class); String url = context.getEnvironment().getProperty("jdbc.database.url"); System.out.println(url);
Spring中是沒有解析屬性占位符的能力,Spring推薦使用一個屬性文件解析類進行處理,它就是PropertySources Placeholder-Configurer,使用它就意味着允許Spring解析對應的屬性文件,並通過占位符去引用對應的配置。
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration // @ComponentScan(basePackages = {"com.ssm.chapter10.annotation"}) @ImportResource({"classpath:ssm/chapter10/spring-dataSource.xml"}) @PropertySource(value = {"classpath:ssm/chapter10/database-config.properties"}) public class ApplicationConfig2 { /** * 作用是為了讓Spring能夠解析屬性占位符, */ @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }
引用已經定義好的配置,這里可以使用注解@Value和占位符
import org.apache.commons.dbcp.BasicDataSourceFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.util.Properties; @Component public class ProfileDataSource { @Value("${jdbc.database.driver}") private String driver = null; @Value("${jdbc.database.url}") private String url = null; @Value("${jdbc.database.username}") private String username = null; @Value("${jdbc.database.password}") private String password = null; @Bean(name = "dataSource2") public DataSource getDataSource() { Properties props = new Properties(); props.setProperty("driver", driver); props.setProperty("url", url); props.setProperty("username", username); props.setProperty("password", password); DataSource dataSource = null; try { dataSource = (DataSource) BasicDataSourceFactory.createDataSource(props); System.out.println("-----dataSource2 init-----"); } catch (Exception e) { e.printStackTrace(); } return dataSource; } }
使用XML方式加載屬性文件
也可以使用XML方式進行加載屬性文件,它只需要使用<con-text:property-placeholder>元素加載一些配置項即可。
代碼清單:通過XML加載屬性文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <context:component-scan base-package="com.ssm.chapter10.annotation"/> <!--<import resourse="spring-datasource.xml"/>--> <!--<context:property-placeholder ignore-resource-not-found="true" location="classpath:ssm/chapter10/database-config.properties"/>--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!--字符串數組,可配置多個屬性文件--> <property name="locations"> <array> <value>classpath:ssm/chapter10/database-config.properties</value> <value>classpath:log4j.properties</value> </array> </property> <property name="ignoreResourceNotFound" value="false"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/springmvc?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> </beans>