Spring all annotations Big Secret

Statement bean notes

  • @Component components, there is no clear role
  • @Service use (service layer) Yewuluojiceng
  • Use @Repository (DAO layer) in the data access layer
  • @Controller use in the presentation layer, the statement Controller

Inject bean notes

  • @Autowired: provided by Spring, automatic assembly according to the type, if the combination of the name will be assembled using @Qualifier
  • @Inject: providing javax.inject.Inject need to import using the JSR-330; the same is automatically achieved injection assembly according to the type, by name if required for assembly, it is necessary with @Named
  • @Resource: by JSR-250, introduced in javax.annotation need to use, based on the name of the automatic assembly, generally specify a name attribute

Functional annotation

  • @Transactional statement affairs
  • @Cacheable statement cache

A comment about configuration class

  • @Configuration declare the current class configuration class
  • @Bean comment on the method, declare the current method returns a value of a bean
  • Component @ComponentScan for scanning,
  • @WishlyConfiguration combination with @ComponentScan @Configuration annotations can replace the two annotations

Section (AOP) related annotations

  • @Aspect Declaring an aspect
  • @After executed after execution method
  • @Before executed before execution method
  • @Around perform before and after the execution method
  • @PointCut sound Mingqie point

@Bean attributes support

Spring new set @Scope Bean type, optionally comprising:

  • Singleton single embodiment, a Spring bean container only one example, the default mode
  • Protetype each call to create a new bean
  • Request web project, every http request to create a new bean
  • Session web project, to create a new bean each http session
  • GlobalSession (a global http session for each new instance of a Bean

Class lifecycle comment

  • @PostConstruct by JSR-250, after executing the class's constructor performed, equivalent to the xml configuration file in the bean initMethod
  • @PreDestory provided by JSR-250, performed before the destruction Bean

Configuration injection

@Value injection attribute value, the injection support in the following manner:

  • Ordinary characters @Value ( "Michael Jackson")
  • Operating system properties @Value ( "# {systemProperties [ ' The os.name ']}")
  • Results Expression @Value ( "# {T (java.lang.Math) .random () * 100}")
  • Other bean property @Value ( "# { domeClass.name }")
  • File Explorer @Value ( "classpath: cn / shiyujun / test.txt")
  • Web site resources @Value ( " https://shiyujun.cn ")
  • Profile property @Value ( "{$ book.name }")

Injection configuration file @PropertySource ( "classpath: cn / shiyujun / test.propertie")

Context switching

  • @Profile to set the current configuration environment context to use by setting Environment of ActiveProfiles
  • @Conditional bean loading of different codes according to the conditions set, including a series of notes
    • @ConditionalOnBean existence bean
    • @ConditionalOnMissingBean bean does not exist
    • @ConditionalOnClass there is a class
    • @ConditionalOnMissingClass a class does not exist
    • @ConditionalOnProperty (prefix = "syj", name = "algorithm", havingValue = "token"), when present in the configuration file SYJ prefixed attributes, attribute name algorithm, then it will instantiate a token value class.
    • @ConditionalOnProperty (prefix = "syj", name = "algorithm", havingValue = "counter", matchIfMissing = true) if all are not met, then the choice counter default implementation
    • If @ConditionalOnJava is a Java application
    • If the Web application is @ConditionalOnWebApplication

Asynchronous relevant

  • @Async use the annotation bean method to declare its actual implementation is an asynchronous task

The timing of tasks related to

  • @Scheduled to declare this is a task, including cron, fixDelay, fixRate other types

Some features open

  • @EnableAspectJAutoProxy turn on automatic proxy support for AspectJ
  • @EnableAsync open support asynchronous method
  • @EnableScheduling open Scheduled Tasks support
  • @EnableWebMvc open configuration support of Web MVC
  • @EnableConfigurationProperties turn supports the configuration of @ConfigurationProperties Bean notes
  • @EnableJpaRepositories open support for the SpringData JPA Repository
  • @EnableTransactionManagement open transaction annotation support
  • @EnableCaching open an annotation-caching support

Test-related notes

  • @RunWith Spring is usually used to support for the JUnit
  • @ContextConfiguration used to load configuration ApplicationContext

SpringMVC part

  • @RequestMapping Web requests for mapping, including access paths and parameters
  • ResponseBody support the return value placed in the response, rather than a page
  • Means for receiving path parameter @PathVariable
  • @RestController The annotation is a combination of notes, equivalent to a combination of @Controller and @ResponseBody, and notes on the class, which means all of the methods of the Controller will assume added @ResponseBody
  • @ControllerAdvice by the annotation, we can place the profile for the global controller in the same position, the annotation @Controller method, can be utilized @ ExceptionHandler, @ InitBinder, @ ModelAttribute annotation to the method,
    which all annotated! the method in the controller RequestMapping effective.
  • @ExceptionHandler used for global exception handling in the controller
  • @InitBinder to set WebDataBinder, WebDataBinder reception request parameters used to automatically bind the Model.
  • Originally role @ModelAttribute is to bind key-value pairs in the Model, is to let the global @RequestMapping have access to the key set here in @ControllerAdvice in.

SpringBoot related

  • @EnableAutoConfiguration Bean automatically loads all the required applications. The combination of the annotation @Import annotations, @ Import annotation introduced EnableAutoCofigurationImportSelector class, which uses the scanning method SpringFactoriesLoader.loaderFactoryNames jar package having a META-INF / spring.factories file. The spring.factories in a statement which automatically configures
    + @SpingBootApplication SpringBoot core notes, the main purpose is to open the automatic configuration. It is also a combination of notes, mainly combines @ Configurer, @ EnableAutoConfiguration (core) and @ComponentScan. Can be closed by means of specific automatic configuration @SpringBootApplication (exclude = {class want to close the .class} auto-configuration)
  • @ImportResource load xml configuration
  • @AutoConfigureAfter rearranged after the specified class autoconfiguration

Guess you like

Origin www.cnblogs.com/zhixiang-org-cn/p/11290054.html