Detailed explanation of XML installation and configuration bean in spring bean assembly tutorial

preface

As we all know, when spring first appeared, XML was the main way to describe configuration. Under the name of spring, we created countless lines of XML code. To some extent, spring has become synonymous with XML.

Now, with the emergence of powerful automatic configuration and Java code configuration, XML is no longer the only choice and should not be the first choice. Learn XML configuration and use it more to maintain the existing XML configuration. I won't say much now. Let's take a look at the detailed introduction.

Create XML configuration specification

Before using XML configuration, you need to create a new configuration specification, just as javaconfig requires us to create a class with @ configuration annotation. In XML configuration, you need to create a new XML configuration file with < beans > element as the root.

As you can see, the basic XML configuration is much more complex than javaconfig. The most basic XML element used to assemble beans is contained in the spring beans schema, which is defined as the root namespace in the above XML file< Beans > is an element in this pattern, which is the root element of all spring configuration files.

Declare a simple < bean >

Use another element in the spring beans pattern: < bean >, which is similar to the @ bean annotation in javaconfig.

Because there is no explicit given ID, the bean will be named according to the fully qualified class name. In this case, the bean ID will be "soundsystem. Sgtpeppers #0". "#0" is a form of counting, which is used to distinguish other beans of the same type. If there is another one, it is "#1".

Although the automatic bean naming method is convenient, a slight reference will think that the automatically generated name is of little use. Therefore, the best way is to use the ID attribute to set a name of your choice for each bean:

It can be seen that XML is no longer directly responsible for creating an instance of sgtpeppers. In the configuration based on javaconfig, we need to instantiate it ourselves. When spring finds the < bean > element, it calls the default constructor of sgtpeoppers to create the bean by default.

Initializing beans with constructor injection

In the spring XML configuration, there is only one way to declare beans: use elements and specify class attributes. If the ID is declared in the XML, there will be a variety of configuration schemes. Specifically, the injection of the constructor is divided into two basic configuration schemes:

The constructor injects bean references. Now we have declared the compactdisc bean, and the sgtpeppers class implements the compactdisc interface, so we actually have a bean that can be injected into the CDPlayer bean. All we need to do is declare CDPlayer in XML and reference compactdisc through ID:

As an alternative, you can also use spring's c-namespace. The c-namespace, introduced in spring 3.0, is a more concise way to describe constructor parameters in XML. To use it, you need to declare its schema at the top of the XML:

After that, we can use it to declare the constructor parameters, as follows:

Obviously, the c-namespace approach is much more concise. Specifically, see the following writing method: "C:" represents the prefix of c-namespace, "CD" represents the constructor parameter name (if you forget, you can see: component scanning and automatic assembly of spring assembly bean), "- ref" represents the injected bean reference.

The CD here directly refers to the name of the constructor parameter, which can be implemented as an alternative by using the position information of the parameter in the whole parameter list.

This c-namespace representation looks more weird than the above. We use "0" to represent the parameter name, that is, the index of the parameter. Because numbers are not allowed as the first character in XML, underscores are used as prefixes. Using the index to identify constructor parameters is better than using the name. As long as the same order is saved, you can rename the parameter name at will. This method is more useful if there are multiple construction parameters. Because there is only one parameter now, you can even use no identification.

This will be the weirdest representation.

Inject literal into constructor

The di we did earlier usually refers to type assembly, that is, to assemble the reference of an object into other objects that depend on it, and sometimes only a literal configuration object needs to be introduced. Now you need to create a new compactdisc implementation:

In sgtpeppers, all parameters are hard coded, but the compactdisc implementation is different, so the XML configuration will be different:

We again inject constructor parameters with the < constructor Arg > element. But this time, instead of using the "ref" attribute, we use the value attribute, which indicates that the given is to be injected into the constructor in the form of literal quantity.

The following is implemented in the form of c-namespace:

It can be seen that the difference between assembly literal and assembly reference is that the suffix "- ref" is removed from the attribute name. Similarly, it is implemented by parameter index:

In terms of assembly bean reference and literal value, the functions of the two implementations are the same. However, there is a case where < constructor Arg > can be implemented, but c-namespace cannot.

Assembly set

Suppose a list type parameter is added to the blankdisc constructor, as follows:

When declaring beans, we must prepare a list in advance. The simplest way is to set it to null directly.

What the < null / > element does is pass null as a parameter to the constructor, but this is not the solution to the problem. Although the injector can execute normally, an exception will occur when calling the play () method.

For a reasonable processing scheme, we can use elements to declare it as a list:

The < list > element is a child of < constructor Arg >, which indicates that a list containing values will be passed to the constructor. Where < value > represents each element. Similarly, instead of < value >, we can implement the assembly of bean reference list.

In terms of assembly set, < constructor Arg > has more advantages than the attributes of c-namespace. The current way of using c-namespace can not realize the function of assembly set.

set a property

All the previous classes are completely injected through the constructor without using the setter method of attributes. Let's take a look at using spring XML to implement attribute injection. Suppose the attribute is injected into CDPlayer as follows:

Here is a question to consider. Is it constructor injection or attribute injection? As a general rule, construct dependencies are used for strong dependencies, and attribute injection is used for optional dependencies. The previous blankdisc is strongly dependent, so constructor injection is the right solution. However, for CDPlayer, the dependence on compactdisc cannot be strong, because even if compactdisc is not loaded, CDPlayer still has some functions.

Therefore, this is feasible:

There will be no problems when creating beans, but exceptions will occur when CDPlayer starts working. At this point, we can inject the compactdisc attribute of CDPlayer.

The < property > element provides the same function for the setter method of the attribute as the < constructor Arg > element provides for the constructor. In this example, it introduces a bean with ID of compactdisc (through ref attribute) and injects it into compactdisc attribute. Similarly, spring provides an alternative to p-namespace for < property >. Introduce p-namespace:

Using the p-namespace, assemble the compactdisc attribute:

Attributes in the namespace follow a naming convention similar to those in the c-namespace.

Inject literals into properties

Property can also inject literals, much like constructor parameters. We configure the blankdisc bean through attribute injection instead of constructor:

Now it doesn't force us to assemble any properties.

Of course, these properties are not set during assembly, and the CD cannot be played normally, so attribute injection is still implemented through the value attribute of the < property > element.

It can be seen that the < constructor Arg > element assembly tracks is exactly the same as before. Another alternative is to implement the p-namespace attribute:

Like c-namespace, the only difference between assembly bean reference and assembly literal is whether it has "- ref" suffix. The same p-namespace does not realize the function of assembly collection.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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
分享
二维码
< <上一篇
下一篇>>