SPRING_IOC (2)

Source: Internet
Author: User

Spring provides several techniques that can help us reduce the number of XML configurations

Automated assembly: helps reduce or even eliminate configuration <property> elements and <construct-arg> elements, allowing spring to automatically identify how the bean's dependencies are assembled.

Automatic detection: More than automatic assembly, it allows spring to automatically identify which classes need to be configured for the spring bean, thus reducing the use of <bean> elements.

4 Types of automatic assembly

Byname,bytype,constructor,autodetect

1,byname Automatic Assembly

<BeanID= "One"class="......">< Propertyname= "Song"value= "Zhangsan"/>< Propertyname= "instrument"ref= "both"/></Bean>

<BeanID= "instrument"class="......"/>

<BeanID= "One"class="......"Autowire= "ByName">< Propertyname= "Song"value= "Zhangsan"/></Bean>

2,bytype Automatic Assembly

Autowire= "Bytype"

Identify a preferred bean for automatic assembly the primary property defaults to True

<id= "One"  class= "..."  primary= "false "/>

Exclude certain beans for automatic assembly setting the Autowire-candidate property to False

<id= "One"  class= "..."  autowire-candidate= " False "/>

3,constructor Automatic Assembly

Constructors

<id= "One"  class= "..."  autowire  = "constructor"/>

4,autodetect Automatic Assembly

First use constructor to assemble automatically, and if you do not find a bean that matches the constructor, spring will attempt to automatically assemble it using Bytype

<id= "One"  class= "..."  autowire= "AutoDetect"  />

can also be mixed with assembly and display assembly

<BeanID= "One"class="......"Autowire= "Bytype">< Propertyname= "Song"value= "Zhangsan"/>< Propertyname= "instrument"ref= "saxophone"/></Bean>

Assembling with annotations

<context:annotation-config/>

The SPRING3 supports several different annotations for automatic assembly:

@atutowired annotations of *spring;

@inject annotation of *jsp-330;

@resource annotation of *jsp-250;

1, using @autowired

Not only can you use it to label setter methods, but you can also annotate any method that requires automatic assembly of bean references

@Autowired (Required=false) @Qualifier ("...")

2, using @inject

Unlike @autowired, the dependency relationship that is marked by the @inject annotation must exist without the required attribute, and an exception is thrown if it does not exist.

@Inject @named ("...")

Automatically detect beans

Use the <context:component-scan> element instead of the <context:annotation-config> element

< Context:component-scan Base-package ="......" > </ Context:component-scan >

By default <context:component-scan> find classes annotated with stereotype annotations, these special annotations are as follows

@Component-generic stereotype annotation that identifies the type as a spring component

@Controller-Identity defines the class as Springmvc Controller

@Repository-Identifies the class defined as a data warehouse

@Service-Identifies the class defined as a service

Any custom annotations that use @component annotations

Filter Component Scan

We can configure <comtext:include-filter> and/or <context:exclude-filter> child elements for <context:component-scan>

<Context:component-scanBase-package="..."><Context:include-filtertype= "assignable"expression="..."/><Context:exclude-filtertype= "Annotation"expression="......"/></Context:component-scan>

Using spring Java-based configuration

Create a Java-based configuration

< Context:component-scan Base-package ="..." />

Defining a Configuration Class

@Configuration

Declaring a simple bean

@bean

SPRING_IOC (2)

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.