学习Spring boot 注解 @Value @Component @ConfigurationProperties
2017-09-14 16:38
921 查看
1、@Value
从当前的配置文件中读取参数,格式为
会自动进行类型转换。
2、@Component
只有加了这个注解的类才可以被@Autowired使用。
3、@ConfigurationProperties
从当前的配置文件中读取一整个集合类型。如,当前配置文件中有一个集合配置为
然后编写一个类如Dog,注意所有的属性都要添加get和set方法
为类配置@ConfigurationProperties注解,参数prefix为配置文件中集合的前缀,再为其加上@Component注解,就可以在其它类中引入它了,如下
4、@RestController 和 @RequestMapping
顺带介绍一下springboot中控制类的注解为@RestController,它相当于@Controller加@ResponseBody。
控制类中的方法注解为@RequestMapping,格式如下,多个请求名用逗号隔开
还有简略写法@GetRequestMapping 和 @PostRequestMapping 相当于把后面的method省略掉了。
从当前的配置文件中读取参数,格式为
@Value("${TypeName}") private Type typeName;
会自动进行类型转换。
2、@Component
只有加了这个注解的类才可以被@Autowired使用。
3、@ConfigurationProperties
从当前的配置文件中读取一整个集合类型。如,当前配置文件中有一个集合配置为
dog: age: 10 type: "哈士奇" favorite: "拆家"
然后编写一个类如Dog,注意所有的属性都要添加get和set方法
@Component @ConfigurationProperties(prefix = "dog") public class Dog { private int age; private String type; private String favorite;
为类配置@ConfigurationProperties注解,参数prefix为配置文件中集合的前缀,再为其加上@Component注解,就可以在其它类中引入它了,如下
@Autowired private Dog dog;
4、@RestController 和 @RequestMapping
顺带介绍一下springboot中控制类的注解为@RestController,它相当于@Controller加@ResponseBody。
控制类中的方法注解为@RequestMapping,格式如下,多个请求名用逗号隔开
@RequestMapping(value = {"/dog"} , method = RequestMethod.GET)
还有简略写法@GetRequestMapping 和 @PostRequestMapping 相当于把后面的method省略掉了。
相关文章推荐
- springboot @value和@configurationproperties注解的区别
- spring boot 学习使用@ConfigurationProperties注解
- Spring Boot第四弹:Spring Boot之@Value和@ConfigurationProperties
- spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案 cannot resolve method location
- spring boot @ConfigurationProperties @EnableConfigurationProperties 配置注解使用
- 在Spring Boot中使用 @ConfigurationProperties 注解
- spring boot:使用@ConfigurationProperties注解加载配置文件
- 在Spring Boot中使用 @ConfigurationProperties 注解
- Spring Boot1.4版本后@ConfigurationProperties注解舍弃location参数后的解决方式
- 【spring Boot】spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案
- spring boot 通过@Value,@ConfigurationProperties获取配置
- Spring Boot 使用@ConfigurationProperties注解获取配置文件中的值
- SpringBoot学习之配置文件注入@ConfigurationProperties
- Spring Boot核心原理-自动配置 以及@ConfigurationProperties 注解
- 在Spring Boot中使用 @ConfigurationProperties 注解
- spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案
- Spring之@Value 注解获取properties值(Spring 3的@value简化配置文件的读取)
- SpringBoot缓存注解学习笔记之Redis
- Spring Boot中@ConfigurationProperties的使用
- 【Spring】学习@Value注解