Spring Automatic Assembly default-autowire = "byname"

Source: Internet
Author: User

Let's sort out the automatic assembly autowire section of spring today. Here we want to solve the following problems:

1. What is automatic assembly?
2 What is the significance of automatic assembly?
§ 3 what types of automatic assembly are available?
§ 4 how to enable automatic assembly?
§ 5 will Automatic Assembly cause problems?

1. What is automatic assembly?

The Spring IoC container can automatically assemble (autowire) the association between beans that collaborate with each other. Therefore, if possible, spring can automatically let us specify the bean co-author (other dependent beans) by checking the content in beanfactory ).

In short, it means that we do not need to configure which class is used for the other beans referenced in the bean. Spring's automatic assembly can help us complete these tasks.

2 What is the significance of automatic assembly? It is important to reference and understand the advantages and disadvantages of automatic assembly. The benefits include:

Automatic Assembly can significantly reduce the number of configurations. However, using the bean template (see here) can achieve the same purpose.

Automatic Assembly allows you to synchronously update configurations with Java code. For example, if you need to add a dependency to a Java class, the dependency will be automatically implemented without modifying the configuration. Therefore, it is strongly recommended to use automatic assembly during the development process, and change to the explicit Assembly mode when the system becomes stable.

§ 3 what types of automatic assembly are available? Five modes described in spring reference
No autowiring is not used by default. The "<ref/>" label must be used to explicitly specify bean collaborators and provide greater protection for deployment.

Control and clear.

Byname is automatically assembled Based on the attribute name. This option will check the container and find the bean exactly the same as the property according to the name, and

Dynamic assembly. For example, in the bean definition, set autowire to by name, and the bean contains the master attribute (both provide

Setmaster (...) method), spring will find the bean definition named master and use it to assemble the attributes to the master.

Bytype if the container contains a bean of the same type as the specified property, it is automatically assembled with the property. If there are multiple

Bean, then an exception is thrown, and it is pointed out that the bytype method cannot be used for automatic assembly. If no matching bean is found,

Then nothing happens, and the attributes are not set. If you do not want this, you can set
Dependency-check = "objects" causes spring to throw an exception.

The constructor and bytype methods are similar, except that they are applied to the constructor parameters. If the parameter class of the constructor is not found in the container

Bean with the same type will throw an exception.

Autodetect determines whether to use constructor or bytype for automatic assembly through the introspection mechanism of bean classes.

If the default constructor is found, the bytype method is used.

§ 4 how to enable automatic assembly?
You can refer to the following configuration to enable automatic assembly reference <beans xmlns = "http://www.springframework.org/schema/beans"

Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: AOP = "http://www.springframework.org/schema/aop"
Xmlns: Tx = "http://www.springframework.org/schema/tx"
Xsi: schemalocation ="
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"

Default-autowire = "bytype">

Of course, you can change the bytype here to another assembly type you want.

§ 5 will Automatic Assembly cause problems? Some disadvantages of automatic assembly:

Although automatic assembly is more amazing than explicit assembly, as mentioned above, spring tries its best to avoid speculation when the Assembly is ambiguous, unexpected results may occur due to unclear assembly, and the association between objects managed by spring cannot be clearly documented.

For tools that generate documents based on the spring configuration file, automatic assembly will make these tools unable to generate dependency information.

Automatic Assembly can reduce the configuration workload, but also make the configuration file less readable, because you cannot
It is difficult to maintain the dependency between these objects!
Note:
When automatic assembly is performed based on the type, multiple bean definitions in the container may match the setter method and the constructor parameter type. In this way, there will be ambiguity. If the bean definition is not unique, an exception is thrown during assembly.

Solution (optional ):
1. Discard automatic assembly and use display assembly.
2. Exclude beans from automatic assembly and reference two functions:
1. explicitly set the 'autowire-candidate' attribute in the bean definition to 'true' or 'false' to set whether it is automatically assembled.

Object.
2. Use bean name pattern matching to restrict automatic assembly. The method is in the <beans/> element
Set the 'default-autowire-candidates' attribute. Wildcard characters can be used, for example, beans ending with 'repository,

You can set it to "* repository".

3. Set the 'primary' attribute to 'true' in the bean definition to set the bean as the preferred automatic assembly bean.
How to Use spring autowire depends on your project design.

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.