spring MethodInvokingFactoryBean xml配置示例详解

1:调用静态方法的返回值作为bean
2:调用实例的返回值注入bean
3:为静态方法注入bean

直接cooy一下java类跟spring的配置,运行以便于你理解这三种配置方式
如果想研究源码的话就自行百度了。
带有静态属性方法的类

package AspectTest;

import java.util.Properties;

public class BeanTest {
	public static Properties properties ;

	public static Properties getProperties() {
		return properties;
	}

	public static void setProperties(Properties properties) {
		BeanTest.properties = properties;
	}
	public static void main(String[] args) {
		BeanTest.properties.getProperty("java.version");
	}

}

测试类

package AspectTest;  
  
import java.util.Date;
import java.util.Properties;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;  


  

public class Client {  
    public static void main(String[] args) {  

    	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/AspectTest/spring-demo4.xml");
    	
    

    	Properties p=(Properties) context.getBean("sysProps");
    System.out.println(	p.getProperty("java.version"));
    
   System.out.println( context.getBean("javaVersion"));
   System.out.println( context.getBean("javaVersion"));
   
 System.out
		.println( BeanTest.properties.getProperty("java.version"));;
   
   
   
   
    
    }  
}  

xml配置

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"  
       xmlns:p="http://www.springframework.org/schema/p"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.1.xsd   
    http://www.springframework.org/schema/aop   
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" default-autowire="byName">  
    <aop:aspectj-autoproxy />  
    <context:component-scan base-package="AspectTest"/>  
    
       <!--调用静态方法的返回值作为bean-->
    <bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="java.lang.System"/>
        <property name="targetMethod" value="getProperties"/>
    </bean>
    <!-- 调用实例的返回值注入bean -->
      <bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"  scope="prototype">
        <property name="targetObject" ref="sysProps"/>
        <property name="targetMethod" value="getProperty"/>
        <property name="arguments" value="java.version"/>
    </bean>

<!-- 为静态方法注入bean -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
	    <property name="staticMethod" value="AspectTest.BeanTest.setProperties"/>
	    <property name="arguments" ref="sysProps"/>
	</bean>
    <!-- 如果目标对象实现了接口 默认情况下会使用动态代理实现AOP,也可以强制使用CGLIB
1)加入 CGLIB库
2)在配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/> -->
</beans>  

控制台输出:

1.7.0_80
DEBUG - Creating instance of bean 'javaVersion'
DEBUG - Returning cached instance of singleton bean 'sysProps'
DEBUG - Invoking afterPropertiesSet() on bean with name 'javaVersion'
DEBUG - Finished creating instance of bean 'javaVersion'
1.7.0_80
DEBUG - Creating instance of bean 'javaVersion'
DEBUG - Returning cached instance of singleton bean 'sysProps'
DEBUG - Invoking afterPropertiesSet() on bean with name 'javaVersion'
DEBUG - Finished creating instance of bean 'javaVersion'
1.7.0_80
1.7.0_80
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值