1. package com.example.el;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.stereotype.Service;
  5.  
  6. /**
  7. * Created by liz19 on 2017/1/31.
  8. */
  9. @Service
  10. public class DemoService {
  11.  
  12. @Value("其他类属性")
  13. private String another;
  14.  
  15. public String getAnother(){
  16. return another;
  17. }
  18.  
  19. public void setAnother(String another){
  20. this.another=another;
  21. }
  22. }
  1. package com.example.el;
  2.  
  3. import org.apache.commons.io.IOUtils;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.context.annotation.ComponentScan;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.context.annotation.PropertySource;
  9. import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
  10. import org.springframework.core.env.Environment;
  11. import org.springframework.core.io.Resource;
  12.  
  13. import java.io.IOException;
  14.  
  15. /**
  16. * Created by liz19 on 2017/1/31.
  17. */
  18. @Configuration
  19. @ComponentScan("com.example.el")
  20. @PropertySource("classpath:test.properties")
  21. public class ElConfig {
  22.  
  23. //注入普通字符串
  24. @Value("Testing String inject")
  25. private String normal;
  26.  
  27. //注入系统属性
  28. @Value("#{systemProperties['os.name']}")
  29. private String osName;
  30.  
  31. //注入表达式结果
  32. @Value("#{ T(java.lang.Math).random()}")
  33. private double randomNumber;
  34.  
  35. //注入其他bean属性
  36. @Value("#{demoService.another}")
  37. private String fromAnother;
  38.  
  39. //注入文件资源
  40. @Value("classpath:test2.txt")
  41. private Resource testFile;
  42.  
  43. //注入网址资源
  44. @Value("http://www.baidu.com")
  45. private Resource testUrl;
  46.  
  47. //指定Properties文件注入并注入值
  48. @Value("${book.name}")
  49. private String bookName;
  50.  
  51. //PropertySource注入需要指定
  52. @Autowired
  53. private Environment environment;
  54.  
  55. //PropertySource注入需要实现
  56. public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
  57. return new PropertySourcesPlaceholderConfigurer();
  58. }
  59.  
  60. public void outputResource(){
  61.  
  62. try {
  63. System.out.println(normal);
  64. System.out.println(osName);
  65. System.out.println(randomNumber);
  66. System.out.println(fromAnother);
  67. System.out.println(IOUtils.toString(testFile.getInputStream()));
  68. System.out.println(IOUtils.toString(testUrl.getInputStream()));
  69. System.out.println(bookName);
  70. System.out.println(environment.getProperty("book.author"));
  71.  
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75.  
  76. }
  77. }
  1. package com.example;
  2.  
  3. import com.example.el.ElConfig;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5.  
  6. /**
  7. * Created by liz19 on 2017/1/31.
  8. */
  9. public class ElApp {
  10.  
  11. public static void main(String[] args){
  12.  
  13. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
  14. ElConfig resourceService = context.getBean(ElConfig.class);
  15. resourceService.outputResource();
  16. context.close();
  17. }
  18.  
  19. }
  1. ##test.properties
  2. book.name=spring boot test
  3. book.author=test Author
  1. ##test2.txt
  2. this is test.txt content

1.多种配置注入,包括了注入普通字符串,注入系统属性,注入表达式结果,注入其他bean属性,注入文件资源,注入网址资源,指定Properties文件注入并注入值,PropertySource注入需要指定,PropertySource注入需要实现。

2. 根据需求,可将应用配置配置到txt文件中,再已注入的方式注入到Service中,供后期代码使用。

Spring-Boot:多种配置注入方式的更多相关文章

  1. Spring Boot 属性配置和使用

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  2. Spring Boot 属性配置和使用(转)

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  3. 玩转Spring Boot 自定义配置、导入XML配置与外部化配置

    玩转Spring Boot 自定义配置.导入XML配置与外部化配置       在这里我会全面介绍在Spring Boot里面如何自定义配置,更改Spring Boot默认的配置,以及介绍各配置的优先 ...

  4. spring六种种依赖注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  5. spring 四种依赖注入方式以及注解注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  6. spring中对象的注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  7. Sping Boot入门到实战之入门篇(四):Spring Boot自动化配置

    该篇为Sping Boot入门到实战系列入门篇的第四篇.介绍Spring Boot自动化配置的基本原理与实现.   Spring Boot之所以受开发者欢迎, 其中最重要的一个因素就是其自动化配置特性 ...

  8. Spring Boot 属性配置&自定义属性配置

    在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...

  9. Spring Boot Security配置教程

    1.简介 在本文中,我们将了解Spring Boot对spring Security的支持. 简而言之,我们将专注于默认Security配置以及如何在需要时禁用或自定义它. 2.默认Security设 ...

随机推荐

  1. c++中利用宏定义简化for循环使用

    话不多说,上方法 #define _for(i,a,b) for( int i=(a); i<(b); ++i) #define _rep(i,a,b) for( int i=(a); i< ...

  2. File文件类

    目录 File文件类 File类的构造方法 File类的创建功能 File类的重命名 File类的删除功能 File类的判断功能 File类的获取功能 文件名称过滤器 File文件类 File:文件和 ...

  3. 我是这样一步步理解--主题模型(Topic Model)、LDA

    1. LDA模型是什么 LDA可以分为以下5个步骤: 一个函数:gamma函数. 四个分布:二项分布.多项分布.beta分布.Dirichlet分布. 一个概念和一个理念:共轭先验和贝叶斯框架. 两个 ...

  4. python基础——元组(tuple)

    Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. tuple1 = () tuple2 = ...

  5. spring与actionMQ整合

    出处:http://www.cnblogs.com/leiOOlei/p/5075402.html 一.配置部分 ActiveMQ的安装这就不说了,很简单, 这个例子采用maven构建,首先看一下po ...

  6. jmeter Linux环境执行总报错 cannot allocate memory

    1.windows环境写好的测试用例,执行没有问题,在Linux环境跑总是报错,提示如下 cannot allocate memory 2.一开始以为是哪块设置有问题,因为脚本里边有设置邮件自动发送, ...

  7. IDEA 控制台输出日志无法grep

    不知从何时开始,我的IDEA控制台无法直接使用Grep插件来过滤输出日志了,这个插件真的挺好用的,不知道是升级后造成的还是我自己设置错误,反正在控制台右键无法打开grep来过滤: 在我开发过程中需要这 ...

  8. Git 从master拉取代码创建新分支

    从master拉取新分支并push到远端 开发过程中经常用到从master分支copy一个开发分支: 1.切换到被copy的分支(master),并且从远端拉取最新版本 $git checkout m ...

  9. JVM系列(1)- JVM常见参数及堆内存分配

    常见参数配置 基于JDK1.6 -XX:+PrintGC 每次触发GC的时候打印相关日志 -XX:+UseSerialGC 串行回收模式 -XX:+PrintGCDetails 打印更详细的GC日志 ...

  10. git基本命令学习(一)

    1 git配置文件 1.1 git权限控制 git有三个不同的权限控制文件,高优先权的设置会覆盖低优先权的设置项,以下按照优先权从高到低介绍: 文件夹中".git" 子文件夹中的c ...