Spring Boot 注解(五)@PropertySource

用法:

  1. @PropertySource 注解提供便利和声明的机制添加 PropertySource 到Spring的 Environment 
  2. @PropertySource 和 @Value组合使用,可以将自定义属性文件中的属性变量值注入到当前类的使用@Value注解的成员变量中
  3. @PropertySource 和 @ConfigurationProperties组合使用,可以将属性文件与一个Java类绑定,将属性文件中的变量值注入到该Java类的成员变量中
  4. @EnableConfigurationProperties注解 和 @ConfigurationProperties组合使用

例子:

app.properties

name=tianming
age=18
db.name=root
db.password=admin

示例一:

AppConfig1.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;


@Configuration
@PropertySource("classpath:/app.properties")
public class AppConfig {
	
	@Autowired
	Environment env;
	
}

示例二:

AppConfig2.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = {"/app.properties"})
public class AppConfig2 {
	
	@Value("${name}")
	public String name;
	
	@Value("${age}")
	public Integer age;
	
}

示例三:

AppConfig3.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = {"/app.properties"})
@ConfigurationProperties(prefix = "db")
public class AppConfig3 {

	private String name;
	
	private String password;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}

示例四:

@EnableConfigurationProperties注解的作用是:使 使用 @ConfigurationProperties 注解的类生效。

如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component或者实现了@Component的其他注解,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入。

简单点说@EnableConfigurationProperties的功能类似于@Component。

application.properties

dp.name=yiguang
dp.age=13

StudentConfig.class

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

//@Component 使用@EnableConfigurationProperties替代
@ConfigurationProperties(prefix="dp") // 根据application.properties中的配置填充字段
//@PropertySource("classpath:config.properties") 如果使用此注解和@EnableConfigurationProperties一起使用,不报错,但是填充的字段为null
public class StudentConfig {

    public String name;

    public int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
    

}

@EnableConfigurationProperties在实际项目中的使用场景
如下,在配置类NacosConfigAutoConfiguration的头上加注解@EnableConfigurationProperties(NacosConfigProperties.class),

而在NacosConfigProperties配置类本身并没有实现了@Component相关的注解,也就是说运行项目时,不会直接把NacosConfigProperties配置类注入到Spring 容器中,而是在执行NacosConfigAutoConfiguration这个配置类时才会去把NacosConfigProperties类注入到spring

 如下,NacosConfigProperties类本身并没有@Component相关注解:

例:

Application.class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;

import springboot111.springboot.config.StudentConfig;
 
@SpringBootApplication
@ServletComponentScan
@EnableConfigurationProperties(StudentConfig.class) // 注意此处
public class Application extends SpringBootServletInitializer {
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

结果:

HelloController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import springboot111.springboot.config.AppConfig;
import springboot111.springboot.config.AppConfig2;
import springboot111.springboot.config.AppConfig3;
import springboot111.springboot.config.StudentConfig;
 
@RestController
public class HelloController {
	
	@Autowired
	Environment env;
	
	@Autowired
	AppConfig2 appConfig2;
	
	@Autowired
	AppConfig3 appConfig3;

	@Autowired
	StudentConfig studentConfig;
 
    @RequestMapping("/hello")
    public String hello() {
    	System.out.println(env.getProperty("name")); // tianming
        return "Hello Spring Boot!";
    }
    
    @RequestMapping("/hello2")
    public String hello2() {
    	System.out.println(appConfig2.name);	// tianming
    	System.out.println(appConfig2.age);		// 18
        return "Hello Spring Boot!";
    }
    
    @RequestMapping("/hello3")
    public String hello3() {
    	System.out.println(appConfig3.getName());	// root
    	System.out.println(appConfig3.getPassword());	// admin
        return "Hello Spring Boot!";
    }

