Spring(19) 协调不同作用域的bean

  1. 这个标题是照着课本来的,但是理解了以后就是给那些  单例模式的bean A  在 每次 运行  其中  需要 非单例模式的bean B  的方法的时候,每次都传一个新的 非单例模式bean B 的实例进去。
  2. 实现的方法是在配置文件中,将需要不同bean B的方法放在lookup-metho标签中。并且将需要的非实例化bean存进去
  3. 代码在这里~~~
    package calleePackage;
    
    /**
     * 这个是被非单例模式的bean
     */
    public class Callee {
    
        private String calleeName;
    
        public String getCalleeName() {
            return calleeName;
        }
    
        public void setCalleeName(String calleeName) {
            this.calleeName = calleeName;
        }
    
    
    
    
    }
    
    package callerPackage;
    
    import calleePackage.Callee;
    
    /**
     * 这个是单例模式的bean
     */
    
    public class Caller {
    
        private Callee callee;
    
        public Callee getCallee() {
            return callee;
        }
    
        public void setCallee(Callee callee) {
            this.callee = callee;
        }
    
        public String whoIsMyCallee(){
            return "my callee is " + getCallee().toString();
        }
    }
    
    <?xml version="1.0" encoding="GBK"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    
        <!--就是通过lookup-method标签来让spring知道哪个方法需要新的非单例模式的bean啦~~-->
        <bean id="caller" class="callerPackage.Caller">
            <lookup-method name="getCallee" bean="callee"/>
        </bean>
    
    <!--spring在默认情况下是将bean设置为单例模式的bean的。要显式指定scope="prototype"
    才会得到非单例模式的bean-->
        <bean id="callee" class="calleePackage.Callee" scope="prototype">
            <property name="calleeName" value="lala"/>
        </bean>
    
    </beans>
    package testPackage;
    
    import callerPackage.Caller;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringTest {
        public static void main(String []args){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            Caller caller1 = applicationContext.getBean("caller", Caller.class);
            Caller caller2 = applicationContext.getBean("caller", Caller.class);
            System.out.println(caller1 == caller2);
    //        caller1和caller2是相等的。
    
            System.out.println(caller1.whoIsMyCallee());
            System.out.println(caller2.whoIsMyCallee());
    
            System.out.println(caller1.getCallee() == caller2.getCallee());
    //        caller1的callee和caller2的callee是不等的
        }
    }
    //运行程序可以看到输出
    //        true
    //        my callee is calleePackage.Callee@1972e513
    //        my callee is calleePackage.Callee@533bda92
    //        false
    

    这是我看李刚编著的《轻量级javaEE企业应用实战(第五版)-Struts2+Spring5+Hibernate5/JAP2》后总结出来的。

猜你喜欢

转载自blog.csdn.net/weixin_39452731/article/details/84859345