The Org.springframework.beans.factory.config.MethodInvokingFactoryBean of spring combat

Source: Internet
Author: User

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> &LT;/BEAN&GT <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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.