For convenience and readability, the coupling between the component and component 2 uses profile dependency injection, while the basic type of the member variable is directly in the code settings. Sometimes the value of a bean's property may be the return value of a method, or the field value of a class, or the return value of another object's Get method. Spring supports the return value of any method, the class or the Feild value of the object, and the get return value of the other bean directly defined as a bean in the container.
Get property values for other beans
The value obtained by Propertypathfactorybean to obtain the property value of the other bean (the return value of Get) can be injected to other beans or defined as a new bean. Which get method to invoke which bean is required.
<bean id= "Son1" class= "Org.springframework.beans.factory.config.PropertiesFactoryBean" ><!--target Bean, Tell which Bean to find the Get method--><property name= "Targetbeanname" value= "person"/><property name= "PropertyPath" value = "Son"/></bean>
As the code above identifies, spring takes the son attribute of the person's bean and places the son result in the container as a bean. PropertyPath's value also supports the form of composite attributes, such as replacing son with Son.age, which represents the get method of the age property of the son attribute in the Peroson object. If you want to assign the returned result directly to the other bean. You need to use nested beans at this point.
<bean id= "Tootherbean" class= "Com.cm.Son" ><property name= "age" ><!--provides parameters for the set method of age using nested beans-- <bean id= "Person.son.age" class= "Org.springframework.beans.factory.config.PropertiesFactoryBean"/></ Property></bean>
This takes the age attribute of the Son property of person, because it is a nested bean, which can exist as the property's value attribute, which means that the value of the nested bean is assigned to the age attribute of the Tootherbean bean.
In addition, we have an easier way.
<util:property-path id= "Son1" path= "Person.son"/>
It means that the value of the son attribute of the bean named person in the container is taken out and placed in the container as a son1 bean.
Alternatively, the value of the Age property that represents the son property of the person is assigned to the Tootherbean value of the bean as a return value.
<bean id= "Tootherbean" class= "Com.cm.Son" ><property name= "age" ><util:property-path path= " Person.son.age "/></property></bean>
Get field value
Similar to property values, Rongguo Fieldretrieveingfactorybean class, we can access static field or an instance of an object, the access value can be set to a new bean, or injected into another bean. There are two questions to note: Which class to access, which field of the class.
<bean id= "Son1" class= "Org.springframework.beans.factory.config.FieldRetrievingFactoryBean" ><property Name= "Targetclass" value= "java.sql.Connection"/><property name= "Targetfield" value= "Transaction_ SERIALIZABLE "/></bean>
In addition, the factory bean has a Setstaticfield method that can simultaneously define which class's static field value. So the above code can be written like this:
<bean id= "Son1" class= "Org.springframework.beans.factory.config.FieldRetrievingFactoryBean" ><property Name= "Staticfield" value= "Java.sql.Connection.TRANSACTION_SERIALIZABLE"/></bean>
If you want to assign the above result to a bean, then:
<bean id= "Tootherbean" class= "Com.cm.Son" ><property name= "age" ><bean id= " Java.sql.Connection.TRANSACTION_SERIALIZABLE "Class=" Org.springframework.beans.factory.config.FieldRetrievingFactoryBean "></property></bean>
The id attribute of the nested bean here is the field expression.
Get method return value
You can call the class method of any class by Methodinvokingfactorybean this factory, or an instance method of any object. If the method is returned by a value, you can either generate a new bean directly, or assign a value to a member variable of another bean. Whether you want to call a static method or an instance method, make Targetclass (or targetobject), Targetmethod and arguments (omit this without arguments).
<bean class= "Org.springframework.beans.factory.config.MethodInvokingBean" ><property name= "TargetObject" ref= "JP"/><property name= "Targetmethod" value= "Add"/><property name= "arguments" ><list>< Ref BEAN=JB2/></list></property></bean>
This code is equivalent to Jf.add (JB2), that is, the JF instance calls the Add method, and has a jb2 instance as a parameter.
At this point, we have been able to represent all the code in XML.
Set is through the property element, get is through the Propertypathfactorybean, create the object is the bean element, the normal method as shown above, the field value can also be obtained. But in general we only write code that often needs to be changed in relation to the project upgrade, or those that can increase the coupling.
This article from "Fingertip Light Fly" blog, declined reprint!
Spring note--7. Advanced dependency Configuration