【Spring】 (3)注入方式读取各种配置
来源:互联网 发布:韦德五次总决赛数据 编辑:程序博客网 时间:2024/06/18 18:28
package com.example.demo_2_2;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;/** * Created by WangBin on 2017/4/10. * */@Servicepublic class DemoService { @Value("注入的普通字符串") private String another; public String getAnother() { return another; } public void setAnother(String another) { this.another = another; }}
package com.example.demo_2_2;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.Bean;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;import java.io.InputStream;/** * Created by WangBin on 2017/4/10. * */@Configuration@ComponentScan("com.example.demo_2_2")@PropertySource("classpath:test.properties")//注入配置文件 【classpath 指的是编译目录的文件 junit 测试类 不再包里生成编译的文件 放入 resource里】public class Elconfig { @Value("我爱你")//注入普通字符串 private String normal; @Value("#{systemProperties['os.name']}")//注入操作系统属性 private String osName; @Value("#{T(java.lang.Math).random()*100.0}")//注入表达式结果 private double randomNumber; @Value("#{demoService.another}")//注入其他bean的属性 private String fromAnother; @Value("classpath:test.txt")//注入文件资源 (使用Value需要 注入下面的bean PropertySourcesPlaceholderConfigurer PropertySource则不用) private Resource testFile; @Value("http://www.baidu.com/")//注入网址资源 private Resource testUrl; @Value("${book.name}")//注入配置文件 需在类上 引入 配置文件【注意这里用的是$】 private String bookName; @Autowired private Environment environment; // 引入的是 类 上注解 @PropertySource getProperty 传入 key 去获取value @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){ return new PropertySourcesPlaceholderConfigurer(); } @Override public String toString() { String str = ""; String urlStr=""; try { InputStream inputStream =testFile.getInputStream(); InputStream inputStream1 = testUrl.getInputStream(); str = IOUtils.toString(inputStream,"UTF-8"); urlStr = IOUtils.toString(inputStream1,"UTF-8"); } catch (IOException e) { e.printStackTrace(); } return "Elconfig{" + "normal='" + normal + '\'' + ", osName='" + osName + '\'' + ", randomNumber=" + randomNumber + ", fromAnother='" + fromAnother + '\'' + ", testFile=" + str + ", testUrl=" + urlStr + ", bookName='" + bookName + '\'' + ", environment='"+environment.getProperty("book.author")+ '}'; }}
package com.example.demo_2_2;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Created by WangBin on 2017/4/11. * */public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Elconfig.class); Elconfig elconfig = context.getBean(Elconfig.class); System.err.println(""+elconfig.toString()); context.close(); }}
test.properties
book.author=wangbin
book.name=spring boot
阅读全文
0 0
- 【Spring】 (3)注入方式读取各种配置
- spring注入方式(3)
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Spring各种属性的注入配置
- spring基础(2)-注入方式和配置集合属性
- Spring-Boot-多种配置注入方式
- Spring三种配置注入方式
- Spring-注入参数详解-[简化配置方式]
- Spring学习(3)Spring注入的三种方式
- Spring 4.0 学习日记(3)--- Spring依赖注入方式
- spring入门(依赖注入方式,生命周期,读取属性文件值,数据库接连)
- spring 多配置加载各种方式
- Spring(三):Spring注入方式
- Spring-02-各种类型信息的注入配置格式
- Spring placeHolder读取文件内容注入参数配置
- Spring中的@Value读取并注入配置信息
- spring 3 bean配置--注入属性配置细节(1)
- c++ j将十进制整数转化为十六进制整数 用栈来实现
- 搜
- CodeForces
- angular购物车查找删除
- cocos2d-x之写自己三国手游(二)
- 【Spring】 (3)注入方式读取各种配置
- unity战棋类游戏移动范围搜索算法设计(未优化)
- HTML之3D动画
- 购物车
- 第1131期AI100_机器学习日报(2017-10-23)
- 二级购物车
- angular的路由表格添加
- 写程序学ML:Logistic回归算法原理及实现(二)
- C/C++如何整行读入字符串?