Scanning components in the Classpath
Component scanning (component scanning): Spring is able to automatically scan, detect and instantiate components with specific annotations from the classpath.
Specific components include:
@Component: Basic annotations that identify a component that is managed by Spring
@Respository: Identifying durable layer components
@Service: Identify service layer (business layer) components
@Controller: Identify presentation layer components
For scanned components, Spring has a default naming policy: Use unqualified class name, first letter lowercase. You can also identify the name of a component in the annotation with the Value property values
The Base-package property specifies a base class package that needs to be scanned, and the Spring container will scan all classes in the base class package and its child packages.
When you need to scan multiple packages, you can use commas to separate them.
If you want to scan only specific classes, not all classes under the base package, you can use the Resource-pattern property to filter specific classes, for example:
<context:component-scan base-package = "Com.yslf.annotation" resource-pattern= " Repository/*.class "></context:component-scan>
<context:include-filter> child nodes represent the target classes to be included
<context:exclude-filter> child nodes indicate the target class to exclude
<context:component-scan> can have a number of <context:include-filter> and <context:exclude-filter> sub-nodes
<context:include-filter> and <context:exclude-filter> sub-nodes support multiple types of filter expressions:
The <context:component-scan> element also automatically registers the Autowiredannotationbeanpostprocessor instance, which can be automatically assembled with @Autowired and @Resource, @ The properties of the inject annotation.
Using @autowired
@Autowired annotations automatically assemble individual bean properties with compatible types
-Constructors, normal fields (even non-public), all methods with parameters can be applied @authwired annotations
-By default, all properties that use @Authwired annotations need to be set. When Spring cannot find a matching Bean assembly property, an exception is thrown, and if a property is allowed to not be set, the required property of the @Authwired annotation can be set to False
-By default, automatic assembly by type will not work when there are multiple type-compatible beans in the IOC container. The name of the Bean can now be provided in the @Qualifier annotations. Spring allows the parameter callout of a method @Qualifiter the name of the injected Bean specified
[Email protected] annotations can also be applied to attributes of an array type, at which time Spring will automatically assemble all matching beans.
[Email protected] annotations can also be applied to collection properties, when Spring reads the type information for that collection, and then automatically assembles all the beans that are compatible with it.
[Email protected] When the annotation is used on JAVA.UTIL.MAP, if the map has a key value of String, Spring will automatically assemble a bean that is compatible with the map value type, at which point the bean name is used as the key value
Automatically assemble beans using @autowired or @inject
Spring also supports @Resource and @Inject annotations, which are similar to the functions of @Autowired annotations
@Resource annotations require a property of the bean name, and if the property is empty, the variable or method name at the label is automatically used as the bean's name
@Inject, like @Autowired annotations, are beans injected by type matching, but without the Reqired property
Suggested use of @Autowired annotations
Related code:
Structure:
Usercontroller.java
@Controller Public class Usercontroller { @Autowired private userservice userservice; Public void Execute () { System.out.println ("Usercontroller execute ..."); Userservice.add (); }}
Userrepository.java
Public Interface userrepository { void Save ();}
Userjdbcrepository.java
@Repository Public class Implements userrepository{ @Override publicvoid Save () { System.out.println ("userjdbcrepository save ..."); }
Userrepositoryimpl.java
@Repository Public class Implements userrepository{ @Autowired (Required=false) private Testobject Testobject; @Override publicvoid Save () { System.out.println (" Userrepository Save ... "); System.out.println ("Testobject");} }
Userservice.java
@Service Public class userservice { private userrepository userrepository; @Autowired publicvoid setuserrepository (@Qualifier ("Userrepositoryimpl") Userrepository userrepository) { this. userrepository = userrepository; } Public void Add () { System.out.println ("userservice add ..."); Userrepository.save (); }}
Testobject.java
Public class testobject { }
Main.java
Public classMain { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Bean-annotation.xml");//testobject testobject= (testobject) Context.getbean ("Testobject");//System.out.println (testobject);Usercontroller Usercontroller= (Usercontroller) context.getbean ("Usercontroller"); System.out.println (Usercontroller); Usercontroller.execute (); //userservice userservice= (userservice) Context.getbean ("UserService");//System.out.println (userservice);// //userrepository userrepository= (userrepository) Context.getbean ("Userrepository");//System.out.println (userrepository); }}
Bean-annotation.xml
<!--Specify the package scanned by the spring IOC container-<!--You can specify the scanned resources by resource-pattern= "Repository/*.class"-<!--< ; Context:component-scan base- Package= "Com.yslf.annotation"Resource-pattern= "Repository/*.class" > </context:component-scan>-<!--context:exclude-filter Sub-section Point specifies which components of the specified expression are excluded-<!--context:include-The Filter child node specifies the component that contains the expression that requires the use-default-filters= "false" mates-<!----> <!----> <context:component-scan base- Package= "Com.yslf.annotation" > <!--<context:exclude-filter type= "annotation"expression= "Org.springframework.stereotype.Repository"/>-<!--<context:include-filter Type= "a Nnotation "expression=" Org.springframework.stereotype.Repository "/>-<!--<context:ex Clude-filter type= "assignable" expression= "Com.yslf.annotation.repository.UserRepository"/>--<!- -<context:include-filter type= "assignable" expression= "Com.yslf.annotation.repository.UserRepository"/> --></context:component-scan>
Annotation-based approach to spring configuration form