spring 运行时属性值注入
继续spring学习,今天介绍两种外部属性值注入的方式。当你需要读取配置信息时,可以快速读取。
开始之前先创建属性文件site.properties,放在classpath下面
#数据库配置 ###
database.oracle.platform=org.hibernate.dialect.Oracle9iDialect
database.mysql.platform=org.hibernate.dialect.MySQL5InnoDBDialect
database.sqlserver.platform=org.hibernate.dialect.SQLServerDialect
1、使用@PropertySource注解和org.springframework.core.env.Environment。@PropertySource声明属性源,Environment用于检索属性值。
@Controller
@RequestMapping("/free")
@PropertySource("classpath:site.properties")
public class FreeMarkerController {
@Autowired
Environment env; @RequestMapping(value = "/list", method = RequestMethod.GET)
public String sayHelloAgain(ModelMap model) {
model.addAttribute("greeting", "Freemarker Again, from Spring 4 MVC");
model.addAttribute("mysql",env.getProperty("database.mysql.platform"));
return "welcome2";
}
}


2、使用@Component注解和@Value注解。使用组件自动扫描方式,首先需要在beans配置文件中加载属性文件到spring上下文中,之后用@Value注解标注属性,用于自动组装。
- 加载属性文件到spring上下文中(property-placeholder简捷易用,个人比较喜欢)
<!-- 引入配置文件的方法一 -->
<context:property-placeholder location="classpath:site.properties"/>
<!-- 引入配置文件的方法二 -->
<!--<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:site.properties" />
</bean>-->
- 创建属性组件,将POJO与属性文件关联起来
@Component
public class SiteProperties {
@Value("${database.mysql.platform}")
private String mysql;
@Value("${database.oracle.platform}")
private String oracle;
@Value("${database.sqlserver.platform}")
private String sqlserver; public String getMysql() {
return mysql;
} public String getOracle() {
return oracle;
} public String getSqlserver() {
return sqlserver;
}
}
- 调用试试
@Autowired
SiteProperties site;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String sayHelloAgain(ModelMap model) {
List<ProcessBlock> list = processBlock.findByName("主干流程");
System.out.println("主干流程描述:"+list.size());
//long div =0L;
//long per = 10/div;
model.addAttribute("greeting", "Freemarker Again, from Spring 4 MVC");
model.addAttribute("mysql",env.getProperty("database.mysql.platform"));
model.addAttribute("oracle",site.getOracle());
return "welcome2";
}


本次测试,到此结束。需要说明的是spring的注入还有其他的方式。个人比较喜欢这两种。当然,在这两者之间,我更喜欢第二种方法一些,用POJO方式进行属性管理,代码会更干净些。
spring 运行时属性值注入的更多相关文章
- Spring Boot之配置文件值注入(@ConfigurationProperties)
前言:Spring Boot配置文件值的注入有两种方式,分别是 @ConfigurationProperties @Value 这里我们使用第一种 首先我们创建一个application.yml文件, ...
- 30.怎样在Swift中添加运行时属性?
和OC一样,Swift中也可以添加运行时属性.下面将提供一个完整的例子,演示如何给按钮点击事件添加运行时属性. 1.示例 import UIKit var s_GofButtonTouchDownKe ...
- 1-3 Spring Bean 的属性值设置
详见http://www.cnblogs.com/chenssy/archive/2013/03/17/2964593.html 1.注入普通的属性值 <bean id="Cat&qu ...
- 【原】iOS动态性(三) Method Swizzling以及AOP编程:在运行时进行代码注入
概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...
- Method Swizzling以及AOP编程:在运行时进行代码注入-备用
概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...
- Spring整合JUnit spring静态对象属性的注入
package cn.itcast.d_junit4; import org.junit.Test; import org.junit.runner.RunWith; import org.sprin ...
- Method Swizzling以及AOP编程:在运行时进行代码注入-b
概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...
- spring boot 配置属性值获取注解@Value和@ConfigurationProperties比较
功能比较 : @ConfigurationProperties @Value 映射赋值 批量注入配置文件中的属性 一个个指定 松散绑定(松散语法)① 支持 不支持 SpEL② 不支持 支持 ...
- Spring经常使用属性的注入及属性编辑器
对于对象的注入,我们使用ref方式,能够指定注入的对象.以下看下对于基本类型的注入.以及当spring无法转换基本类型进行注入时,怎样编写一个相似转换器的东西来完毕注入. 一.基本类型的注入 以下写一 ...
随机推荐
- 用jquery怎么删除<table>的一行
摘录网址:用jquery怎么删除<table>的一行 思路:获取<table>的一行,然后使用 remove() 方法删除之.实例演示如下: 1.HTML结构 <tabl ...
- .Net cache与cache更新
主要用到 System.Runtime.Caching 框架自动给了一个默认值 MemoryCache.Default; 查看微软关于MemoryCache的介绍,可以看到它有Add.Get.Set ...
- python_爬百度百科词条
如何爬取? 明确目标:爬取百度百科,定初始百度词条:python,初始URL:http://baike.baidu.com/item/Python,爬取数据量为1000条,值爬取简介,标题,和简介中u ...
- Certificate downloaded from cloudexpress:11443 is invalid
问题描述: CertificateManagement : Server is not trusted.Received fatal alert: handshake_failure. Now ins ...
- java获取昨天的日期
Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); String yesterday = new ...
- JMeter打开jmx文件报错解决方法
错误提示: Error Problem loading XMLfrom:'D:\software\apace-jmeter-3.3\bin\线程组jmx', missing class com.tho ...
- Java中native关键字[转]
原文链接:http://blog.163.com/yueyemaitian@126/blog/static/21475796200701491621267/ 今日在hibernate源代码中遇到了na ...
- XML实体解析器的作用
XML实体解析器的作用 什么是实体解析器 如果一个sax解析器需要实现对外部实体的自定义处理,那么必须实现一个EntityResolver接口并且注册到SAX驱动上. 从这段文字可以看出来,实体解析器 ...
- 用js脚本一键下载网页所有图片
年前这两天稍微闲一点了,琢磨了一点js脚本,功能是把当前网页页面上的所有图片一次性保存到本地,免得每次都要对图片右键保存. 测试环境:Chrome开发者模式下(启动Chrome,按F12即可) 测试网 ...
- Dapper入门教程(二)——执行非查询语句
描述 你可以从任意实现IDbConnection的类对象中调用Dapper的扩展方法"Execute".它能够执行一条命令(Command)一次或者多次,并返回受影响的行数.这个方 ...