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

2020-07-30 18:37:16 浏览数 (2)

代码语言:javascript复制
通常bean都是单例的,如果一个bean需要依赖另一个bean时,被依赖的bean始终为单例的

让自定义bean获得applicationContext的能力

代码语言:javascript复制
org.springframework.context.ApplicationContextAware

public interface ApplicationContextAware extends Aware {

	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

继承ApplicationContextAware

代码语言:javascript复制
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

代码语言:javascript复制
	<?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. 定义替换者
代码语言:javascript复制
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
代码语言:javascript复制
    <bean id="repl" class="com.shimeath.test.demo7.test.Repl"/>
  1. 替换方法
代码语言:javascript复制
    <bean id="b" class="com.shimeath.test.demo7.test.b">
        <replaced-method name="getA1" replacer="repl"/>
    </bean>

完整xml

代码语言:javascript复制
<?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>

0 人点赞