【Spring MVC】Properties文件的加载

【Spring MVC】Properties文件的加载

转载:https://www.cnblogs.com/yangchongxing/p/10726885.html

参考:https://javadoop.com/post/spring-properties?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io

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 不能单独使用

通过配置@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;
}

猜你喜欢

转载自www.cnblogs.com/yangchongxing/p/10726885.html