Turn: Spring's automatic Assembly Autowire

Source: Internet
Author: User
Tags aop constructor documentation xmlns

http://tjc.javaeye.com/blog/341430

Today to tidy up the Spring Automatic assembly Autowire section, here we want to solve the following problems:
§1 What is automatic assembly?
What is the meaning of §2 automatic assembly?
§3 are there several types of automatic assembly?
§4 How do I enable automatic assembly?
§5 will the problem be caused by automatic assembly?

§1 What is automatic assembly?
Spring reference wrote the spring container is able to autowire relationships between collaborating beans. This means, it is
Possible to automatically let Spring resolve collaborators (other beans) for your beans by inspecting the contents
of the beanfactory.
Referencing the Spring IOC container can automatically assemble (autowire) the association between the beans that are collaborating with each other. So, if possible, spring automatically lets you specify the bean's collaborators (other dependent beans) by checking the contents of beanfactory.
In short, the automatic assembly of Spring can help us do this by not requiring us to configure which class to use for other beans referenced in the bean.

What is the meaning of §2 automatic assembly?
Spring reference wrote It was important to understand the various advantages and disadvantages of autowiring. Some Advantages of
Autowiring include:
autowiring can significantly reduce the volume of configuration required. However, mechanisms such as the
Use of a bean template (discussed elsewhere in this chapter) is also valuable in this regard.
autowiring can cause configuration to keep itself up to date as your objects evolve. For example, if you need
To add an additional dependency to a class, which dependency can be satisfied automatically without the need
To modify configuration. Thus There may is a strong case for autowiring during development, without ruling
The option of switching to explicit wiring is the code base becomes more stable.
It is important to understand the pros and cons of automatic assembly. Some of the advantages include:

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

Automatic assembly allows the configuration to be synchronized with Java code updates. For example, if you need to add a dependency to a Java class, the dependency will be automatically implemented without the need to modify the configuration. Therefore, it is highly recommended to use automatic assembly in the development process and to change the mode of explicit assembly when the system tends to stabilize.

§3 are there several types of automatic assembly?
Spring reference wrote
Mode explanation
No no autowiring at all. Bean references must be defined via a REF element. This is the default,
And changing this was discouraged for larger deployments, since explicitly specifying collaborators gives
Greater control and clarity. To some extent, it's a form of documentation about the structure of a system.

ByName autowiring by property name. This option would inspect the container and look for a bean named exactly
The same as the property which needs to be autowired. For example, if you had a bean definition which is
Set to Autowire by name, and it contains a master property (which is, it has a setmaster (..) method), Spring
Would look for a bean definition named Master, and use it to set the property.

Bytype allows a property to being autowired if there is exactly one bean of the property type in the
Container. If there is more than one, a fatal exception are thrown, and this indicates so you
May isn't use Bytype autowiring for that Bean. If there is no matching beans, nothing
happens; The property was not set. If This is not desirable, setting the Dependency-check= "Objects"
Attribute value specifies that an error should is thrown in the this case.

Constructor this was analogous to bytype and applies to constructor arguments. If there isn ' t exactly one
Bean of the constructor argument type in the container, a fatal error is raised.

AutoDetect chooses constructor or bytype through introspection of the Bean class. If a default
Constructor is found, the Bytype mode would be applied.
Reference
Mode description
No by default, autowiring is not used. You must explicitly specify the bean collaborators by using the "<ref/>" tab for the deployment to give greater
Control and clarity.

ByName is automatically assembled according to the attribute name. This option examines the container and looks for the bean that exactly matches the property, based on its name, and
Assembly. For example, in the bean definition, Autowire is set to by name, and the bean contains the Master property (which also provides
Setmaster (..) method), spring looks for the bean definition named Master and uses it to assemble it to the Master property.

Bytype If there is a bean in the container that is the same as the specified property type, it will be automatically assembled with that property. If there is more than one type of
Bean, the exception is thrown and the automatic assembly cannot be done using the Bytype method. If a matching bean is not found,
Then nothing happens and the property is not set. If you do not want this, then you can set the
Dependency-check= "Objects" lets spring throw an exception.

constructor is similar to Bytype, except that it is applied to constructor parameters. If the constructor parameter class is not found in the container
The same type of bean, then an exception will be thrown.

AutoDetect uses the Bean class's introspection mechanism (introspection) to determine whether automatic assembly is performed using constructor or Bytype mode.
If the default constructor is found, then the Bytype method is used.


§4 How do I enable automatic assembly?
You can use 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, here's the bytype you can change to the other type of assembly you want.

§5 will the problem be caused by automatic assembly?
Spring reference wrote
Some Disadvantages of Autowiring:
autowiring is more magical than explicit wiring. Although, as noted in the above table, Spring was careful to
Avoid guessing in case of ambiguity which might has unexpected results, the relationships between your
Spring-managed objects is no longer documented explicitly.
wiring information may is available to tools, may generate documentation from a Spring container.
Reference
Some disadvantages of automatic assembly:

Although automatic assembly is more magical than explicit assembly, as mentioned above, spring will try to avoid guessing when the assembly is ambiguous, because the assembly is ambiguous and can have unpredictable results, and the association between the objects that spring manages can no longer be clearly documented.

For tools that generate documents based on spring configuration files, automatic assembly will make it impossible for these tools to generate dependency information.

Automatic assembly reduces the amount of configuration work, but also makes the configuration file less readable because you cannot get from the configuration file
It is difficult to maintain a dependency between these objects.
Attention:
When automated assembly by type, there may be multiple bean definitions in the container that match the setter method and constructor parameter types for automatic assembly. There is a question of ambiguity. If the bean definition is not unique, an exception is thrown when assembled.
Solution (optional):
1 Discard the use of automatic assembly, using display assembly.
2 exclude beans from automatic assembly,
Reference
Two functions:
1 set it to be automatically assembled by setting the ' Autowire-candidate ' property in the bean definition to ' true ' or ' false ' explicitly
Object.
2 The automatic assembly is restricted using pattern matching of the bean name, in the way that the <beans/> element
Set in the ' Default-autowire-candidates ' property. You can use wildcard characters, such as beans that end with ' Repository ',
Then you can set it to "*repository".

3 Set the bean as the preferred automatic assembly bean by setting the ' primary ' property to ' true ' in the bean definition.
How to use Spring Autowire depends on the design of your project.

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.