When managing our classes with spring, we sometimes want some attribute values to come from some configuration files, system attributes, or the results of some method calls, which can be injected using the spring Propertyplaceholderconfigurer class for the first two ways. The content has already been mentioned in the previous article and is not repeated here. Here's a description of the third situation, which actually provides a solution to this requirement in spring, That's using the Org.springframework.beans.factory.config.MethodInvokingFactoryBean class to generate the properties of the bean that needs to be injected, and here's an example
Mybean.java an ordinary Pojo class.
Import Org.apache.commons.lang.builder.ToStringBuilder; public class Mybean {private string name; private string javaversion, public string GetName () {return name;} public Voi D setname (string name) {this.name = name;} public String getjavaversion () {return javaversion;} public void Setjavaver Sion (String javaversion) {this.javaversion = javaversion} @Override public String toString () {return TOSTRINGBUILDER.R Eflectiontostring (this); } }
Mybeannameprovider.java provides a static method for generating the Name property of the Mybean class, which is also possible with non-static methods, referring to the Javaversion attribute definition in this example
public class Mybeannameprovider {public static String GetName () {return "" + System.currenttimemillis ();}}
Spring.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.0.xsd "default-lazy-init=" True > <bean id= "Mybean" class= "Mybean" > <property name= "Name" ><ref local= "Mybeanname"/></property> <property name= "javaversion" ><ref local= "JavaVersion" "/></property> </bean> <bean id=" Sysprops "class=" Org.springframework.beans.factory.config.MethodInvokingFactoryBean "> <property name=" Targetclass "> < value>java.lang.system</value> </property> <property name= "Targetmethod" > <value> Getproperties</value> </property> </BEAN> <bean id= "javaversion" class= "Org.springframework.beans.factory.config.MethodInvokingFactoryBean" > < Property name= "TargetObject" > <ref local= "sysprops"/> </property> <property name= "Targetmethod" > <value>getProperty</value> </property> <property name= "Arguments" > <list> < value>java.version</value> </list> </property> </bean> <bean id= "Mybeanname" class= " Org.springframework.beans.factory.config.MethodInvokingFactoryBean "> <property name=" Staticmethod ">< Value>mybeannameprovider.getname</value></property> </bean> </beans>
Test.java a test class
Import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {public static void main (string[] args) {ApplicationContext ctx = new Classpathxmlapplicationcontext (" Spring.xml "); Mybean Mybean = (Mybean) ctx.getbean ("Mybean"); System.out.println (Mybean); } }
Running the test class, you can see that the two properties of Mybean are injected into the class through the spring configuration.