【Spring MVC】Properties文件的加載
轉載:https://www.cnblogs.com/yangchongxing/p/10726885.html
1、通過 @Value 注入使用,適用於注入單個屬性值
@Value("${jdbc.driverClass}") private String driver;
2、使用 @PropertySource 聲明屬性元並通過 Environment 獲取屬性值
聲明屬性源
@PropertySource("classpath:/jdbc.properties")
@PropertySource({"classpath:/jdbc.properties","classpath:/path.properties"})
注入 Environment
@Autowired
Environment env;
獲取屬性值
env.getProperty("jdbc.driverClass")
若是 Spring Boot 的 application.properties 文件,會自動注冊不用聲明 @PropertySource,Environment 可以直接取到值
3、使用 @ConfigurationProperties 注解,這是 spring boot 才有
注意:如果配置文件不在默認的 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()
4、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; }