Spring注解大全
-
@Scope 设置Bean的作用域
-
@Lazy 懒加载,容器启动不创建,第一次使用的时候才会创建对象,并初始化
-
@Conditional 按条件注册Bean,比如根据不同的操作系统来创建Bean
-
@Value 赋值,引入配置文件的值 ${}。可以和@PropertySource配合
-
@ConfigurationProperties:读取配置信息并与Bean绑定
-
@PropertySource:读取指定的properties文件
-
@PropertySource 引入配置文件@PropertySource(value = {“classpath:person.properties”})
-
@ComponentScan 组件扫描,配合@Controller/@Service/@Reponitory/@Component注解,给容器注册Bean
-
@Component: 通用的组件,可标注任意类为Spring组件
-
@Service:对应服务层
-
@Repository:对应持久层
-
@Controller:对应SpringMVC的控制层
-
@Configuration 表明这个类是配置类,相当于一个xml配置文件,配置@Bean注解,给容器注册Bean
-
@Qualifier 如果注入的时候多个同类型的Bean,可以使用@Qualifier()指定注入哪一个Bean
-
@Primary 注入的时候可以优先使用这个Bean装配
-
@Autowired 自动注入,支持使用@Resource(JSR250) @Inject(JSR330) Java规范
- 默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class)
- 如果找到多个相同的类型的组件,再将属性的名称作为组件的id去容器中查找
- @Qualifier(“bookDao”):使用@Qualifier指定需要装配的组件的的名称,而不是使用属性名
-
@Profile 动态的激活和切换不同的环境
- 命令行参数 -Dspring.profiles.active=test\dev\prod
- 使用代码的方式激活
@Configuration public class MainConfigOfProfile { @Profile("dev") @Bean public DataSource devDataSource() throws PropertyVetoException { ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("123456"); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/myshop"); return comboPooledDataSource; } @Profile("prod") @Bean public DataSource prodDataSource() throws PropertyVetoException { ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("123456"); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/myshop"); return comboPooledDataSource; } }
-
@Import 快速给容器中的导入一个组件
- @Import(要导入到容器中的组件), 容器中就会自动注册这个组件,id默认是全类名
@Configuration @Import(value = {Test01.class, Test02.class}) public class MainConfig { @Bean(value = "person") public Person person01(){ return new Person("zhangsan", 23); } }
- ImportSelector接口: 返回需要导入的组件的全类名
// 返回需要导入的组件 public class MyImportSelector implements ImportSelector { /** * AnnotationMetadata 当前标注@Import注解类的所有注解信息 * * @param importingClassMetadata * @return */ @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { // 不要返回null值,会报空指针异常 return new String[]{"com.aloneness.bean.Test03", "com.aloneness.bean.Test04"}; } } @Configuration @Import(value = {Test01.class, Test02.class, MyImportSelector.class}) public class MainConfig { @Bean(value = "person") public Person person01(){ return new Person("zhangsan", 23); } }
- ImportBeanDefinitionRegistrar
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { /** * 把所有需要添加到容器中的bean, 调用BeanDefinitionRegistry.registerBeanDefinition() 手工注册进来 * * @param importingClassMetadata AnnotationMetadata 当前类的注册信息 * @param registry BeanDefinitionRegistry BeanDefinition的注册类 */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { boolean definition = registry.containsBeanDefinition("com.aloneness.bean.Test03"); boolean definition1 = registry.containsBeanDefinition("com.aloneness.bean.Test04"); if(definition && definition1){ RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Test05.class); registry.registerBeanDefinition("test05", rootBeanDefinition); } } } @Configuration @Import(value = {Test01.class, Test02.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class}) public class MainConfig { @Bean(value = "person") public Person person01(){ return new Person("zhangsan", 23); } }
SpringBoot注解大全
- @SpringBootApplication:SpringBoot项目的基石,包括@EnableAutoConfiguration、@ComponentScan和@Configuration
- @EnableAutoConfiguration:启用SpringBoot自动配置机制
- @RestController: @Controller和@ResponseBody的合集,返回的JSON数据,而不是视图
- @PathVariable:获取路径参数
- @RequestParam:获取查询参数
- @RequestBody:用于读取Request请求的body部分并且Content-Type为application/json格式的数据,接收到数据后会自动映射到Java实体类上,系统会使用HttpMessageConverter或者自定义的HttpMessageConverter将请求的body中的json字符串转换为Java对象。
- @ActiveProfiles:一般作用于测试类上,用于声明Spring配置文件
异常处理
- @ControllerAdvice:注解定义全局处理Controller层异常
- @ExceptionHandler:注解声明异常处理方法
参数校验注解
注解使用的是Hibernate Validator框架,所有的注解,推荐使用 JSR 注解,即javax.validation.constraints,而不是org.hibernate.validator.constraints
- @Valid 参数校验注解
- @NotEmpty 被注释的字符串的不能为 null 也不能为空
- @NotBlank 被注释的字符串非 null,并且必须包含一个非空白字符
- @Null 被注释的元素必须为 null
- @NotNull 被注释的元素必须不为 null
- @AssertTrue 被注释的元素必须为 true
- @AssertFalse 被注释的元素必须为 false
- @Pattern(regex=,flag=)被注释的元素必须符合指定的正则表达式
- @Email 被注释的元素必须是 Email 格式。
- @Min(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值
- @Max(value)被注释的元素必须是一个数字,其值必须小于等于指定的最大值
- @DecimalMin(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值
- @DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
- @Size(max=, min=)被注释的元素的大小必须在指定的范围内
- @Digits (integer, fraction)被注释的元素必须是一个数字,其值必须在可接受的范围内
- @Past被注释的元素必须是一个过去的日期
- @Future 被注释的元素必须是一个将来的日期
Json数据处理
- @JsonIgnoreProperties:作用于类上,用于过滤掉特定的字段不返回或者不解析
- @JsonIgnore:一般作用于类的属性上,作用和@JsonIgnoreProperties一样
- @JsonFormat:格式化Json数据
- @JsonUnwrapped:扁平化对象,将类的对象属性直接解析到第一层