运行时值注入
来源:互联网 发布:fileinput.js 参数 编辑:程序博客网 时间:2024/05/20 03:42
注入外部的值
在Spring中,处理外部值的最简单方式就是声明属性源并通过Spring的Environment来检索属性。
使用@PropertySource注解和Environment
程序清单:
package com.lf.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.env.Environment;/** * Created by LF on 2017/5/3. */@Configuration@PropertySource("classpath:application.properties")public class Config { @Autowired private Environment environment; @Bean public String getTitle() { return environment.getProperty("title") + environment.getProperty("context"); }}
title=标题context=内容
测试代码:
package com.lf;import com.lf.config.Config;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Unit test for simple App. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = Config.class)public class AppTest { @Autowired private String string; @Test public void get() { System.err.println("==========title"+string); }}
工程结构
学习Spring的Environment
getProperty()方法有四个重载的变种形式:
String getProperty(String key); String getProperty(String key, String defaultValue); <T> T getProperty(String key, Class<T> targetType); <T> T getProperty(String key, Class<T> targetType, T defaultValue);
前两种形式的getProperty()方法都会返回String类型的值。
第二种在指定属性不存在的时候,会使用一个默认值。
剩下的两种getProperty()方法与前面的两种非常类似,但是它们不会将所有的值都视为String类型。
假设你想要获取的值所代表的含义是连接池中所维持的连接数量。如果我们从属性文件中得到
的是一个String类型的值,那么在使用之前还需要将其转换为Integer类型。但是,如果使用重载形式的getProperty()的话,就能非常便利地解决这个问题。
最后,如果想将属性解析为类的话,可以使用getPropertyAsClass()方法。
除了属性相关的功能以外,Environment还提供了一些方法来检查哪些profile处于激活状态:
- String[] getActiveProfiles():返回激活profile名称的数组;
- String[] getDefaultProfiles():返回默认profile名称的数组;
- boolean acceptsProfiles(String… profiles):如果environment支持给定profile的话,就返回true。
@Value注解
Spring一直支持将属性定义到外部的属性的文件中,并使用占位符值将其插入到Spring bean中。在Spring装配中,占位符的形式为使用“${… }”包装的属性名称。作为样例,我们可以在XML中按照如下的方式解析BlankDisc构造器参数:
为了使用占位符,我们必须要配置一个PropertyPlaceholderConfigurer bean或PropertySourcesPlaceholderConfigurer bean。从Spring3.1开始,推荐使用PropertySourcesPlaceholderConfigurer,因为它能够基于Spring Environment及其属性源来解析占位符。
package com.lf.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;import org.springframework.core.env.Environment;/** * Created by LF on 2017/5/3. */@Configuration@PropertySource("classpath:application.properties")public class Config { @Bean public PropertySourcesPlaceholderConfigurer get() { return new PropertySourcesPlaceholderConfigurer(); }}
package com.lf;import com.lf.config.Config;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Unit test for simple App. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = Config.class)public class AppTest { @Value("${title}") private String string; @Test public void get() { System.err.println("==========title" + string); }}
如果你想使用XML配置的话,Spring context命名空间中的元素将会为你生成PropertySourcesPlaceholderConfigurer bean:
- 运行时值注入
- 运行时值注入Bean
- SpringInAction学习笔记:运行时值注入
- Spring学习笔记(七) --- 运行时值注入
- Spring实战笔记——运行时值注入
- Spring框架_3.5_运行时值注入
- Java 19:Spring 2(Bean运行时值注入:占位符和SpEL表达式 )
- SpringBoot项目使用注入的Service或DAO时值为空
- 运行时注入值
- Spring运行时注入
- delphi远程dll注入运行
- asp 防止SQL注入代码运行
- 在网站中注入【运行】javascript
- 如何修改镜像镜像连接超时值mirroring_connection_timeout
- 学习DDR2时值得一看的资料
- 关于传递参数时值变化的问题
- Java编程时值得注意的地方
- mybatis自动生成主键时值取得
- 使用密钥加密、解密实例
- linux里的jps命令详解(查看java进程)
- 欢迎使用CSDN-markdown编辑器
- C#面试题(曾经面试过三次)
- 浅谈函数调用(二)
- 运行时值注入
- SpringBoot非官方教程 | 第十三篇:springboot集成spring cache
- NYOJ-1236-挑战密室
- 关于Fragment与Fragment、Activity通信的四种方式
- SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列
- @Transactional注解初见
- 简介几种负载均衡原理
- 数据结构二叉树——前中后序深度子节点个数
- linux_011-open.c库文件解析