    @RequestMapping("/hello4")
    public String hello() {
    	System.out.println(studentConfig.name); // yiguang
        return "Hello Spring Boot!";
    }
 
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
00、尚硅谷_SpringBoot_源码、课件 01、尚硅谷_SpringBoot_入门-课程简介 02、尚硅谷_SpringBoot_入门-Spring Boot简介 03、尚硅谷_SpringBoot_入门-微服务简介 04、尚硅谷_SpringBoot_入门-环境准备 05、尚硅谷_SpringBoot_入门-springboot-helloworld 06、尚硅谷_SpringBoot_入门-HelloWorld细节-场景启动器(starter) 07、尚硅谷_SpringBoot_入门-HelloWorld细节-自动配置 08、尚硅谷_SpringBoot_入门-使用向导快速创建Spring Boot应用 09、尚硅谷_SpringBoot_配置-yaml简介 10、尚硅谷_SpringBoot_配置-yaml语法 11、尚硅谷_SpringBoot_配置-yaml配置文件值获取 12、尚硅谷_SpringBoot_配置-properties配置文件编码问题 13、尚硅谷_SpringBoot_配置-@ConfigurationProperties与@Value区别 14、尚硅谷_SpringBoot_配置-@PropertySource、@ImportResource、@Bean 15、尚硅谷_SpringBoot_配置-配置文件占位符 16、尚硅谷_SpringBoot_配置-Profile多环境支持 17、尚硅谷_SpringBoot_配置-配置文件的加载位置 18、尚硅谷_SpringBoot_配置-外部配置加载顺序 19、尚硅谷_SpringBoot_配置-自动配置原理 20、尚硅谷_SpringBoot_配置-@Conditional&自动配置报告 21、尚硅谷_SpringBoot_日志-日志框架分类和选择 22、尚硅谷_SpringBoot_日志-slf4j使用原理 23、尚硅谷_SpringBoot_日志-其他日志框架统一转换为slf4j 24、尚硅谷_SpringBoot_日志-SpringBoot日志关系 25、尚硅谷_SpringBoot_日志-SpringBoot默认配置 26、尚硅谷_SpringBoot_日志-指定日志文件和日志Profile功能 27、尚硅谷_SpringBoot_日志-切换日志框架 28、尚硅谷_SpringBoot_web开发-简介 29、尚硅谷_SpringBoot_web开发-webjars&静态资源映射规则 30、尚硅谷_SpringBoot_web开发-引入thymeleaf 31、尚硅谷_SpringBoot_web开发-thymeleaf语法 32、尚硅谷_SpringBoot_web开发-SpringMVC自动配置原理 33、尚硅谷_SpringBoot_web开发-扩展与全面接管SpringMVC 34、尚硅谷_SpringBoot_web开发-【实验】-引入资源 35、尚硅谷_SpringBoot_web开发-【实验】-国际化 36、尚硅谷_SpringBoot_web开发-【实验】-登陆&拦截器 37、尚硅谷_SpringBoot_web开发-【实验】-Restful实验要求 38、尚硅谷_SpringBoot_web开发-【实验】-员工列表-公共页抽取 39、尚硅谷_SpringBoot_web开发-【实验】-员工列表-链接高亮&列表完成 40、尚硅谷_SpringBoot_web开发-【实验】-员工添加-来到添加页面 41、尚硅谷_SpringBoot_web开发-【实验】-员工添加-添加完成 42、尚硅谷_SpringBoot_web开发-【实验】-员工修改-重用页面&修改完成 43、尚硅谷_SpringBoot_web开发-【实验】-员工删除-删除完成 44、尚硅谷_SpringBoot_web开发-错误处理原理&定制错误页面 45、尚硅谷_SpringBoot_web开发-定制错误数据 46、尚硅谷_SpringBoot_web开发-嵌入式Servlet容器配置修改 47、尚硅谷_SpringBoot_web开发-注册servlet三大组件 48、尚硅谷_SpringBoot_web开发-切换其他嵌入式Servlet容器 49、尚硅谷_SpringBoot_web开发-嵌入式Servlet容器自动配置原理 50、尚硅谷_SpringBoot_web开发-嵌入式Servlet容器启动原理 51、尚硅谷_SpringBoot_web开发-使用外部Servlet容器&JSP;支持 52、尚硅谷_SpringBoot_web开发-外部Servlet容器启动SpringBoot应用原理 53、尚硅谷_SpringBoot_Docker-简介 54、尚硅谷_SpringBoot_Docker-核心概念 55、尚硅谷_SpringBoot_Docker-linux环境准备 56、尚硅谷_SpringBoot_Docker-docker安装&启动&停止 57、尚硅谷_SpringBoot_Docker-docker镜像操作常用命令 58、尚硅谷_SpringBoot_Docker-docker容器操作常用命令 59、尚硅谷_SpringBoot_Docker-docker安装MySQL 60、尚硅谷_SpringBoot_数据访问-简介 61、尚硅谷_SpringBoot_数据访问-JDBC&自动配置原理 62、尚硅谷_SpringBoot_数据访问-整合Druid&配置数据源监控 63、尚硅谷_SpringBoot_数据访问-整合MyBatis(一)-基础环境搭建 64、尚硅谷_SpringBoot_数据访问-整合MyBatis(二)-注解版MyBatis 65、尚硅谷_SpringBoot_数据访问-整合MyBatis(二)-配置版MyBatis 66、尚硅谷_SpringBoot_数据访问-SpringData JPA简介 67、尚硅谷_SpringBoot_数据访问-整合JPA 68、尚硅谷_SpringBoot_原理-第一步:创建SpringApplication 69、尚硅谷_SpringBoot_原理-第二步:启动应用 70、尚硅谷_SpringBoot_原理-事件监听机制相关测试 71、尚硅谷_SpringBoot_原理-自定义starter 72、尚硅谷_SpringBoot_结束语
学习尚硅谷视频整理的文档 Spring Boot 1 1 Spring Boot入门 4 1.1 简介 4 1.2 微服务(martin fowler发表了一篇文章) 5 1.3 环境约束 7 1.4 第一个Spring Boot项目(jar):HelloWorld 8 1.5 入门案例详解 11 1.5.1 POM文件 11 1.5.2 主程序类,主入口类 12 1.6 使用Spring Initializer向导快速创建Spring Boot 16 2 Spring Boot配置 18 2.1 配置文件 18 2.2 YML语法 19 2.3 YML配置文件值获取 21 2.4 properties配置文件乱码问题 24 2.5 @ConfigurationProperties与@Value的区别 25 2.6 配置@PropertySource、@ImportResource、@Bean 27 2.7 配置文件占位符 30 2.8 Profile多环境支持 31 2.9 配置文件的加载位置 33 2.10 外部配置加载顺序 36 2.11 自动配置原理 37 2.12 @Conditional派生注解 41 3 Spring Boot与日志 42 3.1 日志框架分类和选择 42 3.2 SLF4j使用 43 3.3 其他日志框架统一转换成slf4j+logback 44 3.4 Spring Boot日志使用 45 3.5 Spring Boot默认配置 47 3.6 指定日志文件和日志Profile功能 52 3.7 切换日志框架(不使用SLF4j+LogBack) 54 4 Spring Boot与Web开发 55 4.1 Web开发简介 55 4.2 静态资源映射规则 56 4.3 引入Thymeleaf 60 4.4 Thymeleaf语法 61 4.5 SpringMVC自动配置原理 67 4.6 SpringBoot扩展与全面接管 70 4.7 如何修改SpringBoot的默认配置 72 4.8 【实验】CRUD操作 73 4.8.1 默认访问首页 73 4.8.2 登录页面国际化 74 4.8.3 登录 80 4.8.4 拦截器进行登录检查 81 4.8.5 实验要求(没按要求做,不想改了!) 82 4.8.6 CRUD-员工列表 83 4.8.7 CRUD-员工修改 86 4.8.8 CRUD-员工添加 87 4.8.9 CRUD-员工删除 88 4.9 错误处理原理&错误页面定制 90 4.10 配置嵌入式Servlet容器(springboot 1.50版本) 97 4.10.1 如何定制和修改Servelt容器的相关配置 97 4.10.2 注册servlet三大组件【servlet,filter,listener】 98 4.10.3 替换为其他嵌入式容器 102 4.10.4 嵌入式servlet容器自动配置原理 103 4.10.5 嵌入式servlet容器启动原理 103 4.11 使用外置的Servlet容器 104 4.11.1 步骤 104 4.11.2 原理 107 5 Spring Boot与Docker(虚拟化容器技术) 110 5.1 简介 110 5.2 核心概念 111 5.3 安装Docker 112 5.4 Docker常用命令&操作 113 5.5 安装MySQL示例 114 6 Spring Boot与数据访问 115 6.1 JDBC 115 6.1.1 实现 115 6.1.2 自动配置原理 116 6.2 整合Durid数据源 117 6.3 整合Mybatis 122 6.3.1 注解版 123 6.3.2 配置文件版 124 6.4 整合SpringData JPA 125 6.4.1 SpringData简介 125 6.4.2 整合 126 7 Spring Boot启动配置原理 128 7.1 启动流程(Springboot 1.50版本) 128 7.1.1 创建SpringApplication对象 129 7.1.2 运行run方法 130 7.1.3 编写事件监听机制 132 8 Spring Boot自定义starters 136 8.1 概述 136 8.2 步骤 137 9 更多Springboot整合示例 144 10 Spring Boot与缓存 145 10.1 JSR107缓存规范 145 10.2 Spring的缓存抽象 146 10.2.1 基本概念 146 10.2.2 整合项目 146 10.2.3 CacheEnable注解 148 10.2.4 Cache注解 150 10.3 整合redis 154 10.3.1 在Docker上安装redis 154 10.3.2 Redis的Template 154 10.3.3 整合(百度) 155

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值