您的位置:首页 > 编程语言 > Java开发

【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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring