Automatic Spring Assembly
Strictly speaking, it can be divided into automatic detection and automatic assembly.
Automatic Detection allows Spring to automatically detect and define beans. This means that you do not use You can also implement the definition and assembly of most beans in Spring.
To enable this function, you must introduce the context namespace.
Use context: component-scan and specify the packages under which classes can be detected in base-package.
The problem arises. What if you want to make the classes under multiple packages be detected? Google "context: component-scan multiple package", the answer is:
Is separated by commas.
Which classes will Spring register as beans? Default, The class annotated with the stereotype annotation will be searched, including:
@ Component @ Controller @ Repository @ Service use the custom annotation marked by @ Component
package org.chen.auto;import org.springframework.stereotype.Component;@Component("my-guitar")public class Guitar implements Instrument {public void play() {System.out.println("play guitar ... ");}}
The above Guitar class uses the @ Component annotation. By default, its ID is an infinitely fixed class name, that is, guitar. Of course, you can customize it when using the annotation.
In this way, automatic detection and registration are completed.
However, it is annoying to write @ Component to all classes in a package. Spring provides us with scanning of Filter components.
And , Including include and exclude.
For example,
If we use,
Indicates that all classes implementing Instrument will be detected and registered (equivalent to adding @ Component ).
Type is the filter type,
Annotation-based filtering is the most common method.
The following describes automatic assembly.
@ Autowired annotation and @ Inject can accomplish similar work. The difference is that @ Inject comes with the Java language. We recommend using it, javax. inject is a Java EE version. If you are using the Java SE version, you need to manually add it to build path. If you are using Maven, you can add the following dependencies:
javax.inject
javax.inject
1
@ Inject can be used to automatically assemble familiarity, methods, and constructors.
Because @ Inject is assembled by type, an error is returned if multiple types in the container are satisfied. At this time, we can use @ Named to limit (the Bean ID is used by default ).
@Componentpublic class TestBean {@Inject@Named("pianeo")private Instrument instrument;public void say(){System.out.println("test bean is saying ...");instrument.play();}}
At the end of the article, we will give a custom annotation method,
@Target({ ElementType.TYPE })@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {}
If you want to write @ Component, that is
@Target({ ElementType.TYPE })@Retention(RetentionPolicy.RUNTIME)
@Componentpublic @interface MyAnnotation {}
@ Target indicates where the annotation can be used. Including:
- ElementType. TYPE (class, interface, enum)
- ElementType. FIELD (instance variable)
- ElementType. METHOD
- ElementType. PARAMETER
- ElementType. CONSTRUCTOR
- ElementType. LOCAL_VARIABLE
- ElementType. ANNOTATION_TYPE (on another annotation)
- ElementType. PACKAGE (remember package-info.java) meaning are English, here is not a description.
@ Retention: How long will you keep this annotation,
Source code,. Class file, and runtime.