Explain in detail the automatic loading method of beans under Java’s spring framework

The spring container can automatically assemble the relationship between collaborative beans, which helps to reduce the need for XML configuration without writing more < constructor Arg > and < property > elements of a large spring based application.

Auto assemble mode: there are the following auto assemble modes that can be used to instruct the spring container to use auto assemble dependency injection. Use the autowire attribute of the < bean / > element to specify the auto assembly mode for a bean definition.

Byname mode this mode specifies the name of the auto assembly attribute. The spring container visually automatically sets the bean in the XML configuration file with the line attribute set to byname. It then attempts to match and wire a bean whose properties are defined by the same name as in the configuration file. If a match is found, it injects these beans, otherwise it throws an exception.

For example, if a bean definition is set to automatically assemble a byname configuration file, it contains the aspellchecker attribute (that is, it has a setspellchecker (...) Method), spring will find a bean definition named spell check and use it to set the property. The < property > tag can still be used to connect the remaining properties. The following example will illustrate this concept.

To create a spring application: This is texteditor Contents of java file:

Here is another related class file, spellchecker Java content:

Here is mainapp Contents of java file:

The following is the normal configuration file beans XML file:

However, if you want to use auto assemble "byname", the XML configuration file will be as follows:

After creating the source code and bean configuration file, let's run the application. If all goes well, this will print the following information:

The bytype schema specifies the type of property by auto assembly. The spring container looks like a bean in an XML configuration file with the autowire property set to bytype. Then, it tries to match and connect a property if its type has a matching profile with exactly the same bean name. If a match is found, it injects these beans, otherwise it throws an exception.

For example, if a bean definition is set to automatically assemble a bytype configuration file, it contains the aspellchecker property of the spell check type. Spring finds a bean definition named spell check and uses it to set the property. You can still wire the remaining properties using the < property > tag. The following example will illustrate this concept. You will find that it is no different from the above example, except that the XML configuration file has been changed.

Similarly, to create a spring Application Description: here is the texteditor Contents of java file:

Here is another related class file, spellchecker Java content:

Here is mainapp Contents of java file:

The following is the normal configuration file beans XML file:

However, if you want to use auto assemble "bytype", the XML configuration file will be as follows:

When the source code and bean configuration file are created, let's run the application. If all goes well, this will print the following information:

This pattern is very similar to bytype, but it applies to constructor parameters. The spring container looks like the autowire property is set to the bean in the XML configuration file. Then, it tries to match and connect the parameters of its constructor with only one bean name configuration file. If a match is found, it injects these beans, otherwise it throws an exception.

For example, if a bean definition is set to assemble automatically by constructing a configuration file, it has a constructor with one of the parameters of spell check type. Spring looks for a bean definition named spellchecker and uses it to set the parameters of the constructor. The < constructor Arg > tag can still be used to wire the remaining parameters. The following example will illustrate this concept. This is texteditor Contents of java file:

Here is another related class file, spellchecker Java content:

Here is mainapp Contents of java file:

The following is the normal configuration file beans XML file:

However, if you want to use automatic assembly by constructor, the XML configuration file will be as follows:

After creating the source code and bean configuration file, let's run the application. If all goes well, this will print the following information:

In addition, there are autodetect and default methods, which will not be discussed in detail here. Limitations of automatic assembly: the best effect of automatic assembly is that it is always used in a project. If auto assembly is not commonly used, it may be confused as that developers can use it to connect only one or two bean definitions. However, auto assembly can significantly reduce the need to specify attributes or constructor parameters, but you should consider the limitations and disadvantages of auto assembly before using them.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>