美文网首页
Spring MethodInvokingFactoryBean

Spring MethodInvokingFactoryBean

作者: slowwalkerlcr | 来源:发表于2019-12-20 16:14 被阅读0次

    作用

      1. 让某个实例的某个方法的返回值注入为Bean的实例
      1. 让某个类的静态方法的返回值注入为Bean的实例

    使用MethodInvokingFactoryBean

    • 使用IDEA Maven项目非常方便的下载源码查看其类的说明信息,在这里非常方便的可以查看到这个方法的一些使用的说明
    • 小测试一下子,简单的就跟着这个使用的作用的两个方法进行使用一下吧
      1、资源如下面创建一个:spring-methodInvoking.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--调用静态方法的返回值作为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">
            <property name="targetObject" ref="sysProps"/>
            <property name="targetMethod" value="getProperty"/>
            <property name="arguments" value="java.version"/>
        </bean>
    </beans>
    

    2、下面是System中的静态方法的返回值Properties包含配置的属性的文件的信息,相当于调用静态方法然后在调用生成的Properties这个Bean的实例的方法的某个属性

      public static Properties getProperties() {
            SecurityManager sm = getSecurityManager();
            if (sm != null) {
                sm.checkPropertiesAccess();
            }
    
            return props;
        }
    

    3、测试

    @Slf4j
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={
            "classpath:spring-methodInvoking.xml"})
    public class MethodTest extends AbstractJUnit4SpringContextTests{
    
    
        @Resource(name = "sysProps")
        public Properties properties;
    
        @Resource(name ="javaVersion")
        public String javaVersion;
    
        @Test
        public void test(){
            log.info(properties.toString());
            log.info(javaVersion.toString());
        }
    }
    

    测试总结

    测试结果如同我们想象的一样,可以将某个方法或者某个具体的类的静态方法进行调用
    因为我们总会调用这个方法,相当于初始化方法呗,对于返回值,你可以随便返回一个Boolean 这个就将这个Boolean的值注入到了
    spring的容器中去了,不过这个不是最好的手段,你可以继承InitializingBean,或者使用注解@PostConstruct,在初始化Bean之前调用这个方法
    但是有些时候不想初始化某个Bean你还是可以这么干的。

    会使用开发中很重要,知其所以然也是很重要(了解这个背后实现的故事)

    • 继承图
      image.png
      刚刚上面说了MethodInvokingFactoryBean将调用方法的返回值注入为Bean,我不注入可以吗?
      可以的,其实就是调用刚刚那个类的父类MethodInvokingBean,调用某个静态的方法,调用某个类的实例的方法都可以
      最后将MethodInvokingBean注入为Bean其实原理是差不多的。
    <bean id="testBean" class="org.springframework.beans.factory.config.MethodInvokingBean">
            <property name="staticMethod" value="com.common.utils.test.MethodInvokingBeanTest.test"></property>
        </bean>
    
    @Slf4j
    public class MethodInvokingBeanTest {
        public static void test(){
            log.info("调用了这个方法");
        }
    }
    
     @Resource(name = "testBean")
        public MethodInvokingBean methodInvokingBean;
        @Test
        public void testMethod(){
            log.info(methodInvokingBean.getTargetMethod());
        }
    
    • 打印结果

    INFO [MethodInvokingBeanTest.java:15] : 调用了这个方法
    INFO [MethodTest.java:34] : test

    参考链接 https://blog.csdn.net/u012881904/article/details/77531689

    相关文章

      网友评论

          本文标题:Spring MethodInvokingFactoryBean

          本文链接:https://www.haomeiwen.com/subject/gbdinctx.html