This article summarizes Spring's commonly used annotations to facilitate everyone's query and use, as follows:
Before using annotations, turn on the automatic scanning function
Among them, base-package is the package (including sub-packages) that needs to be scanned.
<context:component-scan base-package="cn.test"/>
@Configuration treats a class as an IoC container. If @Bean is registered on one of its method headers, it will be used as a Bean in the Spring container.
@Scope annotation scope
@Lazy(true) indicates lazy initialization
@Service is used to annotate business layer components,
@Controller is used to annotate control layer components (such as action in struts)
@Repository is used to annotate data access components, namely DAO components.
@Component generally refers to components. When components are difficult to classify, we can use this annotation to annotate them.
@Scope is used to specify scope (used on classes)
@PostConstruct is used to specify the initialization method (used on methods)
@PreDestory is used to specify the destruction method (used on methods)
@DependsOn: Define the order of Bean initialization and destruction
@Primary: When multiple Bean candidates appear during automatic assembly, the Bean annotated with @Primary will be the first choice, otherwise an exception will be thrown.
@Autowired is assembled by type by default. If we want to use assembly by name, we can use it in conjunction with the @Qualifier annotation. as follows:
@Autowired @Qualifier("personDaoBean") exists with multiple instances used together
@Resource is assembled by name by default. When a bean matching the name cannot be found, it will be assembled by type.
@PostConstruct initialization annotation
@PreDestroy destroys the annotation and loads it when the singleton is started by default.
@Async asynchronous method call, you need to add the following code:
<bean id="taskExecutor"><property name="corePoolSize" value="10"/><property name="maxPoolSize" value="300"/></bean><task:annotation-driven/>
I hope this article can be helpful to everyone.