【Spring】 (3)注入方式读取各种配置
2017-10-25 08:10
309 查看
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. * */ @Service public 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
相关文章推荐
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Spring读取配置文件,获取bean的几种方式
- Spring各种属性的注入配置
- spring的配置文件中bean的注入方式
- Spring——容器,注入(配置和注解两种方式)
- 实习总结(十)---Spring的下载/安装/优点/依赖注入方式/自动配置
- Spring-boot中读取config配置文件的两种方式
- spring1 配置方式注入
- 【Spring学习05】四种xml配置注入方式
- Spring事务Transaction配置的五种注入方式详解
- Spring事务Transaction配置的五种注入方式详解
- spring基于xml加载和读取properties配置文件的方式
- Spring-AOP配置切入点方式及配置各种类型增强
- 模拟spring的注入和读取配置文件(ApplicationContext.xml)
- Spring事务Transaction配置的五种注入方式详解
- 01_spring基本命名空间及各种类型注入方式
- Spring中的@Value读取并注入配置信息
- Spring中读取bean配置文件的几种方式