SpringBoot鸡汤(注解集合)
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鸡汤(注解集合)的更多相关文章
- SpringBoot鸡汤(注解集合二)
1.@NotNull :属性值不为空 2.@Profiles @Configuration @Profile("production") public class Producti ...
- Spring SpringMVC SpringBoot SpringCloud 注解整理大全
Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...
- 接近8000字的Spring/SpringBoot常用注解总结!安排!
0.前言 大家好,我是 Guide 哥!这是我的 221 篇优质原创文章.如需转载,请在文首注明地址,蟹蟹! 本文已经收录进我的 75K Star 的 Java 开源项目 JavaGuide:http ...
- Spring/SpringBoot常用注解总结
转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...
- SpringBoot常见注解
0.前言 这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使用 SpringBoot 来开发项目基本没 ...
- 菜鸟的springboot常用注解总结
菜鸟的springboot常用注解总结 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用 ...
- SpringBoot 常用注解(持续更新)
SpringBoot 常用注解 @SpringBootApplication @Bean @ComponentScan @ControllerAdvice @ExceptionHandler @Res ...
- 常见的springmvc、SpringBoot的注解
springMvc的常用注解 : @Controller :用于标记在一个类上,使用它标记的类就是一个springmcv Controller对象,分发处理器将会扫描使用了该注解 的类的方法,并检测该 ...
- 浅谈SpringBoot核心注解原理
SpringBoot核心注解原理 今天跟大家来探讨下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot为什么不需要XML,达到零配置 ...
随机推荐
- 转 这种方法可以免去自己计算大文件md5 的麻烦
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;using ...
- 远程连接MongoDB报“Network is unreachable”错误的解决方法
解决办法:/etc/mongod.conf 里把127.0.0.1 改成 0.0.0.0
- pyqt 不规则形状窗口显示
#coding=utf- import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QWidget, QApplicatio ...
- python 移动文件夹
xxx@ddd:~$ mkdir testa testb >>> import shutil >>> shutil.move("/home/xxx/tes ...
- SQL 中常用的功能函数,自定义的功能行数
在SQL Server指定的数据库中,有Programmability目录,在这个目录下,有存储过程,有功能函数. set ANSI_NULLS ON set QUOTED_IDENTIFIER ON ...
- C++.构造函数(超出范围)_01
环境:Win7x64.Qt5.3.2 MSVC2010 OpenGL.vs2010 1.ZC:在 构造函数 中,基类访问子类的成员 会报内存错误,如果访问的是 基本类型的话(如int) 可能还不会出错 ...
- Promise的.then .catch
定义一个promise 调用promise 如果promise的状态为resolve 则 执行 .then 否则执行.catch 可以有多个.then 会按顺序执行 axios.post 可 ...
- [calss*="col-"]匹配类名中包含col-的类名,^以col-开头,$以col-结尾
[class*= col-] 代表包含 col- 的类名 , 例 col-md-4 ,demo-col-2(这个是虚构的)等都可以匹配到. [class^=col-] 代表 以 col- 开头 ...
- leecode第五十四题(螺旋矩阵)
class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) ...
- SublimeText3按ctrl+b执行python无反应
现象:在Sublime中打开.py文件,按”ctrl+b”执行时无反应.点击工具->编译系统中已经有且识别到Python,但执行”run(ctrl+shift+b)”时无反应,Sublime左下 ...