Foreword: This article focuses on sorting out various annotations in Spring, briefly introducing the usage scenarios and usage of each annotation, without doing in-depth research, it can be used as a manual query.
1 Overview
When the note configuration mode is enabled, the functions of the relevant tags in the configuration file:
<!-- Start the scanning component function--> <context:component-scan base-package="com"/> <!-- Start the injection function--> <context:annotation-config /> <!-- Start the annotation transaction configuration function--> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- SpringMVC --> <mvc:annotation-driven/>
Overview of Common Annotations
annotation | effect |
@Component | Annotate a normal Spring Bean class |
@Controller | Annotate a controller component class |
@Service | Annotate a business logic component class |
@Repository | Annotate a Dao component |
@Scope | Specify the scope of the bean instance |
@Resource | Assemble by name first, then assemble by type if not found |
@Autowired | Assemble by type first, then assemble by name if not found, then report error |
@Primary | When there are multiple bean candidates during autowiring, the bean annotated with @Primary will be the first choice |
@Autowired @Qualifier | There are multiple instances used together |
@Resource | Assemble by name first, then assemble by type if not found |
@Lazy(true) | Indicates lazy initialization |
@PostConstruct | Used to specify the initialization method (used on methods) |
@PreDestory | Used to specify the destruction method (used on methods) |
@DependsOn | Define the order in which beans are initialized and destroyed |
@Transational | Encapsulates the AOP implementation of transaction management |
@Async | Asynchronous method call |
2. Mind map