Automatic Spring Assembly

Source: Internet
Author: User

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;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:


@ 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 ...");;}}

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 meaning are English, here is not a description.

    @ Retention: How long will you keep this annotation,

    Source code,. Class file, and runtime.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.