【Spring MVC】Properties文件的加載html
轉載:http://www.javashuo.com/article/p-xtjknanx-bw.htmljava
一、經過 @Value 注入使用,適用於注入單個屬性值spring
@Value("${jdbc.driverClass}") private String driver;
二、使用 @PropertySource 聲明屬性元並經過 Environment 獲取屬性值sql
聲明屬性源app
@PropertySource("classpath:/jdbc.properties")
@PropertySource({"classpath:/jdbc.properties","classpath:/path.properties"})
注入 Environmentoop
@Autowired Environment env;
獲取屬性值post
env.getProperty("jdbc.driverClass")
如果 Spring Boot 的 application.properties 文件,會自動註冊不用聲明 @PropertySource,Environment 能夠直接取到值spa
三、使用 @ConfigurationProperties 註解,這是 spring boot 纔有code
注意:若是配置文件不在默認的 application.properties 文件,則使用 @PropertySource("classpath:/jdbc.properties") 引入,
另外 @ConfigurationProperties 必須配合 @Configuration 或者 @Bean 才能使用,不能單獨使用
引用 Spring Boot 實戰:從技術上將 @ConfigurationProperties 註解不會生效,除非先向 Spring Boot 自動配置類添加 @EnableConfigurationProperties 註解,但一般無需這麼作,由於 Spring Boot 自動配置後面的所有配置類都已經加上了 @EnableConfigurationProperties 註解,因此不須要顯示的添加 @EnableConfigurationProperties 註解
經過配置@Configuration使用
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @Data @Configuration @ConfigurationProperties(prefix="jdbc") public class JdbcConfig { private String driverClass; private String jdbcUrl; private String user; private String password; }
經過配置@Bean使用
import lombok.Data; @Data public class JdbcConfig { private String driverClass; private String jdbcUrl; private String user; private String password; } @Bean @ConfigurationProperties(prefix="jdbc") public JdbcConfig jdbcConfig() { return new JdbcConfig(); }
其餘地方注入對象使用
@Autowired JdbcConfig jdbc; jdbc.getDriverClass()
四、XML 中使用佔位符 ${} 引用值,爲了使用佔位符必須配置一個 PropertyPlaceholderConfigurer 或者 PropertySourcesPlaceholderConfigurer
<!-- 導入資源文件 --> <context:property-placeholder location="classpath:mysql.properties"/> <!-- 配置數據源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <property name="initialPoolSize" value="${jdbc.initialPoolSize}" /> <property name="minPoolSize" value="${jdbc.minPoolSize}" /> <property name="maxPoolSize" value="${jdbc.maxPoolSize}" /> <property name="maxIdleTime" value="${jdbc.maxIdleTime}" /> <property name="testConnectionOnCheckout" value="${jdbc.testConnectionOnCheckout}" /> <property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}" /> <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" /> </bean>
Spring3.1 以前 PropertyPlaceholderConfigurer
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> </bean>
@Bean public PropertyPlaceholderConfigurer propertiess() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[]{new ClassPathResource("jdbc.properties")}; ppc.setLocations(resources); ppc.setIgnoreUnresolvablePlaceholders(true); return ppc; }
Spring3.1後 PropertySourcesPlaceholderConfigurer
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> </bean>
@Bean public PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[]{new ClassPathResource("sys.properties")}; pspc.setLocations(resources); pspc.setIgnoreUnresolvablePlaceholders(true); return pspc; }