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的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,除了使用注解方式,我们还可以使用XML配置来实现Spring Lookup Method。下面是一个使用XML配置的示例: 首先,定义一个抽象类: ``` public abstract class AbstractBean { public void doSomething() { getDependency().execute(); } public abstract Dependency getDependency(); } ``` 接下来,定义一个具体的子类: ``` public class ConcreteBean extends AbstractBean { private Dependency dependency; @Override public Dependency getDependency() { if (dependency == null) { dependency = createDependency(); } return dependency; } protected Dependency createDependency() { // 返回一个新的Dependency对象 } } ``` 注意到这里的`createDependency()`方法没有被`@Lookup`注解标记,这是因为我们将会在XML配置这个方法。 接下来,我们需要在XML配置这个类: ``` <bean id="concreteBean" class="com.example.ConcreteBean"> <lookup-method name="createDependency" bean="dependency"/> </bean> <bean id="dependency" class="com.example.Dependency"/> ``` 注意到这里的`lookup-method`元素,它告诉Spring在运行时为`createDependency()`方法生成一个代理,并将其返回值注入到`ConcreteBean`对象。 现在,我们可以在其他类注入`concreteBean`对象,并调用它的`doSomething()`方法,它将会使用`ConcreteBean`的`createDependency()`方法来获取一个新的`Dependency`对象,并执行它的`execute()`方法。 这就是使用XML配置的Spring Lookup Method的一个简单示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值