Spring combat through the XML assembly bean

Source: Internet
Author: User

Although spring has long been associated with XML, it is now important to be clear that XML is no longer the only option to configure spring. Spring now has a powerful automation configuration and Java-based configuration, and XML should no longer be your first choice. However, given that there are already so many XML-based spring configurations, it is important to understand how to use XML in spring. However, I hope that the content of this section is just to help you maintain your existing XML configuration, and hopefully you will use the Automation configuration and Javaconfig when you complete the new spring work.

1.1 Creating an XML Configuration specification

Before using XML to assemble beans for spring, you need to create a new configuration specification. When using javaconfig, this means creating a class with @configuration annotations, whereas in an XML configuration, this means creating an XML file with the <beans> element as its root.

It is easy to see that this basic XML configuration is much more complex than the Javaconfig class of the same functionality. As a start, all you need in Javaconfig is @configuration, but when using XML, you need to declare multiple XML Schema (XSD) files at the top of the configuration file that define the XML elements that configure spring. An easy way to create and manage a spring XML configuration file with Spring Tool Suite is to use Spring Toolsuite (https://spring.io/tools/sts). In the Spring Tool Suite menu, select the file>new>spring Bean configuration file to create the Springxml profile, and you can select the available configuration namespaces.

1.2 Declaration of a simple <bean>

To declare a bean in an XML-based spring configuration, we use another element in the Spring-beans pattern:<bean>. The <bean> element is similar to the @bean annotation in Javaconfig. We can declare compactdiscbean in the following way:

This declares a very simple bean, the class that created the bean is specified by the class attribute, and the fully qualified class name is used. Because there is no explicitly given ID, the bean will be named according to the fully qualified class name. In this case, the Bean's ID will be "soundsystem." Sgtpeppers#0 ". Wherein, "#0" is a count of the formTo distinguish between other beans of the same type. If you declare anotherSgtpeppers is not explicitly identified, the automatically obtained ID willis "Soundsystem." Sgtpeppers#1 ". Although an automated bean naming method is convenient, if you want to reference it later,The auto-generated name will not be of much use. So, generally speaking, a better approachBy using the id attribute, set a name of your choice for each bean:

1.3 Initializing beans with constructor injection

In the spring XML configuration, there is only one way to declare a bean: Use the <bean> element and specify the class attribute. Spring will get the necessary information from here to create the bean. However, when you declare di in XML, there are a number of optional configuration schemes and styles. Specific to

constructor injection, there are two basic configuration options to choose from:

constructor Injection Bean referenceAccording to the current definition, Cdplayerbean has a type of compactdisc that acceptsconstructor. In this way, we have a good scenario to learn how to inject beans into theuse. The Sgtpeppers Bean is now declared, and the Sgtpeppers class implements theCompactdisc interface, so in fact we've got one that can injectto the Bean in the Cdplayerbean. All we need to do is sound in the XML .CDPlayer and reference sgtpeppers by ID:

when spring encounters this <bean> element, it creates a cdplayer real example. The <constructor-arg> element tells spring to pass a bean reference with ID Compactdisc to the CDPlayer constructor.

as an alternative scenario, you can also use the C-namespace of spring. The C-namespace is introduced in Spring 3.0, which is a more concise way to describe constructor parameters in XML . To use it, you must declare its schema at the top of the XML, as shown below

After C-namespaces and schema declarations, we can use it to declare constructor parameters
, as shown below:

so far, the di we do usually refers to the type of assembly- that is, the object are assembled into other objects that depend on them-and sometimes we need to do is to configure the object with a literal value. To illustrate this, suppose you want to create a new implementation of Compactdisc, as follows:

in Sgtpeppers, the name of the record and the artist's name are all hard-coded, but this a COMPACTDISC implementation differs from the other, it is more flexible. Like a real empty disk , it can be set to any artist and album name you want. Now, we can put

The existing sgtpeppers is replaced with this class:

we use the <constructor-arg> element again to inject the constructor parameters. But this time we did not use the "ref" attribute to refer to other beans, but instead used the Value property, which indicates that the given value is to be injected into the constructor in literal form . What should this example look like if you were to use a C-namespace? The first case is the name of the reference constructor parameter:

in terms of assembling bean references and literal values,<constructor-arg> and C-named null The function is the same. But there is a situation where <constructor-arg> can do that, and C-namespaces are not. Next, let's take a look at how the collection

Assembly into the constructor parameters.

First, you can use the <list> element to declare it as a column Table

When the element in the list is a reference type

1.4 Setting properties

so far, the CDPlayer and Blankdisc classes have been injected entirely through the constructor , there is no setter method that uses attributes. Next, let's look at how to implement attribute injection using Spring XML. Suppose the cdplayer of the attribute injection is as follows:

<property> The function provided by the setter method for the attribute of the element The same functionality as the <constructor-arg> element provides for the constructor. In This example, it references a bean with ID compactdisc (via the ref attribute) and injects it into the Compactdisc property (via the Setcompactdisc () party law). If you run the test now, it should be able to pass.

we already know that spring provides a C-named null for the <constructor-arg> element similar to the alternative, Spring provides a more concise P-namespace, as an alternative to the <property> element. In order to enable the P-namespace, you must declare it with other namespaces in the XML file:

We can use the P-namespace to assemble the Compactdisc property in the following way

However, blankdisc this time completely through attributes
Injection is configured instead of constructor injection. The new Blankdisc class is as follows:

Spring combat through the XML assembly bean

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.