Spring -- configure bean through annotation, spring -- bean
Spring configures bean through Annotation
Bean configuration based on Annotation
Configure bean Attributes Based on Annotations
Scan components in classpath
Component scan: Spring can automatically scan from classpath to detect and instantiate components with specific annotations.
Specific components include:
-@ Component: Basic annotation that identifies a Component managed by Spring.
-@ Responsitory: identifies the persistence layer component.
-@ Service: identifies a Service layer (Business Layer) component.
-@ Controller: identifies the presentation layer component.
For the scanned components, Spring has the default naming policy: Use a non-qualified class name, and the first letter is in lower case. You can also use the value Attribute value in the annotation to identify the component name.
After a specific annotation is used on the component class, you must declare <context: component-scan> In the Spring configuration file:
The base-package attribute specifies a base class package to be scanned. The Spring container will scan all classes in this base class package and its sub-packages.
To scan multiple packages, use commas to separate them.
If you only want to scan all the classes in the specified class instead of the base package, you can use the resource-pattern attribute to filter the specific classes. For example:
<Context: component-sacn base-package = "com. yl. spring. beans" resource-pattern = "autowire/*. class"/>
<Context: include-filter> a subnode indicates the target class to be included.
<Context: exclude-filter> the subnode indicates the target class to be excluded.
<Context: component-sacn> has several <context: include-filter> and <context: exclude-filter> subnodes.
<Context: include-filter> and <context: exclude-filter> subnodes support multiple types of filter expressions:
Category |
Example |
Description |
Annotation |
Com. yl. XxxAnnotation |
All classes labeled with XxxAnnotation. This type uses whether the target class is labeled with an annotation for filtering. |
Assinable |
Com. yl. XxxService |
All classes that inherit or expand XxxService. This type uses whether the target class inherits or extends a specific class for filtering. |
Aspectj |
Com. yl. * Service |
All classes whose names end with the Service and the classes that inherit or extend them. This type is filtered using the AspectJ expression. |
Regex |
Com. yl. anno .* |
All classes under the com. yl. anno package. This type uses regular expressions and filters based on the class name of the class. |
Custom |
Com. yl. XxxTypeFilter |
The XxxTypeFilter is used to define the filtering principle through code. This class must implement the org. springframework. core. type. TypeFilter Interface |
Component Assembly
<Context: component-scan> the element automatically registers an AutowiredAnnotationBeanPostProcessor instance, which can automatically assemble attributes with @ Autowired, @ Resource, and @ Inject annotations.
Use @ Autowired to automatically assemble beans
@ Autowired annotation automatic assembly of single bean attributes with compatible types
-Constructor. For common fields (even if they are not public), @ Autowired can be applied to all methods with only parameters.
-By default, all attributes using the @ Autowired annotation must be set. If Spring cannot find matching bean Assembly attributes, an exception is thrown. If a property is not allowed to be set, you can set the required attribute of @ Autowired annotation to false.
-By default, when multiple types of compatible beans exist in the IOC container, automatic assembly of types will not work. In this case, the bean name can be provided in the @ Qualifiter annotation. Spring allows the annotation of the input parameters of the method @ Qualifiter to specify the name of the injected bean.
-@ Autowired annotation can also be applied to attributes of the array type. In this case, Spring automatically matches all matching beans.
-@ Autowired annotation can also be applied to set attributes. In this case, Spring reads the type information of the set and then automatically assembles all compatible beans.
-@ Autowired annotation is used in java. util. if the key value of the Map is String, Spring automatically assembles the bean compatible with the Map value type. At this time, the bean name is used as the key value.
TestObject. java
1 package com.yl.annotation;2 3 import org.springframework.stereotype.Component;4 5 @Component6 public class TestObject {7 8 }
UserRepository. java Interface
1 package com.yl.annotation.repository;2 3 public interface UserRepository {4 public void save();5 6 }
UserRepositoryImpl. java
1 package com.yl.annotation.repository; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Repository; 5 6 import com.yl.annotation.TestObject; 7 8 @Repository 9 //@Repository("userRepository")10 public class UserRepositoryImpl implements UserRepository {11 12 @Autowired(required=false)13 private TestObject testObject;14 15 @Override16 public void save() {17 System.out.println("UserRepository save...");18 System.out.println(testObject);19 }20 21 }
UserJdbcRepository. java
1 package com.yl.annotation.repository; 2 3 import org.springframework.stereotype.Repository; 4 5 @Repository 6 public class UserJdbcRepository implements UserRepository { 7 8 @Override 9 public void save() {10 System.out.println("UserJdbcRepository save...");11 }12 13 }
UserService. java
1 package com.yl.annotation.service; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Qualifier; 5 import org.springframework.stereotype.Service; 6 7 import com.yl.annotation.repository.UserRepository; 8 9 @Service10 public class UserService {11 @Autowired12 @Qualifier("userJdbcRepository")13 private UserRepository userRepository;14 15 /*@Autowired16 @Qualifier("userJdbcRepository")17 public void setUserRepository(UserRepository userRepository) {18 this.userRepository = userRepository;19 }*/20 21 public void add() {22 System.out.println("UserService add...");23 userRepository.save();24 }25 }
UserController. java
1 package com.yl.annotation.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 6 import com.yl.annotation.service.UserService; 7 8 @Controller 9 public class UserController {10 @Autowired11 private UserService userService;12 13 public void execute() {14 System.out.println("UserController execute...");15 userService.add();16 }17 }
Beans-annotation.xml
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xmlns: context = "http://www.springframework.org/schema/context" 5 xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context 6 http://www.springframework.org/schema/contex T/spring-context-4.1.xsd "> 7 8 <! -- Specify the Spring IOC container scan package --> 9 <! -- You can use resource-pattern to specify the scanned resource --> 10 <! -- <Context: component-scan 11 base-package = "com. yl. annotation "12 resource-pattern =" repository /*. class "> </context: component-scan> --> 13 14 <! -- Context: exclude-filter subnode specifies which components of the specified expression are excluded --> 15 <! -- Context: The include-filter subnode specifies the components that contain the specified expressions. The subnode must use-default-filters in combination with --> 16 <context: component-scan 17 base-package = "com. yl. annotation "> 18 <! -- Use-default-filters = "false"> --> 19 <! -- <Context: exclude-filter type = "annotation" 20 expression = "org. springframework. stereotype. Repository"/> --> 21 22 <! -- <Context: include-filter type = "annotation" 23 expression = "org. springframework. stereotype. Repository"/> --> 24 <! -- <Context: exclude-filter type = "assignable" 26 expression = "com. yl. annotation. repository. UserRepository"/> --> 27 28 <! -- <Context: include-filter type = "annotation" 29 expression = "com. yl. annotation. repository. userRepository "/> --> 30 </context: component-scan> 31 </beans>
Use @ Resource or @ Inject to automatically assemble bean
Spring also supports @ Resource and @ Inject annotations. These two annotations are similar to the functions of @ Autowired annotations.
@ Resource annotation requires a bean name attribute. If this attribute is empty, the variable or method name at the annotation is automatically used as the bean name.
@ Inject and @ Autowired annotations are also injection beans by type, but there is no required attribute
@ Autowired annotation is recommended.