使用@Value和@PropertySource注入外部资源

1简介

在Spring Boot进行项目开发的过程中,肯定会有这样一种场景,比如说事件上报,在开发时开发人员可能会模拟在代码中写入一个事件上报Url,然后当部署到生产环境时,该url就需要从外部导入,一般通过修改配置文件的方式达到类似的目的。
在Spring开发中经常涉及调用各种资源的情况,包含普通文件,网址,配置文件,系统环境变量等,这种情况可以使用Spring EL-Spring表达式语言实现资源的注入。
在这里插入图片描述

2实践

程序演示使用IDEA集成开发环境,演示@Value的使用,并通过注解@PropertySource可以注入自定义配置文件或者其他任意新建的文本文件。
注意:以下实践在Spring环境下是通用的,Spring Boot也是可用的的。

2.1项目结构

在这里插入图片描述

2.2 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>value</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>value</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

其中

<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
        </dependency>

依赖可以简化文件相关的操作,本例中使用commons-io将file转换成字符串。

2.3 DemoService

package com.example.value.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 需被注入的类
 *
 * @Owner:
 * @Time: 2019/3/31-12:35
 */
@Service
public class DemoService {
    @Value("其他类的属性")
    private String another;

    public String getAnother() {
        return another;
    }

    public void setAnother(String another) {
        this.another = another;
    }
}

其中在上类中使用@Value注解注入了普通字符串“其他类的属性。”

2.4 ElConfig 类

package com.example.value.config;

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;


/**
 * 演示@Value的使用
 *
 * @Owner:
 * @Time: 2019/3/31-12:32
 */
@Configuration
@ComponentScan("com.example.value.service") //扫包
@PropertySource("classpath:test.properties") //注意文件格式的指定
public class ElConfig {
    @Value("I Love You!") //1 注入普通字符串
    private String normal;

    @Value("#{systemProperties['os.name']}") //2 注入操作系统属性
    private String osName;

    @Value("#{ T(java.lang.Math).random() * 100.0 }") //3 注入表达式结果
    private double randomNumber;

    @Value("#{demoService.another}") //4  注入其他Bean的属性
    private String fromAnother;

    @Value("classpath:test.txt") //5  注入了文件资源
    private Resource testFile;

    @Value("http://www.baidu.com") //6   注入网页资源
    private Resource testUrl;

    @Value("${book.name}") //7   注入classpath:test.properties中资源项,注意美元符号$
    private String bookName;

    @Autowired
    private Environment environment; //7 属性也可以从environment中获取。

    @Bean //7
    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"));
            System.out.println(environment.getProperty("book.school"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

注意:注入配置配件使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcePlaceholderConfigure的Bean

2.5 ValueApplication主类

package com.example.value;

import com.example.value.config.ElConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@SpringBootApplication
public class ValueApplication {

    @Autowired
    private ElConfig config;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
        ElConfig resourceService = context.getBean(ElConfig.class);

        resourceService.outputResource();
    }

}

2.6 resources下两个文件的内容

2.6.1 test.txt

123334455
aaa
bbb
ccc

2.6.2 test.properties

book.author=sqh
book.name=spring boot
book.school=NJUST

3控制台打印结果

I Love You!   #直接注入字符串
Windows 8.1   #注入了系统属性
87.12913167952843   # 注入了表达式结果
其他类的属性   # 其他类的属性
123334455    # test.txt
aaa
bbb
ccc
<!DOCTYPE html>   # 注入了网页内容
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

spring boot  # 配置文件中的三个值
sqh
NJUST

4 总结

通过上述的方式,也附带的阐述了在Spring Boot中不使用application.properties配置文件,而是用其他任意的配置文件来放入程序配置的使用方法,可以看到@Value很灵活,也很方便,作者暂时比较常用的就是在applicaiton.properties中放入类似book.name这样的配置项而使用@Value("${book.name}")直接注入的方式。本文简单阐述的主要目标即是@Value,希望能有所帮助。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值