前言

===========================================

初步接触Spring Boot

===========================================

资源文件中自定义属性值,配置在一个对象bean上,然后在程序中可以使用bean的属性值。

 一。

二。

@Component
标志本类为一个bean
@PropertySource(value = "classpath:/application.properties")
指定绑定哪个资源文件,【如果要绑定自定义的资源文件中的值的话,是可以用上的】这里的application.properties文件是springboot默认的资源文件,是可以不用指定的,这里绑定的话,会去加载绑定两次。
@ConfigurationProperties(prefix = "com.sxd")
指定绑定资源文件中前缀以com.sxd开头的属性名,其他的不会绑定过来。因为这里location属性取消了,所以采用上面注解进行替代方案

package com.sxd.beans;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Component
@PropertySource(value = "classpath:/application.properties")
@ConfigurationProperties(prefix = "com.sxd")
public class ConfigBean { private String name;
private String want; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getWant() {
return want;
} public void setWant(String want) {
this.want = want;
} }

三。

@EnableConfigurationProperties(ConfigBean.class)
激活绑定的bean
@Autowired
将绑定的某个bean自动注入

package com.sxd.firstdemo;

import com.sxd.beans.ConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@SpringBootApplication
@EnableConfigurationProperties(ConfigBean.class)
public class FirstdemoApplication { @Autowired
ConfigBean configBean; @RequestMapping("/")
public String index(){ return "Hello Spring Boot,"+configBean.getName();
}
public static void main(String[] args) {
SpringApplication.run(FirstdemoApplication.class, args);
}
}

 四。

运行结果:

【spring Boot】spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案的更多相关文章

  1. spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案 cannot resolve method location

    问题 在spring boot(版本1.5.1.RELEASE)项目中,当准备映射自定义的配置文件属性到类中的时候,发现原本的@ConfigurationProperties注解已将location属 ...

  2. Spring Boot 2.2.2.RELEASE 版本中文参考文档【3.2 - 3.10】

    Spring Boot 2.2.2.RELEASE版本中文文档持续更新中~如有需要获取参考文档文件,关注公众号JavaSo,回复“参考文档”即可. 3.2 结构化代码 Spring Boot不需要任何 ...

  3. Spring Boot 2.2.2.RELEASE 版本中文参考文档【3.1】

    使用Spring Boot 本节将详细介绍如何使用Spring Boot.它涵盖了诸如构建系统,自动配置以及如何运行应用程序之类的主题.我们还将介绍一些Spring Boot最佳实践.尽管Spring ...

  4. Spring Boot 2.2.2.RELEASE 版本中文参考文档

    写在前面 在我初次接触MongoDB的时候,是为了做一个监控系统和日志分析系统.当时在用Java操作MongoDB数据里的数据的时候,都是在网上查找Demo示例然后完成的功能,相信大家也同样的体会,网 ...

  5. spring Boot+spring Cloud实现微服务详细教程第二篇

    上一篇文章已经说明了一下,关于spring boot创建maven项目的简单步骤,相信很多熟悉Maven+Eclipse作为开发常用工具的朋友们都一目了然,这篇文章主要讲解一下,构建spring bo ...

  6. 255.Spring Boot+Spring Security:使用md5加密

    说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)h ...

  7. 256.Spring Boot+Spring Security: MD5是加密算法吗?

    说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)h ...

  8. Spring Boot+Spring Security:获取用户信息和session并发控制

    说明 (1)JDK版本:1.8(2)Spring Boot 2.0.6(3)Spring Security 5.0.9(4)Spring Data JPA 2.0.11.RELEASE(5)hiber ...

  9. Spring Boot -- Spring Boot之@Async异步调用、Mybatis、事务管理等

    这一节将在上一节的基础上,继续深入学习Spring Boot相关知识,其中主要包括@Async异步调用,@Value自定义参数.Mybatis.事务管理等. 本节所使用的代码是在上一节项目代码中,继续 ...

随机推荐

  1. Linux设备驱动程序 之 顺序锁

    当要保护的资源很小,很简单,会频繁的被访问而且写入访问很少的且必须快速时(即读不允许让写饥饿),就可以使用顺序锁(seqlock):从本质上讲,顺序锁会允许读取者对资源的自由访问,但需要读取者检查是否 ...

  2. linux 搜索文件夹下的所有文件内容

    find . -type f -name "*.*" | xargs grep "博客园"

  3. react-hook的简单的动画插件react-simple-animate(其实是react插件,但是这里只介绍react-hook的简单用法)

    1.useAnimate(普通anima动画的形式) (1)js const animate = useAnimate({ complete: { display: 'none' }, //动画完成的 ...

  4. ubuntu如何删除刚添加的源?

    答: sudo add-apt-repository -r <source_url> 如: sudo add-apt-repository -r ppa:linaro-maintainer ...

  5. android Api操作SQLite数据库的示例代码

    import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.databa ...

  6. Linux shell利用sed如何批量更改文件名详解[转载]

     需求背景: 目录A用来存放自动化的包生成的apk文件,现在开发修改了包名的 命名规则:环境名称(pro|uat)-release-日期-v版本号.apk 原来的是思路是通过正则表达式匹配新的包名,但 ...

  7. WPF学习笔记 - 数据绑定(在代码中)

    在程序代码里,有两种设置绑定的方法,一种是调用FrameworkElement或FrameContentElement对象的SetBinding实例方法. 例如: Public MainWindow( ...

  8. centos虚拟机存储扩容

    在vSphere Web Client上面创建的虚拟机,用了一段时间后存储无法满足需求,需要将原来的存储300G扩容到500G 点此编辑即可修改磁盘2的储存大小,但是修改此配置后,虚拟机centos是 ...

  9. 【转】MAC 配置ssh免密登录

    配了挺多次免密登录,老是忘了,备份一下 转载自知乎,如侵删.https://zhuanlan.zhihu.com/p/32279976 -------------------------------- ...

  10. C++typedef的详细用法

    转自知乎的一段解释: 作者:知乎用户链接:https://www.zhihu.com/question/29798061/answer/144423125来源:知乎著作权归作者所有.商业转载请联系作者 ...