前言

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

初步接触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. IntelliJ IDEA 2017 上传本地项目至码云

    码云免费还挺好用,和Git类似.首先需要注册一个码云.这个就不介绍了. 点击加号,新建一个项目. 填写项目名称.  复制一下你的项目地址.  如图勾选.将项目至于git管理. 首先commit.快捷按 ...

  2. Android跨进程通信AIDL服务

    服务(Service)是android系统中非常重要的组件.Service可以脱离应用程序运行.也就是说,应用程序只起到一个启动Service的作用.一但Service被启动,就算应用程序关闭,Ser ...

  3. ubuntu下如何使用apt-get安装arm64的交叉编译工具链?

    答: sudo apt-get install gcc-aarch64-linux-gnu -y

  4. leetcode25 K 个一组翻转链表

    这道题关于链表的操作,中间指针操作略复杂. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

  5. DisplayUtils

    import android.app.Activity; import android.content.Context; import android.graphics.Rect; import an ...

  6. Android之SharedPreference存储数据

    *路径: /data/data/包名/shared_prefs/ * 以Xml文件存储数据 * 编写步骤 // 1. 获取sp SharedPreferences sp = this.getShare ...

  7. Apple全系列缓冲区溢出内核RCE(CVE-2018-4407)poc

    # CVE-2018-4407 ICMP DOS # https://lgtm.com/blog/apple_xnu_icmp_error_CVE-2018-4407 # from https://t ...

  8. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-2.使用Mybatis注解开发视频列表增删改查

    笔记 2.使用Mybatis注解开发视频列表增删改查     讲解:使用Mybatis3.x注解方式 增删改查实操, 控制台打印sql语句              1.控制台打印sql语句      ...

  9. Scala语法01 - 基础语法

  10. Linux (Ubuntu)安装nexus,搭建maven私有服务器

     下载 nexus oos(开源版)https://www.sonatype.com/download-oss-sonatype 2 将下载的压缩包上传到/opt/nexus/文件夹下面 3 解压文件 ...