1.@ProtertySource

@PropertySouce是spring3.1开始引入的基于java config的注解。

通过@PropertySource注解将properties配置文件中的值存储到Spring的 Environment中,Environment接口提供方法去读取配置文件中的值,参数是properties文件中定义的key值。

2. 例子

比如有一个配置文件config.properties

jdbc.driver = oracle.jdbc.driver.OracleDriver

jdbc.url = jdbc\:oracle\:thin\:@(DESCRIPTION\=(ADDRESS\=(PROTOCOL\=TCP)(HOST\=10.221.129.208)(PORT\=1523))(CONNECT_DATA\=(SERVICE_NAME\=otatransuser)))

jdbc.username= sassy

jdbc.password = password

2.1  用法1- @PropertySource和@Value

创建java配置类

@Configuration
@PropertySource("classpath:jdbc.properties")
public class PropertiesWithJavaConfig {

@Value(${jdbc.driver})
private String driver;
@Value(${jdbc.url})
private String url;
@Value(${jdbc.username})
private String username;
@Value(${jdbc.password})
private String password;

//要想使用@Value 用${}占位符注入属性,这个bean是必须的,这个就是占位bean,另一种方式是不用value直接用Envirment变量直接getProperty('key')  
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}

2.2 用法2-通过Environment设置

@Configuration
@PropertySource("classpath:jdbc.properties")
public class PropertiesWithJavaConfig { @Autowired
private Environment env; }

接着就可以用env.getProperty("jdbc.driver")得到相应的属性值

3.等同于在xml中配置properties文件

?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:property-placeholder location="classpath:jdbc.properties" /> </beans>

在Spring 4中,Spring提供了一个新的注解——@PropertySources,从名字就可以猜测到它是为多配置文件而准备的。

@PropertySources({

@PropertySource("classpath:config.properties"),

@PropertySource("classpath:db.properties")

})

public class AppConfig {

//something

}

@PropertySouce注解的更多相关文章

  1. @PropertySouce注解 读取 properties文件

    https://www.cnblogs.com/whx7762/p/7885735.html 1.@ProtertySource @PropertySouce是spring3.1开始引入的基于java ...

  2. 基于spring注解AOP的异常处理

    一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...

  3. [Spring]IoC容器之进击的注解

    先啰嗦两句: 第一次在博客园使用markdown编辑,感觉渲染样式差强人意,还是github的样式比较顺眼. 概述 Spring2.5 引入了注解. 于是,一个问题产生了:使用注解方式注入 JavaB ...

  4. Android注解使用之通过annotationProcessor注解生成代码实现自己的ButterKnife框架

    前言: Annotation注解在Android的开发中的使用越来越普遍,例如EventBus.ButterKnife.Dagger2等,之前使用注解的时候需要利用反射机制势必影响到运行效率及性能,直 ...

  5. Android注解使用之注解编译android-apt如何切换到annotationProcessor

    前言: 自从EventBus 3.x发布之后其通过注解预编译的方式解决了之前通过反射机制所引起的性能效率问题,其中注解预编译所采用的的就是android-apt的方式,不过最近Apt工具的作者宣布了不 ...

  6. spring注解源码分析--how does autowired works?

    1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...

  7. 编写高质量代码:改善Java程序的151个建议(第6章:枚举和注解___建议88~92)

    建议88:用枚举实现工厂方法模式更简洁 工厂方法模式(Factory Method Pattern)是" 创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其它子类" ...

  8. ASP.NET MVC5----常见的数据注解和验证

    只要一直走,慢点又何妨. 在使用MVC模式进行开发时,数据注解是经常使用的(模型之上操作),下面是我看书整理的一些常见的用法. 什么是验证,数据注解 验证 从全局来看,发现逻辑仅是整个验证的很小的一部 ...

  9. 基于注解的bean配置

    基于注解的bean配置,主要是进行applicationContext.xml配置.DAO层类注解.Service层类注解. 1.在applicationContext.xml文件中配置信息如下 &l ...

随机推荐

  1. Linux下IP的存储位置

    在linux下,配置多个IP的话,通常是eth0... eth0. ..eth0.x等等, 那么如果要配置大量的IP呢,这么配置也是可以的,但是繁琐,虽说这种情况很少. 对于添加大量IP,有一定规定的 ...

  2. 四:(之一和之二) docker架构和底层技术分析(C/S架构)

    1.架构和底层技术 Docker Host提供了RESTUL api,使docker client可以通过这些命令调用dockerd. Registry是一个公用的存储镜像的容器,类似于github. ...

  3. Invalid MEX-file: caffe.mexa64 的解决方案

    http://blog.csdn.net/iamzhangzhuping/article/details/53105708

  4. ubuntu下修改文件权限

    参考:https://www.cnblogs.com/viviwind/archive/2012/08/02/2619451.html 常用方法如下: sudo chmod 600 ××× (只有所有 ...

  5. 2.13 C++拷贝构造函数

    参考:http://www.weixueyuan.net/view/6344.html 总结: 如果拷贝构造函数的参数不是对象的引用,则是不允许的.如 book(book b); 是无法编译通过的. ...

  6. 手打struts知识点

    Struts2概论 1.MVC原理 MVC(Model-View-Controller),程序设计理念 视图不用多说,html.jsp等 控制器,中转站,分配各个组件应当做什么,接受参数并跳转其他处理 ...

  7. socket-重叠模型(overlap)

    socket-重叠模型(overlap) 重叠模型的基本设计原理便是让应用程序使用一个重叠的数据结构,一次投递一个或多个Winsock I/O请求.针对那些提交的请求,在它们完成之后,应用程序可为它们 ...

  8. 自定义ListView 、GradView 重写onMeasure方法让其正确显示

    1 继承原始的 2 重写onMeasure方法 @Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpe ...

  9. mybatis 异常 There is no getter for property named 'bizId' in 'class java.lang.Long'

    mybatis 异常 There is no getter for property named 'bizId' in 'class java.lang.Long' 当使用mybatis进行传参的时候 ...

  10. TensorFlow函数:tf.zeros_like

    tf.zeros_like函数 tf.zeros_like( tensor, dtype=None, name=None, optimize=True ) 定义在:tensorflow/python/ ...