zoukankan      html  css  js  c++  java
  • Spring-Boot:多种配置注入方式

    package com.example.el;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    
    /**
     * Created by liz19 on 2017/1/31.
     */
    @Service
    public class DemoService {
    
        @Value("其他类属性")
        private String another;
    
        public String getAnother(){
            return another;
        }
    
        public void setAnother(String another){
            this.another=another;
        }
    }
    package com.example.el;
    
    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.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;
    
    /**
     * Created by liz19 on 2017/1/31.
     */
    @Configuration
    @ComponentScan("com.example.el")
    @PropertySource("classpath:test.properties")
    public class ElConfig {
    
    
        //注入普通字符串
        @Value("Testing String inject")
        private String normal;
    
        //注入系统属性
        @Value("#{systemProperties['os.name']}")
        private String osName;
    
        //注入表达式结果
        @Value("#{ T(java.lang.Math).random()}")
        private double randomNumber;
    
        //注入其他bean属性
        @Value("#{demoService.another}")
        private String fromAnother;
    
        //注入文件资源
        @Value("classpath:test2.txt")
        private Resource testFile;
    
        //注入网址资源
        @Value("http://www.baidu.com")
        private Resource testUrl;
    
        //指定Properties文件注入并注入值
        @Value("${book.name}")
        private String bookName;
    
        //PropertySource注入需要指定
        @Autowired
        private Environment environment;
    
        //PropertySource注入需要实现
        public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
            return new PropertySourcesPlaceholderConfigurer();
        }
    
        public void outputResource(){
    
    
            try {
                System.out.println(normal);
                System.out.println(osName);
                System.out.println(randomNumber);
                System.out.println(fromAnother);
                System.out.println(IOUtils.toString(testFile.getInputStream()));
                System.out.println(IOUtils.toString(testUrl.getInputStream()));
                System.out.println(bookName);
                System.out.println(environment.getProperty("book.author"));
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    package com.example;
    
    import com.example.el.ElConfig;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    /**
     * Created by liz19 on 2017/1/31.
     */
    public class ElApp {
    
    
    
        public static void main(String[] args){
    
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
            ElConfig resourceService = context.getBean(ElConfig.class);
            resourceService.outputResource();
            context.close();
        }
    
    }
    ##test.properties
    book.name=spring boot test
    book.author=test Author
    ##test2.txt
    this is test.txt content

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

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

     

  • 相关阅读:
    Python学习杂记_2_格式化字符串的一些操作
    Python学习杂记_1_PyCharm使用的一些收获
    autolayout sizeclass 资料集锦
    据说这个是获得当前的控制器方法,没试过
    Mac下搭建php开发环境【转】
    搜索栏会消失 uisearchbar 狂点消失的问题解决
    mac下XAMPP服务器配置多站点配置局域网配置 (转)
    在 Xcode 6 中使用矢量图( iPhone 6 置配 UI)
    收到远程通知,怎么区分是点击通知栏提醒进去的还是在foreground收到的通知?
    开发经验之状态机思想,分别使用了swift,OC,C,PHP语言实现
  • 原文地址:https://www.cnblogs.com/MarchThree/p/6359634.html
Copyright © 2011-2022 走看看