Spring&SpringBoot常用注解
文章目录
- @RequestMapping
- @GetMapping
- @PostMapping
- @PathVariable
- @RequestParam
- @Valid
- @ExceptionHandler(value = Exception.class)
- @SpringBootApplication
- @Configuration
- @Bean
- @ComponentScan & @ComponentScans
- @Conditional
- @Import
- @DependsOn
- @EnableTransactionManagement
关键类
- ClassPathXmlApplicationContext
- AnnotationConfigurationApplicationContext
组件类
@Component
@Controller
@RestController
- @Controller
- @ResponseBody
@Service
@Repository
依赖注入类
@Autowired
@Autowired 是Spring的注解。可以标注在构造器,参数,方法(setter、@Bean(可略,默认从容器中拿)),属性。通过 byType 的方式去注入,仅有一个required属性,默认为true,默认装配规则如下:
1.先通过类型查找,找到只有一个则装配,找不到则抛出异常,如果找到多个,则将field的名称作为组件的id去容器中查找,找到则装配,找不到则抛出异常。
2.当required为false时,最终找不到则忽略不装配。
@Qualifier
@Qualifier是Spring的注解。字面含义为合格者,按名称注入。用于装配时有多个候选bean时通过指定一个标识来解决冲突,仅有一个默认value属性,作为标识,定义bean时通过添加@Qualifier(“xxx”)指定。每一个bean都有一个自身名称标识的Qualifier。
一般当出现两个及以上bean时,不知道要注入哪个,作为@Autowired()的修饰用。
1.当value不指定时,这个标识为"",而不是bean的名称或者作用的属性名称。
2.当通过类型找到一个时,但是不匹配Qualifier指定标识,也会抛出异常(它不仅解决冲突,甚至有点矫枉过正)
@Primary
指定Bean为首选装配组件,当找到多个Bean时,首选Bean被自动装配。
@Resource(JSR250)
@Resource 是 J2EE 的注解。以通过 byName 和 byType 的方式注入。不支持@Primary。
默认先按 byName的方式进行匹配,如果匹配不到,再按 byType的方式进行匹配。用与资源(通常是JNDI)的注入。 Spring对其进行了支持。
装配规则:
1.如果指定了name属性,则按照name查找,找到且类型匹配则装配,否则配抛出异常
2.如果指定了name和type属性,则按照name和type精确查找,找到则装配,找不到抛出异常
3.如果仅指定了type属性,则按照type查找,如果只找到一个则装配,否则抛出异常
4.如果name和type都没有指定,则默认按照作用的属性名字查找,找到则装配,找不到则按照类型找,找到一个则装配,找不到或者找到多个抛出异常
注意 : 如果不指定name 和 type。先按 byName (其name为 注入的自定义的名称,首字母大写的变为小写,后面为驼峰命名规则)。 如果匹配不到,再按 byType的方式进行匹配。
@Inject(JSR330)
需要导入javax.inject包,和@Autowried功能一样,但不支持required属性。
@Named(JSR330)
需要导入javax.inject包,和@Qualifier功能一样。
SpringMVC
@RequestMapping
@GetMapping
组合注解,相当于 @RequestMapping(method = RequestMethod.GET)
@PostMapping
组合注解,相当于 @RequestMapping(method = RequestMethod.POST)
@PathVariable
获取url中的数据
@RequestParam
获取请求参数的值
@Valid
表单验证在Controller层加上@valid,后紧跟BindingResult,通过
BingdingResult.getFieId.getDefaultMessage()获取错误信息,表单验证注解
@ExceptionHandler(value = Exception.class)
捕获异常 (用于统一异常处理)
@Scope
- singleton:单实例,在容器启动的时候创建对象,容器关闭时销毁对象。 - prototype:多实例,在每次使用的时候创建对象,容器关闭时容器并不负责销毁,必须手动销毁。
@Lazy
@PostConstruct(JSR250)
@PreDestroy(JSR250)
xxxAware接口
实现Aware接口,可以注入Spring底层组件(ApplicationContext,BeanFactory等)到自定义组件中(通过后置处理器注入),同时Aware实现方法可以被Spring回调。
@Profile
- 标注在@Bean方法上。
- 标注在配置类@Configuration上。
- 没有标注环境标识的bean在任何环境下都是加载的。
- 激活方式: 命令行方式激活:-Dspring.profiles.active=dev
- 代码方式激活
// 创建ApplicationContext AnnotationConfigApplicationContext applicationContext = new AnnocationConfigApplicationContext(); // 设置激活环境 applicationContext.getEnvironment().setActiveProfiles("test","dev"); // 注册主配置类 applicationContext.register(MainConfigProfile.class); // 启动刷新容器 applicationContext.refresh();
@PropertySource & @PropertySources
读取外部配置文件,保存在运行环境变量中,等效于xml文件的<context:property-placeholder>
- value:数组,可以一次指定多个配置文件
@ConfigurationProperties
@Value
- 基本数值
- SpEL,#{}
- @PropertySource,${},取出配置文件中的值(在运行环境变量里的值),此方式可以标注在属性上,也可以标注在方法参数上。
- 实现EmbeddedValueResolverAware接口的setEmbeddedValueResolver方法获取到StringValueResolver,然后在获取到属性值。
Spring AOP
@EnableAspectJAutoProxy
- 开启基于注解的AOP模式
- 等效于xml的<aop:aspectj-autoproxy>
- 给容器中导入类:@Import(AspectJAutoProxyRegistrar.class),利用AspectJAutoProxyRegistrar给容器中注入AnnotationAwareAspectJAutoProxyCreator。
注意:EnableXXX的功能是在容器中注册组件,主要关注注册的是什么组件,组件什么时候工作,组件有什么功能?
@Aspect
- 定义切面类
@Pointcut
- 定义切入点表达式
- 切入点表达式可以用在类内部,或者用在外部类(需要指定全名)
SpringBoot
@SpringBootApplication
组合注解
- @SpringBootConfiguration:表示Application作为配置文件存在
- @EnableAutoConfiguration:表示启用SpringBoot内置的自动配置功能
- @ComponentScan : 扫描bean,路径为Application类所在package以及package下的子路径,在spring boot中bean都放置在该路径以及子路径下。
@Configuration
- 本质是@Component
- 等效于applicationContext.xml配置文件
- 如果在创建IoC容器时不是指定扫描包的方式,而是直接指定配置类的方式,则此注解也可以不标注。
@Configuration public class AppConfig(){ @Bean ... }
@Bean
等效于xml文件的<bean>
- @Bean value:指定bean id
- 生命周期 构造方法
- initMethod,等效方法是实现InitializingBean接口,或者在init方法上直接使用注解@PostConstruct(JSR250)。注意:initMethod已经不推荐,推荐在@Bean方法内部直接通过编程方式实现init方法的内容。
- destroyMethod,等效方法是实现DisposableBean接口,或者在destroy方法上直接使用注解@PreDestroy(JSR250)。
- 实现BeanPostProcessor接口,对所有Bean统一设置init方法调用前后的拦截方法。注意:此接口是Spring非常重要的接口,可以完成与Bean相关的各种分析处理。
// 给容器注册Bean,类型为返回值类型,id默认为方法名,@Bean参数也可以指定id。 @Bean("person") @Scope("singleton") @Lazy public Person person01(){ return new Person("Yanggle") }
@ComponentScan & @ComponentScans
等效于xml文件的<context:component-scan>,@ComponentScan可以配置多个,或者用@ComponentScans组合多个配置。
- value:指定扫描的包
- includeFilters:过滤需要包含的包
- excludeFilters:过滤需要排除的包
- @Filter type:ANNOTATION,ASSIGNABLE_TYPE,CUSTOM,ASPECTJ,REGEX
- classes
- 实现TypeFilter接口,根据不同的场景加载不同的Bean。
@ComponentScan(value="com.yanggle", includeFilters={ @Filter(type="FilterType.ANNOTATION",classes={Controller.class}) } ) @ComponentScan(value="com.yanggle", excludeFilters={ @Filter(type="FilterType.ANNOTATION",classes={Service.class}) }, ) public class AppConfig(){ @Bean ... }
@Conditional
可以标注在方法上,也可以标注在类上
// 给容器注册Bean,类型为返回值类型,id默认为方法名,@Bean参数也可以指定id。 @Conditional({LinuxCondition.class}) @Bean("person") public Person person01(){ return new Person("Yanggle") } public class LinuxCondition implement Condition{ public boolean matches(ConditionContext context,AnnotationTypeMetadata metadata){ //获取BeanFactory //获取类加载器 //获取当前环境信息 //获取Bean注册类 return true; } }
@Import
Bean加载方式:
- 组件注解 + 包扫描:自定义组件常用
- @Bean:加载第三方组件常用
- @Import:id默认是全类名 通过指定需要导入的具体类数组。
- 通过实现ImportSelector接口的selectImports方法,返回需要导入的所有类的全类名数组,注意:此方法不能返回null,但可以返回空数组。
- 通过实现ImportBeanDefinitionRegistrar接口的registerBeanDefinitions方法手工注册Bean到容器中。
- 通过实现Spring提供的FactoryBean接口实现Bean注册到容器中。 默认获取到的是工厂Bean调用getObject创建的对象。
- 要获取工厂Bean本身则需要在id前加一个标识符“&”。
@Configuration @Import({Person.class, MyImportSelector.class,MyImportBeanDefinitionRegistrar.class}) public class AppConfig(){ @Bean ... }
@DependsOn
@EnableTransactionManagement
开启事务管理
MyBatis
@MapperScan(“com.yanggle.dao”)
@Mapper
- 点赞
- 收藏
- 分享
- 文章举报
- SpringBoot常用注解总结
- Spring Boot 集成 resteasy篇 — jax-rs常用注解介绍
- SpringBoot常用注解
- Springboot 常用注解
- 【SpringBoot】常用注解
- SpringBoot常用注解(不断更新中)
- spring boot 实体类常用注解范例
- springboot入门(二)--springboot常用注解及配置
- Springboot 常用注解
- springboot常用注解介绍
- Spring Boot 最常用的25个注解,干货了解一下
- spring boot常用处理url参数注解
- Springboot 常用注解
- springBoot Restfull 常用注解
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍(转)
- spring boot之Spring Mvc开发和常用注解说明
- Spring boot常用注解解释
- springboot常用注解
- springboot中的常用注解
- Springboot 常用注解