Spring--Spring Annotation Encyclopedia and Detailed Explanation

Annotations and explanations used by Spring

annotation Explanation
@Controller Combined annotation (combined with @Component annotation), applied in the MVC layer (control layer), DispatcherServlet will automatically scan the annotated class, and then map the web request to the method annotated @RequestMapping.
@Service Combined annotation (combined @Component annotation), applied in service layer (business logic layer)
@Reponsitory Combined annotation (combined with @Component annotation), applied in dao layer (data access layer)
@Component Indicates that an annotated class is a "component" and becomes a Bean managed by Spring. When using annotation-based configuration and class path scanning, these classes are considered candidates for automatic detection. At the same time @Component is still a meta-annotation.
@Autowired Tools provided by Spring (automatically injected by Spring's dependency injection tools (BeanPostProcessor, BeanFactoryPostProcessor).)
@Resource Notes provided by JSR-250
@Inject Notes provided by JSR-330
@Configuration Declare that the current class is a configuration class (equivalent to a Spring configuration xml file)
@ComponentScan Automatically scan all classes using @Service, @Component, @Controller, @Repository under the specified package and register
@Bean Annotation on the method, declare that the return value of the current method is a Bean. The init () method and destroy () method can be defined in the class corresponding to the returned Bean, and then defined in @Bean (initMethod = ”init”, destroyMethod = ”destroy”), execute init after construction, and destroy before destruction.
@Aspect Declare a aspect (that is to say, this is an additional function)
@After Post advices are executed before the original method.
@Before Pre-advice (advice) is executed after the original method.
@Around Advice around (advice), executed before the original method is executed, and then executed after the original method is executed (@Around can implement the other two types of advice)
@PointCut Declare pointcuts, ie define interception rules and determine which methods will be cut in
@Transactional Declare transactions (general default configuration can meet the requirements, of course, you can also customize)
@Cacheable Declaring data cache
@EnableAspectJAutoProxy Turn on Spring's support for AspectJ
@Value It is worth injecting. Often used with the Sping EL expression language to inject ordinary characters, system attributes, expression operation results, other bean attributes, file content, URL request content, configuration file attribute values, etc
@PropertySource Specify the file address. Provides a convenient, declarative mechanism for adding PropertySource to Spring's environment. Used with @configuration class.
@PostConstruct Marked on the method, the method is executed after the execution of the constructor is completed.
@PreDestroy Marked on the method, the method is executed before the object is destroyed.
@Profile Indicates that when one or more specified files are active, a component is eligible for registration. Use @Profile annotation class or method to achieve the choice of instantiating different beans in different situations. @Profile ("dev") means instantiated when dev.
@EnableAsync Enable asynchronous task support. The annotation is on the configuration class.
@Async The annotation indicates that this method is an asynchronous method, and indicates that all methods of this class are asynchronous methods on the class.
@EnableScheduling Note On the configuration class, support for scheduled tasks is enabled.
@Scheduled Note on the method, stating that the method is a scheduled task. Support multiple types of scheduled tasks: cron, fixDelay, fixRate
@Conditional Create specific beans based on meeting a specific condition
@Enable* 通过简单的@Enable*来开启一项功能的支持。所有@Enable*注解都有一个@Import注解,@Import是用来导入配置类的,这也就意味着这些自动开启的实现其实是导入了一些自动配置的Bean(1.直接导入配置类2.依据条件选择配置类3.动态注册配置类)
@RunWith 这个是Junit的注解,springboot集成了junit。一般在测试类里使用:@RunWith(SpringJUnit4ClassRunner.class) — SpringJUnit4ClassRunner在JUnit环境下提供Sprng TestContext Framework的功能
@ContextConfiguration 用来加载配置ApplicationContext,其中classes属性用来加载配置类:@ContextConfiguration(classes = {TestConfig.class(自定义的一个配置类)})
@ActiveProfiles 用来声明活动的profile–@ActiveProfiles(“prod”(这个prod定义在配置类中))
@EnableWebMvc 用在配置类上,开启SpringMvc的Mvc的一些默认配置:如ViewResolver,MessageConverter等。同时在自己定制SpringMvc的相关配置时需要做到两点:1.配置类继承WebMvcConfigurerAdapter类2.就是必须使用这个@EnableWebMvc注解。
@RequestMapping 用来映射web请求(访问路径和参数),处理类和方法的。可以注解在类和方法上,注解在方法上的@RequestMapping路径会继承注解在类上的路径。同时支持Serlvet的request和response作为参数,也支持对request和response的媒体类型进行配置。其中有value(路径),produces(定义返回的媒体类型和字符集),method(指定请求方式)等属性。
@ResponseBody 将返回值放在response体内。返回的是数据而不是页面
@RequestBody 允许request的参数在request体中,而不是在直接链接在地址的后面。此注解放置在参数前。
@PathVariable 放置在参数前,用来接受路径参数。
@RestController 组合注解,组合了@Controller和@ResponseBody,当我们只开发一个和页面交互数据的控制层的时候可以使用此注解。
@ControllerAdvice 用在类上,声明一个控制器建言,它也组合了@Component注解,会自动注册为Spring的Bean。
@ExceptionHandler 用在方法上定义全局处理,通过他的value属性可以过滤拦截的条件:@ExceptionHandler(value=Exception.class)–表示拦截所有的Exception。
@ModelAttribute 将键值对添加到全局,所有注解了@RequestMapping的方法可获得次键值对(就是在请求到达之前,往model里addAttribute一对name-value而已)。
@InitBinder 通过@InitBinder注解定制WebDataBinder(用在方法上,方法有一个WebDataBinder作为参数,用WebDataBinder在方法内定制数据绑定,例如可以忽略request传过来的参数Id等)。
@WebAppConfiguration 一般用在测试上,注解在类上,用来声明加载的ApplicationContext是一个WebApplicationContext。他的属性指定的是Web资源的位置,默认为src/main/webapp,我们可以修改为:@WebAppConfiguration(“src/main/resources”)。
@EnableAutoConfiguration 此注释自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找。该注解组合了@Import注解,@Import注解导入了EnableAutoCofigurationImportSelector类,它使用SpringFactoriesLoader.loaderFactoryNames方法来扫描具有META-INF/spring.factories文件的jar包。而spring.factories里声明了有哪些自动配置。
@SpingBootApplication SpringBoot的核心注解,主要目的是开启自动配置。它也是一个组合注解,主要组合了@Configurer,@EnableAutoConfiguration(核心)和@ComponentScan。可以通过@SpringBootApplication(exclude={想要关闭的自动配置的类名.class})来关闭特定的自动配置。
@ImportResource 虽然Spring提倡零配置,但是还是提供了对xml文件的支持,这个注解就是用来加载xml配置的。例:@ImportResource({“classpath
@ConfigurationProperties 将properties属性与一个Bean及其属性相关联,从而实现类型安全的配置。例:@ConfigurationProperties(prefix=”authot”,locations={“classpath
@ConditionalOnBean 条件注解。当容器里有指定Bean的条件下。
@ConditionalOnClass 条件注解。当类路径下有指定的类的条件下。
@ConditionalOnExpression 条件注解。基于SpEL表达式作为判断条件。
@ConditionalOnJava 条件注解。基于JVM版本作为判断条件。
@ConditionalOnJndi 条件注解。在JNDI存在的条件下查找指定的位置。
@ConditionalOnMissingBean 条件注解。当容器里没有指定Bean的情况下。
@ConditionalOnMissingClass 条件注解。当类路径下没有指定的类的情况下。
@ConditionalOnNotWebApplication 条件注解。当前项目不是web项目的条件下。
@ConditionalOnResource 条件注解。类路径是否有指定的值。
@ConditionalOnSingleCandidate 条件注解。当指定Bean在容器中只有一个,后者虽然有多个但是指定首选的Bean。
@ConditionalOnWebApplication 条件注解。当前项目是web项目的情况下。
@EnableConfigurationProperties 注解在类上,声明开启属性注入,使用@Autowired注入。例:@EnableConfigurationProperties(HttpEncodingProperties.class)。
@AutoConfigureAfter 在指定的自动配置类之后再配置。例:@AutoConfigureAfter(WebMvcAutoConfiguration.class)
发布了7 篇原创文章 · 获赞 69 · 访问量 20万+

Guess you like

Origin blog.csdn.net/u014320421/article/details/103713891