【Spring】 (3)注入方式讀取各種配置

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




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章