Using XML files to configure in Spring configuration files is actually asking Spring to execute the corresponding code, for example:
Using the <bean> element is actually to let Spring execute the unparametered or parameter-containing constructor use the <property> element, actually to let Spring execute the setter method once
However, Java programs may also have other types of statements: calling getter methods, calling ordinary methods, accessing Fields of classes or objects, etc. Spring also provides corresponding configuration syntax for such statements:
Call getter method: Use PropertyPathFactoryBean
Calling the Filed value of a class or object: Use FiledRetrievingFactoryBean
Calling a normal method: Use MethodInvokingFactoryBean
Inject the property values of other beans
PropertyPathFactoryBean is used to obtain the property value of the target bean (actually the value returned by calling the getter method). The obtained value can be injected into other beans, or a new bean can be directly defined. See the following configuration file:
<bean id="person"> <property name="age" value="30" /> <property name="son"> <!-- Use nested beans to define property values --> <bean> <property name="age" value="11" /> </bean> </property></bean><bean id="son2"> <!-- The age property is not directly injected, but assigns the age property of the son in person to the age property of son2--> <property name="age"> <!-- Note that the PropertyPathFactoryBean is used here --> <bean id="person.son.age" /> </property></bean>
The properties of Person and Son classes can be seen from the configuration file, which is no longer given. The main program is as follows:
public class Test { public static void main(String args[]) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("age=" + ac.getBean("son2", Son.class).getAge()); }} Output result:
age=11
The property value of a bean instance can not only inject another bean, but also directly define the property value of the bean instance as a bean instance. This is also done through PropertyPathFactoryBean. Add this paragraph to the above configuration file:
<bean id="son1" > <!-- Determine the target bean, indicating which bean the component of son1 comes from --> <property name="targetBeanName" value="person" /> <!-- Determine the attribute, indicating which property of son1 comes from --> <property name="propertyPath" value="son" /></bean>
Execute the Test class above and replace son2 with son1, and the result is the same.
Inject the Field value of other beans
Through the FieldRetrievingFactoryBean class, you can inject the Field values of other beans into other beans, or you can directly define a new bean. Here is the configuration clip:
<bean id="son"> <property name="age"> <bean id="java.sql.connection.TRANSACTION_SERIALIZABLE" /> </property></bean>
The main test program is similar to the above definition, and is no longer provided here, and the execution results are as follows:
age=8
In this configuration, the value of the age of the son object is equal to the value of java.sql.Connection.TRANSACTION_SERIALIZABLE. In the above definition, when defining the FieldRetrievingFactoryBean factory bean, the specified id is not the unique identifier of the Bean instance, but the expression of the Field (that is, the value to be taken out).
Note: Field can be either static or non-static. The Field expression specified by the configuration snippet above is a static Field value, so it can be accessed directly through the class name. If the Field value is non-static, it should be accessed through the beans that already exist in the container - that is, the first phrase of the Field expression should be the beans that already exist in the container.
The Field value can also be defined as a Bean instance, for example, add the following paragraph to the configuration file:
<bean id="age" > <!-- targetClass specifies the target class where the Field is located --> <property name="targetClass" value="java.sql.Connection" /> <!-- targetField specifies the Field name--> <property name="targetField" value="TRANSACTION_SERIALIZABLE" /></bean>
Add the following output to the main program:
System.out.println("age=" + ac.getBean("age"));
The execution result is the same as above.
When using FieldRetrievingFactoryBean to get Field value, you must specify the following two properties:
targetClass or targetObject: Used to specify the target accumulation or target object where the Field value is located. If the Field you want to obtain is static, use targetClass to specify the target accumulation; if the Field is non-static, use targetObject to specify the target object.
targetField: Specifies the Field name of the target class or target object
If the Field is a static Field, there is a more concise way to write it:
<bean id="age" > <!-- value specifies which static domain value of which class--> <property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE" /></bean>
Methods that inject other beans return values
Through the MethodInvokingFactoryBean factory bean, the return value of the target method can be injected into the property value of the bean. This factory bean is used to obtain the return value of the specified method. The method can be either a static method or an instance method; this value can be injected into the specified attribute of the specified bean instance or directly defined as a bean instance. See example:
<bean id="valueGenerator" /><bean id="son1"> <property name="age"> <!-- Get method return value: call the getValue method of valueGenerator--> <bean > <property name="targetObject" ref="valueGenerator" /> <property name="targetMethod" value="getValue" /> </bean> </property></bean>
Here is the ValueGenerator:
public class ValueGenerator { public int getValue() { return 2; } public static int getStaticValue() { return 3;}} The test program still prints the value of age in son1, the code is omitted, and the result is as follows:
age=2
If you want to call a static method, modify the configuration to:
<bean id="son1"> <property name="age"> <!-- Get method return value: call the getStaticValue method of valueGenerator--> <bean > <property name="targetClass" value="com.abc.util.ValueGenerator" /> <property name="targetMethod" value="getStaticValue" /> </bean> </property></bean>
The test results are:
age=3
Since Java supports overloading, only giving the method name is not enough to determine which method to call. The above configuration can be called successfully because neither method in ValueGenerator has parameters. If there are parameters in the method, how to configure it? Include the following content in the configuration file:
<bean id="sysProps" > <property name="targetClass" value="java.lang.System" /> <property name="targetMethod" value="getProperties" /><bean><bean id="javaVersion"> <!-- Point to the above sysProps Bean --> <property name="targetObject" value="sysProps" /> <property name="targetMethod" value="getProperty" /> <!-- Configure parameters here--> <property name="arguments"> <!-- Use list element to list multiple parameters of the calling method --> <list> <value>java.version</value> </list> </property><bean>
In the above example, it is equivalent to calling the getProperty method of java.lang.System with "java.version" as a parameter. The return value will create a bean named javaVersion. That is, equivalent to:
javaVersion = java.lang.System.getProperty("java.version");
Like Field in the previous article, if the method to be called is a static method, there is also a more concise method:
<bean id="myBean"> <!-- Use the staticMethod property to directly specify the target method of the target class--> <property name="staticMethod" value="com.abc.util.ValueGenerator.getStaticValue" /></bean>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.