1、@Value
从当前的配置文件中读取参数,格式为
@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省略掉了。