spring注入方式之singleton注入prototype

singleton注入prototype
  • lookup方法注入
    使用场景:一个singleton的bean使用一个prototype的bean时。

    public interface Bean2{//jiekou 
        Bean1 getBean1();//定义一个方法
    }
    
    <bean id="bean1"  class="..." scope="prototype" />
    <bean id="bean2" class ="" >
        <lookup-method  name="getXXX" bean="car"/>
    </bean>
    
    这样配置后,就相当于如下的代码:
    public class Bean2Impl implements Bean2,ApplicationContextAware{
        private ApplicationContext ctx;
        public Bean1 getBean1(){
            return (Bean1)ctx.getBean("bean1");
        }
    
        public void setApplicationContext(ApplicationContext ctx) throws BeanException{
            this.ctx=ctx;
        }
    
    }
    

猜你喜欢

转载自blog.csdn.net/Jatham/article/details/82630777
今日推荐