Spring-Boot:多种配置注入方式
package com.example.el; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; /**
* Created by liz19 on 2017/1/31.
*/
@Service
public class DemoService { @Value("其他类属性")
private String another; public String getAnother(){
return another;
} public void setAnother(String another){
this.another=another;
}
}
package com.example.el; import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource; import java.io.IOException; /**
* Created by liz19 on 2017/1/31.
*/
@Configuration
@ComponentScan("com.example.el")
@PropertySource("classpath:test.properties")
public class ElConfig { //注入普通字符串
@Value("Testing String inject")
private String normal; //注入系统属性
@Value("#{systemProperties['os.name']}")
private String osName; //注入表达式结果
@Value("#{ T(java.lang.Math).random()}")
private double randomNumber; //注入其他bean属性
@Value("#{demoService.another}")
private String fromAnother; //注入文件资源
@Value("classpath:test2.txt")
private Resource testFile; //注入网址资源
@Value("http://www.baidu.com")
private Resource testUrl; //指定Properties文件注入并注入值
@Value("${book.name}")
private String bookName; //PropertySource注入需要指定
@Autowired
private Environment environment; //PropertySource注入需要实现
public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
return new PropertySourcesPlaceholderConfigurer();
} public void outputResource(){ try {
System.out.println(normal);
System.out.println(osName);
System.out.println(randomNumber);
System.out.println(fromAnother);
System.out.println(IOUtils.toString(testFile.getInputStream()));
System.out.println(IOUtils.toString(testUrl.getInputStream()));
System.out.println(bookName);
System.out.println(environment.getProperty("book.author")); } catch (IOException e) {
e.printStackTrace();
} }
}
package com.example; import com.example.el.ElConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* Created by liz19 on 2017/1/31.
*/
public class ElApp { public static void main(String[] args){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
ElConfig resourceService = context.getBean(ElConfig.class);
resourceService.outputResource();
context.close();
} }
##test.properties
book.name=spring boot test
book.author=test Author
##test2.txt
this is test.txt content
1.多种配置注入,包括了注入普通字符串,注入系统属性,注入表达式结果,注入其他bean属性,注入文件资源,注入网址资源,指定Properties文件注入并注入值,PropertySource注入需要指定,PropertySource注入需要实现。
2. 根据需求,可将应用配置配置到txt文件中,再已注入的方式注入到Service中,供后期代码使用。
Spring-Boot:多种配置注入方式的更多相关文章
- Spring Boot 属性配置和使用
Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...
- Spring Boot 属性配置和使用(转)
Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...
- 玩转Spring Boot 自定义配置、导入XML配置与外部化配置
玩转Spring Boot 自定义配置.导入XML配置与外部化配置 在这里我会全面介绍在Spring Boot里面如何自定义配置,更改Spring Boot默认的配置,以及介绍各配置的优先 ...
- spring六种种依赖注入方式
平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...
- spring 四种依赖注入方式以及注解注入方式
平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...
- spring中对象的注入方式
平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...
- Sping Boot入门到实战之入门篇(四):Spring Boot自动化配置
该篇为Sping Boot入门到实战系列入门篇的第四篇.介绍Spring Boot自动化配置的基本原理与实现. Spring Boot之所以受开发者欢迎, 其中最重要的一个因素就是其自动化配置特性 ...
- Spring Boot 属性配置&自定义属性配置
在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...
- Spring Boot Security配置教程
1.简介 在本文中,我们将了解Spring Boot对spring Security的支持. 简而言之,我们将专注于默认Security配置以及如何在需要时禁用或自定义它. 2.默认Security设 ...
随机推荐
- [leetcode] 135. Candy (hard)
原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...
- python字符编码-文件操作
字符编码 字符编码历史及发展 为什么有字符编码 ''' 原因:人们想要将数据存入计算机 计算机的能存储的信息都是二进制的数据 内存是基于电工作的,而电信号只有高低频两种,就用01来表示高低电频,所以计 ...
- Java_Map接口
Map接口 1.1 Map接口概述 我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它们存储数据的形式不同,如下图. Collection中的集合,元素是孤立存在 ...
- git pull 出现non-fast-forward的错误
1.git pull origin daily_liu_0909:liu_0909 出现non-fast-forward的错误,证明您的本地库跟远程库的提交记录不一致,即 你的本地库版本需要更新2.g ...
- three出现状态200的报错
报错代码如下: {status: 200, data: {…}} "sa_SpringUserFindOneView_findPbUserByUserId"header.vue?1 ...
- 关于java飞机躲炮弹的一些对象说明(带源码)
1.飞机躲炮弹的各种实体类都需要一个画笔将他们画出来 (GameObject) import java.awt.*; public void drawSelf(Graphics g){ g.drawI ...
- jboss反序列化漏洞复现(CVE-2017-7504)
jboss反序列化漏洞复现(CVE-2017-7504) 一.漏洞描述 Jboss AS 4.x及之前版本中,JbossMQ实现过程的JMS over HTTP Invocation Layer的HT ...
- jenkins下使用python虚拟环境
jenkins下使用python虚拟环境碰到的一些坑: 1. 构建使用window批处理 - 坑1 c: cd c:\xxxxx\xxxxx\scripts activate c: cd c:\xxx ...
- Python基础总结之第十天开始【认识一下python的另一个数据对象-----字典】(新手可相互督促)
看了大家的评论,还是有意外的收货.感谢每个小伙伴的评论与补充. 众人拾柴火焰高~ 今天的笔记是记录python中的数据对象----字典! 前面有讲到list列表和tuple元组的笔记,他们都是一样可以 ...
- Python基础总结之第十一天开始【再深入一下函数,重新认识一下】(新手可相互督促)
感谢最近大家的关注,希望我的学习笔记对大家有帮助!也感谢各位的评论和推荐,请多多指教. 在重新认识函数之前,我们先看两个函数.一个是我们在前面笔记经常用到的print() :另一个是input() ...