<spring combat >3: Minimizing Spring XML configuration

Source: Internet
Author: User

1 Automatic Assembly Bean Properties

1.1 4 types of automatic assembly

    • ByName: Automatically assemble other beans with the same name or ID as the bean's attributes into the corresponding properties of the bean
    • Bytype: Automatically assemble other beans of the same type as the bean's attributes into the corresponding properties of the bean
    • Constructor: Automatically assembles other beans with the same type as the bean's constructor into the corresponding entry parameters of the Bean constructor
    • AutoDetect: First try to use constructor for automatic assembly, if you fail then try to use Bytype for automatic assembly

1.2 Default automatic assembly

1.3 Mixing with automatic assembly and display assembly

2 Assembling with annotations

Spring disables annotation assembly by default and needs to be enabled in the configuration:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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:annotation-config/></Beans>

Spring 3 supports a number of different annotations:

    • @Autowired: Spring comes with
    • @Inject: JSR-330
    • @Resource: JSR-250

2.1 Using @autowired

@Autowired can label the setter method instead of the <property> element and try to perform Bytype automatic assembly on the method. You can also annotate any method that requires automatic assembly of Bean references.

@Autowired can also label the constructor, even if the <constructor-arg> element is not used in the XML configuration file, and is automatically assembled.

@Autowired can label attributes directly and remove setter methods.

Problem: You must have only one Bean in your app that fits into the attributes or parameters noted in the annotations.

If the property does not have to be assembled, NULL is also acceptable, and you can set the required property to false, such as:

false )Private instrument instrument;

If you have multiple beans that meet the assembly criteria, you can specify the bean with Spring's @Qualifier annotations:

@Autowired @qualifier ("saxophone") Private instrument instrument;

This will attempt to inject a Bean with ID saxophone.

To create a custom Qualifier:

 import   Org.springframework.beans.factory.annotation.Qualifier;  import   Java.lang.annotation.ElementType;  import   java.lang.annotation.Retention;  import   Java.lang.annotation.RetentionPolicy;  import   Java.lang.annotation.Target; @Target ({Elementtype.field, elementtype.parameter, Elementtype.type}) @Retention (retentionpolicy.runtime) @Qualifier  public  @interface   Stringedinstrument {} 
Import com.hundsun.idol.Instrument; Import com.hundsun.idol.StringedInstrument; @StringedInstrument  Public class Implements Instrument {    @Override    publicvoid  play () {        //     }}

When using @StringedInstrument to qualify the properties of an automatic assembly:

@Autowired @stringedinstrument Private instrument instrument;

Spring will shrink the bean to only the beans that are marked by the @StringedInstrument annotations.

2.2 Standard-based automated assembly with @Inject

2.3 Using an expression in annotation injection

@Value

3 Automatic detection of beans

Adding <context:annotation-config> in configuration helps to completely eliminate <constructor-arg> and <property> elements, but still requires display definition <bean>.

<context:component-scan> elements, in addition to completing the same work as <context:annotation-config>, allow Spring to automatically detect beans and define beans:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 " >    <!--scans the specified packages and sub-packages to find classes that can be registered as Spring beans -    <Context:component-scanBase-package= "Com.hundsun.idol"/></Beans>

3.1 to automatically detect label beans

By default,,<context:component-scan> finds classes that are annotated with stereotype annotations, which are the following special annotations:

    • @Component: Generic stereotype annotations, identified as Spring components
    • @Controller: Identified as Spring MVC Controller
    • @Repository: Identity as Data Warehouse
    • @Service: Identify as a service
    • Use any custom annotations that @Component callout
// The default ID is an unqualified class name  Public class Implements Instrument {} @Component (//  ID display named: Kennypublicclassimplements  Performer {}

3.2 Filter Component Scan

By configuring <context:include-filter> and <context:exclude-fliter> child elements for <context:component-scan>, you can adjust the scan behavior at will.

    • Annotation: Filter Scan uses the class labeled by the specified annotation to specify the annotations to be scanned by the expression property
    • assignable: Filter scans classes derived from the expression property of the specified type
    • LI>ASPECTJ: Filter scans the class that matches the ASPECTJ expression specified by the Expression property
    • custom: Uses a custom TypeFilter implementation class that is specified by the expression property
    • Regex: Filter Scan class name matches the regular expression specified by the Expression property
<base-package= "Com.hundsun.idol">        <!--  Automatic registration of all instrument implementation classes--         <type= "assignable" expression = "Com.hundsun.idol.Instrument" /> </ Context:component-scan >

4 using Spring Java-based configuration

--eof--

<spring combat >3: Minimizing Spring XML configuration

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.