The content is referenced from the spring in action book.
The behavior of creating collaborative relationships between application objects is often called assembly, which is also the essence of dependency injection.
1. Create a spring configuration
Spring is a container-based framework. If spring is not configured, then it is an empty container, so it is necessary to configure spring to tell the container which beans it needs to load and how to assemble the beans so that they can work with each other.
Starting with spring3.0, the spring container provides two ways to configure the bean. The first is the traditional use of one or more XML files as a configuration file. The second provides a configuration method based on Java annotations.
Let's first say how the XML file is configured:
When you declare a bean in an XML file, the root element of the spring configuration file is the <beans> element that is defined by the spring beans namespace. The following is a typical spring XML configuration file.
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!--Bean declarations Go here -</Beans>
All spring configuration information can be placed within the <beans> element, including the declaration of the <bean> element. But the beans namespace is not the only spring namespace. Spring's core framework comes with 10 namespace configurations. As shown below:
2. Inject Bean properties
There are three ways to inject bean properties in spring, two commonly used, the first being injected through a constructor. The second type is injected through a setter.
The first is injected by the constructor:
Let's look at the simplest constructor injection method, which is injected by default constructor.
<id= "Duke" class= "Com.pcitc.springInAction.Juggler"/ >
Inject simple values through the constructor:
Now, on-demand instantiation, do not go to the default construction method:
<id= "Duke" class= "Com.pcitc.springInAction.Juggler" > <value= "/>" </ Bean >
Summary: You can use the <constructor-arg> element to tell spring additional information when constructing a bean. If you do not configure the <constructor-arg> element, spring uses the default construction method, which is configured with the Value property of <constructor-arg> set to 15, Spring will call another construction method of juggler.
To inject an object reference through a constructor:
<BeanID= "Sonnet29"class= "Com.pcitc.springInAction.Sonnet29" /> <BeanID= "Poeticduke"class= "Com.pcitc.springInAction.PoeticJuggler"> <Constructor-argvalue= " the" /> <Constructor-argref= "Sonnet29" /> </Bean>
Summary: <constructor-arg ref= "sonnet29"/> pass the bean reference to the constructor, and when spring touches the declaration of Sonnet29 and Duke, the logic it executes is essentially the same as the following Java code:
Newnew Poeticjuggler (sonnet29);
To create a bean from a factory method:
What if the bean you want to declare doesn't have a public construction method? The method is to create the bean from the factory method.
Sometimes a static factory method is the only way to instantiate an object. Spring supports the assembly of factory-created beans through the Factory-method property of the <bean> element.
Define a singleton class:
package com.pcitc.springInAction; public class Stage { Stage () {} private static class Stagesingletonholder { static
Stage instance = new Stage (); public static Stage getinstance () { return Stagesingl Etonholder.instance; }}
The stage class does not have a public constructor, which is the only way to create an instance of a class by invoking a specified static method with spring's Factory-method property instead of constructing a method.
This bean is declared under the spring configuration file.
<id= "Thestage" class= "Com.pcitc.springInAction.Stage " Factory-method = "GetInstance" />
First, by setter method injection:
Typically JavaBean properties are private and have a set of accessor methods that exist in the form of setxxx and getxxx. Spring can use the property's set method to configure the value of the property to implement setter method injection.
Brief description:
How to inject simple values:
<id= "Kenny"class= " Com.springinaction.springidol.Instrumentalist "><name=" Song " value=" Jingle Bells "/></bean>
In the spring configuration file, once instrumentalist is instantiated, the,<peoperty> element instructs spring to call the Setsong method to set the value of the song property to "Jingle Bells". It is necessary to note that the type of value values is not based on string types, or numeric types and Boolean types, and spring is automatically converted based on the type of the property value.
To inject an object reference through a setter:
<BeanID= "Kenny2"class= "Com.springinaction.springidol.Instrumentalist">< Propertyname= "Song"value= "Jingle Bells" />< Propertyname= "instrument"ref= "saxophone" /></Bean>
Inject internal beans:
<BeanID= "Kenny"class= "Com.springinaction.springidol.Instrumentalist">< Propertyname= "Song"value= "Jingle Bells" />< Propertyname= "instrument"><Beanclass= "Org.springinaction.springidol.Saxophone" /></ Property></Bean>
Internal beans are defined by directly declaring a <bean> element as a child of the <property> element, and the internal bean is limited to setter injection, and the internal bean can be assembled into the constructor method's entry, as follows:
<BeanID= "Duke"class= "Com.springinaction.springidol.PoeticJuggler"><Constructor-argvalue= " the" /><Constructor-arg><Beanclass= "Com.springinaction.springidol.Sonnet29" /></Constructor-arg></Bean>
Here, an instance of Sonnet29 will be created as an internal bean and passed as a parameter to the Poeticjuggler constructor.
Summary: It is important to note that the internal bean does not have an id attribute. Although it is legal to configure an ID property for an internal bean, it is completely unnecessary because the internal bean is never referenced by name. This also highlights the biggest disadvantage of using internal beans: they cannot be reused. Internal beans are only available for one injection, and cannot be referenced by other beans, and in some ways affect the readability of the spring XML configuration.
Spring Combat Assembly Bean