Assembly of Spring

Source: Internet
Author: User

Spring assembly is available in three ways:

    • XML-based explicit configuration
    • Automatic assembly based on annotations
    • Display configuration in Java

1. Automatic assembly based on annotations

Spring Auto-transfer from two angles of component scanning and automatic assembly

    • Component Scan: Spring automatically discovers the bean that is created in the application context and needs to specify the scanned package when configured
<context:component-scan base-package= "com.cn" ></context:component-scan>

The component scan gives the class instantiation marked with the following annotations to the spring container management

@Controller, @Service, @Repository, @Component
    • Automatic assembly: Spring automatically satisfies the dependencies between beans

At the time of component scanning, after the bean is created, the container will satisfy the bean's dependency as much as possible. Automatic assembly is implemented by the annotation @autowired, which is defined as follows:

@Target ({elementtype.constructor, Elementtype.field, Elementtype.method, Elementtype.annotation_type}) @Retention ( Retentionpolicy.runtime) @Documentedpublic @interface autowired {    boolean required () default true;}

A, the value of the property required defaults to True, indicating that after the bean is created, it is important that the matching bean is assembled by the type of the @autowired annotation, otherwise the container starts throwing an exception:

caused By:org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type [ Com.cn.pojo.Person] found for dependency:expected at least 1 beans which qualifies as Autowire candidate for this Dependen Cy. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (Required=true)}

b, setting the value of required to false,spring will attempt to perform automatic assembly, but if there is no matching bean,spring The bean will be left in an assembly state. At this time, there will be a hidden danger, usually need to do a null check, or throw NullPointerException exception

C, regardless of whether or not to set the value of required, when there are multiple beans in the container that satisfy the dependency, spring throws an exception indicating that the bean to be selected for automatic assembly is not explicitly indicated.

Spring offers a variety of options to address this ambiguity:

1) Set one of the optional beans as preferred, and spring will use the preferred bean when the assembly encounters such ambiguity.

@Primary annotation configuration, often used with @controller, @Service, @Repository, @Component:

Package Com.cn.pojo;import Org.springframework.context.annotation.primary;import Org.springframework.stereotype.Component, @Primary @componentpublic class Person {//...}

or XML configuration:

<bean id= "Person" class= "Com.cn.pojo.Person" primary= "true" ></bean>

NOTE: If you configure two or more than two preferred beans, then it will not work properly, and there is also ambiguity.

2) Use qualifiers to specify spring-assembled beans

@Qualifier annotations are the primary way to use qualifiers, and are often used with @autowired

Package Com.cn.controller;import Com.cn.pojo.person1;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import org.springframework.stereotype.controller;@ Controllerpublic class Hellocontroller {    @Autowired    @Qualifier ("Person1")    private Person1 Person1;    public void print () {        System.out.println (person1.tostring ());}    }

Qualifier:

When instantiating a bean, the default qualifier is the first letter lowercase class name. Consistent with the default Bean ID rule, if the bean does not want to use the default ID, you can specify the value of the ID on the @controller, @Service, @Repository, @Component.

You can use @Qualifier annotations to specify qualifiers for a class when instantiating an object

Package Com.cn.pojo;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.context.annotation.primary;import org.springframework.stereotype.component;@ Component@qualifier ("person100") public class Person1 {   //...}

2. Explicit XML-based configuration

    • Initializing a bean with a constructor

A, assembly literal

B, Assembly Bean

C, Assembly set

D, c namespaces

    • Set Property Initialization Bean

A, assembly literal

B, Assembly Bean

C, Assembly set

D, p namespaces

    • Util namespaces: Used to define a collection that is separate from the bean and can be referenced by an assembly bean

Summary: In general, annotation-based automatic assembly and XML-based display configuration are used in the project, and there are few display configurations in Java.

Assembly of Spring

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.