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

引用 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;
}

【Spring MVC】Properties文件的加载的更多相关文章

  1. spring mvc easyui tree 异步加载树

    使用spring mvc 注解 异步加载一棵树 jsp: <ul id="orgInfoTree"></ul> $(function(){ loadOrgT ...

  2. ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别

    一:ContextLoaderListener加载内容 二:DispatcherServlt加载内容 ContextLoaderListener和DispatcherServlet都会在Web容器启动 ...

  3. 3.Properties文件的加载和使用

    一.Properties简介 Properties 类继承自HashTable,提供的方法很像Map的实现类HashMap.它在 Java 编程的早期就有了,并且几乎没有什么变化.J2SE 的 Tig ...

  4. ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别【转】

    原文地址:https://blog.csdn.net/py_xin/article/details/52052627 ContextLoaderListener和DispatcherServlet都会 ...

  5. properties文件的加载方式

    下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...

  6. Java读取Properties文件 Java加载配置Properties文件

    static{ Properties prop = new Properties(); prop.load(Thread.currentThread().getContextClassLoader() ...

  7. Spring中 <context:property-placeholder 的使用与解析 .properties 配置文件的加载

    转: Spring中property-placeholder的使用与解析 Spring中property-placeholder的使用与解析 我们在基于spring开发应用的时候,一般都会将数据库的配 ...

  8. Spring Framework框架解析(1)- 从图书馆示例来看xml文件的加载过程

    引言 这个系列是我阅读Spring源码后的一个总结,会从Spring Framework框架的整体结构进行分析,不会先入为主的讲解IOC或者AOP的原理,如果读者有使用Spring的经验再好不过.鉴于 ...

  9. Django---MTV和MVC的了解,Django的模版语言变量和逻辑,常见的模板语言过滤器,自定义过滤器,CSRF了解,Django的母版(继承extends,块block,组件include,静态文件的加载load static),自定义simple_tag和inclusion_tag

    Django---MTV和MVC的了解,Django的模版语言变量和逻辑,常见的模板语言过滤器,自定义过滤器,CSRF了解,Django的母版(继承extends,块block,组件include,静 ...

随机推荐

  1. nyoj 455-黑色帽子

    455-黑色帽子 内存限制:64MB 时间限制:1000ms 特判: No 通过数:4 提交数:7 难度:1 题目描述:         最近发现了一个搞笑的游戏,不过目前还没玩过.一个舞会上,每个人 ...

  2. 小白学 Python 爬虫(2):前置准备(一)基本类库的安装

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 本篇内容较长,各位同学可以先收藏后再看~~ 在开始讲爬虫之前,还是先把环境搞搞好,工欲善其事必先利其器嘛~~~ 本篇 ...

  3. bash:双引号和单引号

    单引号.双引号都能引用字符和字符串 单引号:'$i'仅仅是字符,没有变量的意思了 双以号:变量等能表示出来

  4. 🙈羞,Spring Bean 初始化/销毁竟然有这么多姿势

    文章来源:http://1t.click/bfHN 一.前言 日常开发过程有时需要在应用启动之后加载某些资源,或者在应用关闭之前释放资源.Spring 框架提供相关功能,围绕 Spring Bean ...

  5. ansible on aws linux 2

    1. 安装epel yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 2. 安 ...

  6. 提高PHP性能效率的几个技巧!

    如何提高效率问题,往往同样的功能,不一样的代码,出来的效率往往大不一样. ● 用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有ec ...

  7. Python 编程语言要掌握的技能之一:使用数字与字符串的技巧

    最佳实践 1. 少写数字字面量 “数字字面量(integer literal)” 是指那些直接出现在代码里的数字.它们分布在代码里的各个角落,比如代码 del users[0] 里的 0 就是一个数字 ...

  8. 10-kubernetes serveraccount RBAC

    目录 认证安全 serviceAccountName 和 userAccount serviceaccount 创建 使用admin 的SA 测试 URL访问kubernetes资源 APIserve ...

  9. e = e || window.event的区别及用法

    本文链接:https://blog.csdn.net/qq_41348029/article/details/81288481 e = e || window.event 在做事件处理时,用于区分IE ...

  10. $("#loginname").tips和jQuery中 的ajax

    jquery tips 提示插件 jquery.tips.js v0.1beta: 使用方法 $("#loginname").tips({ //#loginname为jquery的 ...