Spring commonly USES a summary of annotations

  • 2020-04-01 03:27:29
  • OfStack

This article summarizes the commonly used Spring annotations for easy reference and use, as follows:

Enable automatic scanning before using annotations

Where base-package is the package to be scanned (including subpackage).


<context:component-scan base-package="cn.test"/>

@configuration treats a class as an IoC container, and if an @bean is registered on the head of one of its methods, it is treated as a Bean in the Spring container.
@scope annotates the Scope
@lazy (true) indicates Lazy initialization
@service is used to label business layer components,
@controller is used to annotate control-layer components (such as action in struts)
@repository is used to annotate the data access component, the DAO component.
@component refers to a Component in general, and we can use this annotation to annotate components when they are not easy to categorize.
@scope is used to specify Scope Scope (on a class)
@postconstruct is used to specify an initialization method (on a method)
@predestory is used to specify the destruction method (on the method)
DependsOn: defines the order in which beans are initialized and destroyed
@primary: when multiple Bean candidates are present during auto-assembly, the Bean annotated as @primary will be the preferred, otherwise an exception will be thrown
The @autowired defaults to assembly by type, and if we want to use assembly by name, we can use it in conjunction with the @qualifier annotation. As follows:
There are multiple instances of @autowired @qualifier ("personDaoBean")
@resource is assembled by name by default, and by type only when a bean matching the name cannot be found.
PostConstruct initializes annotations
PreDestroy annotation default singleton   Load on startup
@async asynchronous method call, need to add the following code:


<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10"/>
<property name="maxPoolSize" value="300"/>
</bean>
<task:annotation-driven/> 

I hope this article will be helpful to you.


Related articles: