Spring Automatic Assembly

Source: Internet
Author: User

Strictly speaking, the actual detection and automatic assembly are divided into two aspects.

First, auto-detect, Auto-detect is to allow spring to automatically detect and define beans. This means that without the use of <BEAN/>, you can also implement the definition and assembly of most of the beans in spring.

To enable this feature, you have to introduce a context namespace.

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"    Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"     xmlns:context= "Http://www.springframework.org/schema /context "    xsi:schemalocation="        Http://www.springframework.org/schema/beans/         http Www.springframework.org/schema/beans/spring-beans.xsd         Http://www.springframework.org/schema/context          Http://www.springframework.org/schema/context/spring-context.xsd ">            <context:component-scan        Base-package= "Org.chen.auto" >    </context:component-scan></beans>

and use Context:component-scan, and in base-package indicate which packages under which the class can be detected.

The question is, what if you want to enable classes under multiple packages to be detected? Google "Context:component-scan multiple package", the answer is:

<context:component-scan base-package= "X.y.z.service, X.y.z.controller"/>

is separated by commas.


So which classes will spring register as beans? The default,<context:component-scan> looks for classes annotated with stereotype (stereotype) annotations, including:

    1. @Component
    2. @Controller
    3. @Repository
    4. @Service
    5. Custom annotations using @component annotations


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 guitar class above uses the @component annotation, whose ID is the infinite class name, the guitar, and of course you can customize it when using annotations.

This completes the automatic detection and registration.


However, if you need to write all the classes below a package, it is annoying to @component. Spring provides us with a scan of the filtering components.

<context:include-filter/> and <context:exclude-filter/>, respectively, are included and excluded.

Like what

If we use it,

  <context:component-scan        base-package= "Org.chen.auto" >        <context:include-filter type= "assignable "expression=" org.chen.auto.Instrument "/>    </context:component-scan>

Indicates that all classes implementing instrument are detected and registered (equivalent to adding @component).

Type is a filter,


Annotation-based filtering is the most common.


Let's talk about automatic assembly.

@Autowired annotations and @inject can do similar work. The difference is that @Inject is the Java language comes with, it is recommended to use it, Javax.inject is Java EE version, if you are using the version of the SE, you need to manually add to the build path, if you are using MAVEN, you can add the following dependencies:

<dependency><groupId>javax.inject</groupId><artifactId>javax.inject</artifactId> <version>1</version>       </dependency>

@Inject can be used to automate assembly of familiarity, methods, and constructors.

Because the @inject is assembled by type, an error is given if there are multiple types in the container that are satisfied. We can then use @named to qualify (using the bean's ID 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 give a way to define annotations,
@Target ({elementtype.type}) @Retention (retentionpolicy.runtime) public @interface myannotation {}

If you want to write @Component,

@Target ({elementtype.type}) @Retention (Retentionpolicy.runtime)
@Componentpublic @interface myannotation {}

Where @Target is the target, indicating 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)
The meaning is English translation, here does not explain each.


@Retention indicate how long you will keep this note,


This is a document from Oracle. There are source code,. class file, run at three kinds.

Spring Automatic Assembly

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: info-contact@alibabacloud.com 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.