前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring——单例Bean中使用多例Bean

Spring——单例Bean中使用多例Bean

作者头像
shimeath
发布2020-07-30 18:37:16
8890
发布2020-07-30 18:37:16
举报
通常bean都是单例的,如果一个bean需要依赖另一个bean时,被依赖的bean始终为单例的

让自定义bean获得applicationContext的能力

org.springframework.context.ApplicationContextAware

public interface ApplicationContextAware extends Aware {

	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

继承ApplicationContextAware

public static class b implements ApplicationContextAware {
        a a1;

        public void say(){
            a1 = this.getA1();
            System.out.println(a1);
            System.out.println(this);
        }

        public a getA1() {
            return context.getBean(a.class);

        }
		//定义一个变量存放context
        private ApplicationContext context;
        //设置context的值
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.context = applicationContext;
        }
    }

lookup-method实现

以上的方法对spring的api耦合过高,通过lookup-method方式解决

在bean中配置

通过对方法拦截。name为拦截方法名,bean为替换返回值的bean的id

	<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="a" class="com.shimeath.test.demo7.test.a" scope="prototype"/>
    <bean id="b" class="com.shimeath.test.demo7.test.b">
        <lookup-method name="getA1" bean="a"/>
    </bean>

</beans>

replaced-method方法替换

通过对bean中的某一方法进行拦截,将请求转发到替换者处理

  1. 定义替换者
public static class Repl implements ApplicationContextAware, MethodReplacer{
        @Override
        public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
            return this.context.getBean(a.class);
        }

        ApplicationContext context;

        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.context = applicationContext;
        }
    }
  1. 定义bean
    <bean id="repl" class="com.shimeath.test.demo7.test.Repl"/>
  1. 替换方法
    <bean id="b" class="com.shimeath.test.demo7.test.b">
        <replaced-method name="getA1" replacer="repl"/>
    </bean>

完整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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="repl" class="com.javacode2018.lesson001.demo7.test.Repl"/>
    <bean id="a" class="com.javacode2018.lesson001.demo7.test.a" scope="prototype"/>
    <bean id="b" class="com.javacode2018.lesson001.demo7.test.b">
        <replaced-method name="getA1" replacer="repl"/>
    </bean>

</beans>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 让自定义bean获得applicationContext的能力
  • lookup-method实现
    • 在bean中配置
    • replaced-method方法替换
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档