继续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 运行时属性值注入的更多相关文章

  1. Spring Boot之配置文件值注入(@ConfigurationProperties)

    前言:Spring Boot配置文件值的注入有两种方式,分别是 @ConfigurationProperties @Value 这里我们使用第一种 首先我们创建一个application.yml文件, ...

  2. 30.怎样在Swift中添加运行时属性?

    和OC一样,Swift中也可以添加运行时属性.下面将提供一个完整的例子,演示如何给按钮点击事件添加运行时属性. 1.示例 import UIKit var s_GofButtonTouchDownKe ...

  3. 1-3 Spring Bean 的属性值设置

    详见http://www.cnblogs.com/chenssy/archive/2013/03/17/2964593.html 1.注入普通的属性值 <bean id="Cat&qu ...

  4. 【原】iOS动态性(三) Method Swizzling以及AOP编程:在运行时进行代码注入

    概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...

  5. Method Swizzling以及AOP编程:在运行时进行代码注入-备用

    概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...

  6. Spring整合JUnit spring静态对象属性的注入

    package cn.itcast.d_junit4; import org.junit.Test; import org.junit.runner.RunWith; import org.sprin ...

  7. Method Swizzling以及AOP编程:在运行时进行代码注入-b

    概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...

  8. spring boot 配置属性值获取注解@Value和@ConfigurationProperties比较

    功能比较 :     @ConfigurationProperties  @Value  映射赋值 批量注入配置文件中的属性 一个个指定 松散绑定(松散语法)① 支持 不支持 SpEL② 不支持 支持 ...

  9. Spring经常使用属性的注入及属性编辑器

    对于对象的注入,我们使用ref方式,能够指定注入的对象.以下看下对于基本类型的注入.以及当spring无法转换基本类型进行注入时,怎样编写一个相似转换器的东西来完毕注入. 一.基本类型的注入 以下写一 ...

随机推荐

  1. myeclipse编码

    window --->perferences

  2. windows下搭建virtualenv虚拟环境

    操作系统:windows7 旗舰版 64bit pip install django==1.9.1pip install virtualenv 虚拟环境工具>pip install virtua ...

  3. linux tpm 测试完整记录,亲测有效。

    没有tpm芯片,采用模拟器的方式来测试. 实验环境:内核版本 3.10.0-327 软件包准备: 内网,没有仓库,自己网上下载: 1. cmake-3.9.6-Linux-x86_64.tar.gz ...

  4. 通过EXPLAIN分析低效SQL的执行计划

    explain select * from film where rating>9\G; select_type 表示select的类型 SIMPLE 代表简单表,不用表连接或子查询 PRIMR ...

  5. RocketMQ-顺序消费

    看了https://www.jianshu.com/p/453c6e7ff81c这篇博客,得出顺序消费的结论."要实现严格的顺序消息,简单且可行的办法就是:保证生产者 - MQServer ...

  6. JS题目

    1.请你谈谈Cookie的弊端 cookie虽然在持久保存客户端数据提供了方便,分担了服务器存储的负担,但还是有很多局限性的. 第一:每个特定的域名下最多生成20个cookie 1.IE6或更低版本最 ...

  7. 无废话XML--XML约束(schema)

    Schema  的由来 DTD 作为 XML 1.0 规范的重要组成部分, 对于 XML 文档的结构起到很好的描述作用. 但是,它也具有一些缺点,比如,它采用了非 XML 的语法规则.不支持数据类型. ...

  8. Asp.net core 2.0.1 Razor 的使用学习笔记(四)

    ASP.net core 2.0.1 中 asp.net identity 2.0.1 的基本使用(三)—用户注册 一.修改用户注册 1.打开Pages文件夹>Account>Regist ...

  9. win10预览版无开始菜单解决方案

    1.按下Win+R键打开“运行”程序,键入gpedit.msc 回车以打开本地组策略编辑器 2.调到图示位置将windows设置->安全设置->本地策略->安全选项->“用户账 ...

  10. 6 个 Linux 运维典型问题

    作为一名合格的 Linux 运维工程师,一定要有一套清晰.明确的解决故障思路,当问题出现时,才能迅速定位.解决问题,这里给出一个处理问题的一般思路: 重视报错提示信息:每个错误的出现,都是给出错误提示 ...