Spring 常用注解
组件注解
@Component
- 组件注解,Spring boot 会将指定路径下标注了@Component 注解的类自动加载,默认单例加载,如果要声明对多例,则需要使用@Scope 注解进行定义声明。
@Component
public class UserServiceImpl {
}
多例BEAN
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class PrototypeBean {
// 类的实现
}
- 目标对象是类级别
@Service
是@Component 的别名,作用同@Component
- 声明业务层
@Service
public class UserServiceImpl {
}
@Repository
是@Component 的别名,作用同@Component
- 数据访问层
@Repository
public class UserRepositoryImpl {
}
@Controller
是@Component 的别名,作用同@Component
@Controller
public class UserController {
}
@RestController
REST API 注解。是@Controller 和@ResponseBody 的的组合
- Spring Web
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
* @since 4.0.1
*/
@AliasFor(annotation = Controller.class)
String value() default "";
}
配置相关
@Configuration
声明一个类中的多个@Bean方法可以会被spring 容器处理
public class AppConfig {
@Bean
public MyBean myBean() {
// instantiate, configure and return bean ...
}
}
@Bean
声明一个类会生成一个bean 将被Spring 容器管理
@Bean
public MyBean myBean() {
// instantiate and configure MyBean obj
return obj;
}
@Value
用于方法或者构造器参数,声明一个默认的表达式,可以用于读取外部配置文件
@ConfigurationProperties
与@Configuration 结合使用,绑定外部配置的properties 或者yaml文件
@Configuration
@ConfigurationProperties(prefix = "xxx.xx")
public class ParamsConfig {}
相较于Value 注解,ConfigurationProperties 可以注入复杂类型;比如list 、map 等
配置类
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "complex.config")
public class ComplexConfigProperties {
private List<String> simpleList;
private Map<String, String> simpleMap;
private List<NestedObject> nestedList;
// standard getters and setters
public static class NestedObject {
private String name;
private int value;
// standard getters and setters
}
}
配置文件
complex:
config:
simpleList:
- listValue1
- listValue2
simpleMap:
key1: value1
key2: value2
nestedList:
- name: nestedName1
value: 1
- name: nestedName2
value: 2
注入注解
@Autowried
在Spring Boot 自动装配了上面的类之后,如果想在类中使用其他组件的类,可以使用 @Autowried 注解进行注入
@Qualifier
当定义了一个Service Interface 存在多个实现时,一般都是按照名称进行注入对应的实例,如果需要按照名称指定对应的类的话,就可以使用@Qualifier 注解指定对应名称的Service
功能注解
功能注解通常是基于Spring AOP 进行动态代理实现,所以在使用时要注意代理是否生效。
@Transcational
事务注解,可通过订单指定的异常类进行触发,默认为Throable.class 触发事务回滚
事务隔离级别(4个):
- 读未提交
- 读已提交
- 可重复读
- 串行化
事务传播级别(7个):
- REQUIRED ,当存在事务时则继续使用当前事务,如果当前不存在事务,则创建一个新的事务
- SUPPORTS,当前存在事务则继续使用当前事务,如果不存在事务则不创建
- MANDATORY,支持当前事务,如果当前不存在事务则抛出异常
- REQUIRES_NEW ,不论存在还是不存在事务,都创建一个新的事务
- NOT_SUPPORTED,如果存在事务则暂停当前事务,声明当前方法不在事务内执行
- NEVER,不支持事务,如果当前存在事务则抛出异常
- NESTED
@Retryable 和@Recover
- 需要引入spring-retry
- 并且开启重试配置
重试注解,与事务注解一样,当抛出指定异常时会进行回滚,可以指定重试次数、重试策略(重试时间、每次增加多少秒)
@Retryable(value = Throwable.class, backoff = @Backoff(multiplier = 2))
void pushMessage(){
}
@Aync
异步注解,方法级别的注解,在方法上标注@Aysnc 在正常调用下,方法里面的业务逻辑会异步执行,默认放到spring 的taskExecutor 这个线程池里面执行,线程池可通过配置类统一配置
@Async
void pushMessage(){
}