#### @SpringBootApplication
用于标注在应用主程序类
```java
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
}
```
#### @RestController
用于标注REST风格的controller,等价于同时标注@Controller和@ResponseBody这两个注解。标注@RestController的controller类中的所有请求映射都是REST风格的。
```java
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "Hello,this is spring-boot";
}
}
等价于
@Controller
@ResponseBody
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "Hello,this is spring-boot";
}
}
```
#### @Configuration
添加@Configuration注解的类会被识别成为一个配置类,在该类中可以使用@Bean为IOC容器添加组件
```java
package com.miantiao.boot.configuration;
import com.miantiao.boot.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author: miantiao
* @date: 2021/8/8
*/
@Configuration
public class MyConfiguration {
//为IOC容器添加一个名为createPerson,类型为Person的组件
@Bean
public Person createPerson(){
return new Person(24, "tom");
}
//为IOC容器添加一个名为Tom,类型为Person的组件
@Bean("Tom")
public Person newPerson(){
return new Person(24, "tom");
}
}
```
在外部调用配置类中的方法获得到的对象都是单例的,如果不想获得单例的对象,可以通过给@Configuration添加proxyBeanMethods属性设置为false来实现
```java
@Configuration(proxyBeanMethods=false)
```
proxyBeanMethods指的是在IOC容器中代理Bean的方式:
- proxyBeanMethods = true:Full模式
- proxyBeanMethods = false: Lite模式
#### @Import
@Import可以自动给容器中创建出多个类型的组件,可以包含外部引用的类型,并且默认组件的名字为全类名。多个类型以数组的形式出现
```java
package org.springframework.context.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
Class<?>[] value();
}
```
例如:
```java
@Import({Person.class, DBHelper.class})
//可以为IOC容器添加Person, DBHelper两个类型的组件,组件的名字分别为:com.miantiao.boot.bean.Person、ch.qos.logback.core.db.DBHelper@5ba745bc
```
#### @Conditional
条件装配注解。当满足Conditional指定的条件,则进行组件注入
```java
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
Class<? extends Condition>[] value();
}
```
@Conditional 有很多派生注解:
- ConditionalOnBean : 当容器中存在指定bean时
- ConditionalOnClass:当容器中存在指定class时
- ConditionaIOnCloudPlatform:
- ConditionalOnExpressionConditionalOnJava
- ConditionalOnJndi
- ConditionalOnMissingBean:当容器中不存在指定bean时
- ConditionaIOnMissingClass : 当容器中没有指定class时
- ConditionalOnNotWebApplication:当应用不是web应用时
- ConditionalOnProperty:当配置文件中有指定属性时
- ConditionalOnResource:当容器中存在指定资源时
- ConditionalOnSingleCandidate:当指定对象时唯一时
- ConditionaIOnWarDeployment:当以war包的形式部署时
- ConditionalOnWebApplication:当应用时web应用时
#### @ImportResource
该注解用于在SpringBoot项目中导入Spring的配置文件。例如导入项目根目录下的名为bean.xml的Spring配置文件。
```java
@ImportResource("classpath:bean.xml")
```
### @ConfigurationProperties
该注解可以自动绑定配置。
```java
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
@AliasFor("prefix")
String value() default "";
@AliasFor("value")
String prefix() default "";
boolean ignoreInvalidFields() default false;
boolean ignoreUnknownFields() default true;
}
```
例如,application.properties配置文件中有:
```xml
miantiao.name=miantiao
miantiao.age=25
maintaio.gender=male
```
同时,有一个Person类中有相应的name、age、gender等属性,这时就可以通过@ConfigurationProperties组合@Component注解来实现属性值的自动绑定。
```java
@Component
@ConfigurationProperties(prefix = "miantiao")
public class Person {
int age;
String name;
String gender;
public Person(){
}
public Person(int age, String name, String genger) {
this.age = age;
this.name = name;
this.gender = genger;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getGenger() {
return gender;
}
public void setGenger(String genger) {
this.gender = genger;
}
}
```
除此之外,@ConfigurationProperties注解还可以和@EnableConfigurationProperties注解结合使用来实现自动绑定配置。
```java
//首先在项目的配置类上添加
@EnableConfigurationProperties(Person.class)
//然后在要自动绑定配置的Person类添加头注解
@ConfigurationProperties(prefix="miantiao")
```
![Spring-boot 注解汇总](https://miantiao.online/upload/2020/2/papyros-wallpaper%201-aa172656ca864f8e902451962580abdd.png)
Spring-boot 注解汇总