Common Spring annotations and spring annotations
Enable automatic scanning before using annotations
The base-package is the package to be scanned (including the sub-package ).
<context:component-scan base-package="cn.test"/>
@ Configuration regards a class as an IoC container. If @ Bean is registered in a method header, it will act as the Bean in the Spring container. @ Scope annotation Scope @ Lazy (true) indicates delayed initialization @ Service is used to mark Service layer components and @ Controller is used to mark control layer components (such as actions in struts)
@ Repository is used to mark the data access component, that is, the DAO component. @ Component refers to a Component. When the Component is difficult to classify, we can use this annotation for annotation.
@ Scope is used to specify the scope Scope (used in the class) @ PostConstruct is used to specify the initialization method (used in the method)
@ PreDestory: Specifies the destruction method (used on the method)
@ DependsOn
:Define the sequence of Bean initialization and destruction
@ Primary
:When multiple Bean candidates exist during automatic assembly, the Bean annotated as @ Primary will be the first candidate; otherwise, an exception @ Autowired is thrown and assembled by type by default, if you want to use name-based assembly, you can use it together with the @ Qualifier annotation. As follows: @ Autowired @ Qualifier ("personDaoBean") there are multiple instances used together with @ Resource assembled by name by default. Only beans with the same name cannot be found will be assembled by type. @ PostConstruct initialization annotation @ PreDestroy destroy annotation loading @ Async Asynchronous Method call by default when a single instance is started. The following code needs to be added:
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"><property name="corePoolSize" value="10"/><property name="maxPoolSize" value="300"/></bean><task:annotation-driven/>
Spring uses annotations
Do not be confused by the name, UserDAOImpl
How does one use spring annotations in java Development?
Annotation configuration (simplified XML configuration file)
1) automatic component Scanning
First, add <context: component-scan/> In applicationContext. xml.
A. Scan the Bean component annotation to replace the definition of the <bean> element in xml.
@ Service: used for Service Components
@ Control: used for Action Control components
@ Respository: used to access DAO data components
@ Component for other components
After the Bean component scans the container, the default name is the class name (in lower case)
To customize the name, use @ Service ("id ")
B. annotation mark of dependency Injection
@ Resource by name @ Resource (name = "id ")
@ AutoWired by name
@ Autowired
@ Qualifier ("id name ")
C. Other annotations
@ Scope is equivalent to <bean scope = "">
@ PostConstruct is equivalent to <bean init-method = "">
@ PreDestroy is equivalent to <bean destroy-method = "">