1.(ConfigBean.java :是一个带有属性的bean类)

@Configuration
@ConfigurationProperties(prefix = “com.md”)
@PropertySource(“classpath:test.properties”)
public class ConfigTestBean {
private String name;
private String want;
// 省略getter和setter
}

有时候属性太多了,一个个绑定到属性字段上太累,官方提倡绑定一个对象的bean,这里我们建一个ConfigBean.java类,顶部需要使用注解:
@ConfigurationProperties(prefix = “com.dudu”):来指明使用哪个

添加@Configuration和@PropertySource(“classpath:test.properties”)后才可以读取test.properties文件里面的属性值。
@PropertySource:标注的属性源

2.Chapter2Application.java:是一个带有main()方法的类,用于启动应用程序(关键)。

@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class Chapter2Application {
public static void main(String[] args) {
SpringApplication.run(Chapter2Application.class, args);
}
}

@EnableConfigurationProperties:并指明要加载哪个bean,如果不写ConfigBean.class,在bean类那边添加
@SpringBootApplication:是Sprnig Boot项目的核心注解,主要目的是开启自动配置。
@RestController:注解等价于@Controller+@ResponseBody的结合,使用这个注解的类里面的方法都以json格式输出。

3.Controller类

@RestController
public class UserController {
@Autowired
ConfigBean configBean;
@RequestMapping("/")
public String hexo(){
return configBean.getName()+configBean.getWant();
}
}

@RequestMapping :注解提供路由信息。它告诉Spring任何来自”/”路径的HTTP请求都应该被映射到 home 方法。
@RestController: 注解告诉Spring以字符串的形式渲染结果,并直接返回给调用者json格式数据。
@ImportResource :注解加载XML配置文件。
@Configuration:注解该类,等价 与XML中配置beans;
@Bean:标注方法等价于XML中配置bean
@Import: 注解可以用来导入其他配置类

@ComponentScan: 注解自动收集所有的Spring组件,包括 @Configuration 类。
例子:
@ComponentScan(basePackages = “com.hyxt”,includeFilters = {@ComponentScan.Filter(Aspect.class)})

SpringBoot鸡汤(注解集合)的更多相关文章

  1. SpringBoot鸡汤(注解集合二)

    1.@NotNull :属性值不为空 2.@Profiles @Configuration @Profile("production") public class Producti ...

  2. Spring SpringMVC SpringBoot SpringCloud 注解整理大全

    Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...

  3. 接近8000字的Spring/SpringBoot常用注解总结!安排!

    0.前言 大家好,我是 Guide 哥!这是我的 221 篇优质原创文章.如需转载,请在文首注明地址,蟹蟹! 本文已经收录进我的 75K Star 的 Java 开源项目 JavaGuide:http ...

  4. Spring/SpringBoot常用注解总结

    转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...

  5. SpringBoot常见注解

    0.前言 这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使用 SpringBoot 来开发项目基本没 ...

  6. 菜鸟的springboot常用注解总结

    菜鸟的springboot常用注解总结 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用 ...

  7. SpringBoot 常用注解(持续更新)

    SpringBoot 常用注解 @SpringBootApplication @Bean @ComponentScan @ControllerAdvice @ExceptionHandler @Res ...

  8. 常见的springmvc、SpringBoot的注解

    springMvc的常用注解 : @Controller :用于标记在一个类上,使用它标记的类就是一个springmcv Controller对象,分发处理器将会扫描使用了该注解 的类的方法,并检测该 ...

  9. 浅谈SpringBoot核心注解原理

    SpringBoot核心注解原理 今天跟大家来探讨下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot为什么不需要XML,达到零配置 ...

随机推荐

  1. CPU核数和load average的关系

    在前面的文章<Linux系统监控——top命令>中我简单提到了,判断load average的数值到底大不大的判断依据,就是数值除以CPU核数,大于5,就说明超负荷运转了.——这里其实不太 ...

  2. 1.1.1 vue-cli脚手架工具

    参考文档: windows下npm安装vue(以下教程大部分都是参考这篇博客的,按照着这篇博客自己实现了一遍) npm安装vue-cli脚手架 一.前言 npm:nodejs下的包管理器,安装好nod ...

  3. 正则获取 某段 DIV 中 的内容

    string html = "<div class='aa'><div class='left'>324324<div>dsfsdf</div> ...

  4. 学习笔记19—dpabi错误集

    1.回归斜边量的时候千万不要用红色标记的地方,而要用紫色标记的地方

  5. 学习笔记2—MATLAB的copyfile技巧

    clearclc %一.新建文件夹,%二.将原始路径下的数据剪切到新建文件夹中 path = ('E:\DFC_DMN_ASD_DATA_res\Cluster_hcc\4,6,8\Cluster_6 ...

  6. npm install 报错ERR! 404 Not Found: event-stream@3.3.6

    在win下开发的node工程,在linux下用dockerfile部署时,遇到npm install时报错 Step / : RUN npm install ---> Running in 2e ...

  7. Codeforces 36B - Fractal

    36B - Fractal 思路:分形 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #def ...

  8. 雷林鹏分享:XML 应用程序

    XML 应用程序 本章演示一些基于 XML, HTML, XML DOM 和 JavaScript 构建的小型 XML 应用程序. XML 文档实例 在本应用程序中,我们将使用 "cd_ca ...

  9. Lab 3-2

    Analyze the malware found in the file Lab03-02.dll using basic dynamic analysis tools. Questions and ...

  10. 20171022xlVBA练手提取入所记录

    Sub GetWordText改进() Dim Wb As Workbook Dim Sht As Worksheet Dim Rng As Range Dim wdApp As Object Dim ...