When we use spring to manage our classes, sometimes we want some attribute values to come from some configuration files, system attributes, or some method call results, for the first two usage methods, you can use spring's propertyplaceholderpolicer class for injection.ArticleAs mentioned in, we will not repeat it here. Here we will provide some explanations for the third case. In fact, in spring, we provide a solution to this requirement, that is, using Org. springframework. beans. factory. config. the methodinvokingfactorybean class is used to generate the attributes of the bean to be injected. The following is an example.
Mybean. Java is a common pojo class.
Import Org. apache. commons. lang. builder. tostringbuilder; <br/> public class mybean {<br/> private string name; <br/> private string javaversion; </P> <p> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> Public String getjavaversion () {<br/> return javaversion; <br/>}< br/> Public void setjavaversion (string javaversion) {<br/> This. javaversion = javaversion; <br/>}< br/> @ override <br/> Public String tostring () {<br/> return tostringbuilder. reflectiontostring (this); <br/>}< br/>
Mybeannameprovider. Java provides a static method to generate the name attribute of the mybean class. Likewise, non-static methods can also be used. Refer to the javaversion attribute definition in this example.
Public class mybeannameprovider {<br/> Public static string getname () {<br/> return "" + system. currenttimemillis (); <br/>}< br/>}
Spring. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xmlns: AOP = "http://www.springframework.org/schema/aop" <br/> 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 <br/> http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" <br/> default-Lazy-init = "true"> <br/> <bean id = "mybean" class =" mybean "> <br/> <property name =" name "> <ref local =" mybeanname "/> </property> <br/> <property name =" javaversion "> <ref local = "javaversion"/> </property> <br/> </bean> <br/> <bean id = "sysprops" class = "org. springframework. beans. factory. config. methodinvokingfactorybean "> <br/> <property name =" targetclass "> <br/> <value> JAVA. lang. system </value> <br/> </property> <br/> <property name = "targetmethod"> <br/> <value> getproperties </value> <br/> </property> <br/> </bean> <br/> <bean id = "javaversion" class = "org. springframework. beans. factory. config. methodinvokingfactorybean "> <br/> <property name =" targetobject "> <br/> <ref local =" sysprops "/> <br/> </property> <br/> <property name = "targetmethod"> <br/> <value> getproperty </value> <br/> </property> <br/> <property name = "arguments"> <br/> <list> <br/> <value> JAVA. version </value> <br/> </List> <br/> </property> <br/> </bean> </P> <p> <bean id = "mybeanname" class = "org. springframework. beans. factory. config. methodinvokingfactorybean "> <br/> <property name =" staticmethod "> <value> mybeannameprovider. getname </value> </property> <br/> </bean> <br/> </beans>
Test. Java is a test class.
Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. classpathxmlapplicationcontext; <br/> public class test {<br/> Public static void main (string [] ARGs) {<br/> applicationcontext CTX = new classpathxmlapplicationcontext ("Spring. XML "); <br/> mybean = (mybean) CTX. getbean ("mybean"); <br/> system. out. println (mybean); <br/>}< br/>
Run the test class and you can see that both attributes of mybean are injected into the class through spring configuration.