spring的lookup-method和replaced-method

大家推荐个靠谱的公众号程序员探索之路,公众号内点击网赚获取彩蛋,大家一起加油https://i-blog.csdnimg.cn/blog_migrate/93320939ba8f8b0a898e29429753f496.png ​  

我们 使用spirng注入的时候 一般都是 singleton 依赖 singleton  prototype 依赖 prototype 这个时候
直接设置属性依赖就可以了
但是 当 单例依赖 多例呢  在使用属性依赖的时候 我们 每次都拿到了第一次初始化的bean
解决这个问题有两种方式:
1.每次用到的时候直接从bean容器中取 
2.<lookup-method 标签   作用:替换掉抽象方法的返回值 也就是说 该方法必须有返回值
使用这个标签时 对代码还有要求 
package com.zzh.learning.LookUp;

public class Fruit {
    public void getFruit(){
        System.out.println("getFruit");
    }
}
package com.zzh.learning.LookUp;

import java.util.concurrent.atomic.AtomicInteger;

public class Apple extends Fruit{
    private static AtomicInteger a = new AtomicInteger(0);
    public void getFruit(){
        System.out.println("apple"+a.get());
    }
}
package com.zzh.learning.LookUp;

import java.util.concurrent.atomic.AtomicInteger;

public class Bananer extends Fruit{
    //其实用int就行  哈哈 因为 我们 就起一个main线程来测试 没有并发
    private static AtomicInteger a = new AtomicInteger(0);
    public void getFruit(){
        System.out.println("bananer"+a.get());
    }
}
package com.zzh.learning.LookUp;

// 水果盘,可以拿到水果
public abstract class TestLookUp{
    public void h(){
        this.getFruit().getFruit();
    }
    // 抽象方法获取新鲜水果
    protected abstract Fruit getFruit();
}
    //多例的apple 和 bananer
    <bean id="apple" class="com.zzh.learning.LookUp.Apple" scope="prototype"/>
    <bean id="bananer" class="com.zzh.learning.LookUp.Bananer" scope="prototype"/>
    <bean id="testLookUp" class="com.zzh.learning.LookUp.TestLookUp">
        <lookup-method name="getFruit" bean="apple"/>
    </bean>
    <bean id="testLookUp2" class="com.zzh.learning.LookUp.TestLookUp">
        <lookup-method name="getFruit" bean="bananer"/>
    </bean>
    public static void main(String[] args) {
        // 用我们的配置文件来启动一个 ApplicationContext
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
        System.out.println("context 启动成功");
        // 从 context 中取出我们的 Bean,而不是用 new MessageServiceImpl() 这种方式
        TestLookUp testLookUp = (TestLookUp)context.getBean("testLookUp");
        TestLookUp testLookUp2 = (TestLookUp)context.getBean("testLookUp2");
        testLookUp.h();
        testLookUp2.h();
        TestLookUp testLookUp1 = (TestLookUp)context.getBean("testLookUp");
        TestLookUp testLookUp22 = (TestLookUp)context.getBean("testLookUp2");
        testLookUp1.h();
        testLookUp22.h();
    }
    结果:
    context 启动成功
    apple1
    bananer1
    apple2
    bananer2
<replaced-method 替换方法
    <bean id="apple" class="com.zzh.learning.LookUp.Apple" scope="prototype">
        <replaced-method name="getFruit" replacer="orange"/>
    </bean>
    <bean id="orange" class="com.zzh.learning.LookUp.Orange" scope="prototype"/>
    package com.zzh.learning.LookUp;

import org.springframework.beans.factory.support.MethodReplacer;

import java.lang.reflect.Method;

public class Orange implements MethodReplacer{
    @Override
    public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
        System.out.println("我是来替代apple的");
        return null;
    }
}
package com.zzh.learning.LookUp;

import java.util.concurrent.atomic.AtomicInteger;

public class Apple{
    private static AtomicInteger a = new AtomicInteger(0);

    public void getFruit(){
        System.out.println("apple"+a.get());
    }
}
结果:
我是来替代apple的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值