【Spring MVC】Properties文件的加载

【Spring MVC】Properties文件的加载html

转载:http://www.javashuo.com/article/p-xtjknanx-bw.htmljava

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

一、经过 @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; }
相关文章
相关标签/搜索