Spring中Bean的作用域是什么?如何理解Bean的作用域的概念?

一个bean的实例实质就是一个类的实例
那个类的实例呢?总得指明一下吧
而通常实例的名字要给出吧
而通常实例的名字就是类名字的首字母小写
<bean id="commandManager" class="com.wiley.beginningspring.ch2.CommandManager"
      scope="singleton">
</bean>

//阅读知道:singleton是不用写的

           设置了scope="singleton"或不设置scope的bean实例称为无状态实例!!!

<bean id="command" class="com.wiley.beginningspring.ch2.Command"
      scope="prototype">
</bean>

两个bean都配置了scope
scope可以取两个值:singleton和prototype
scope的意思是范围
sinleton和prototye分别代表两个范围
范围是从哪里到哪里?
我们知道存在两个类:CommandManager和Command
貌似singleton的范围囊括prototype的范围


第一层观察:
public class CommandManager implements ApplicationContextAware{
    private ApplicationContext applicationContext;
    //定义execute()方法
    //为什么在一个无参的同名的方法中调用另一个有参的同名的方法?
    //如果你在思考这个问题,那么就进行了错误的思考
    //这里定义的方法是那个类的方法?说出来!CommandManager类!!
    //createCommand()方法是创建Command类的一个实例
    //所以这是什么?
    //在本类定义的方法中以调用别的类的方法为逻辑
    //所以该方法的逻辑不是自定义的而是调用别的类的
    //注意两个类的实例的作用域是不同的
    //commandManager实例的作用域是sinleton
    //command实例的作用域是prototype
    //思考是一个发散和整合大量信息的过程,所以会很花时间
    public void execute(){
        //createCommand()方法哪里定义的?
        //execute(parameter)方法哪里定义的?
        createCommand().execute(new CommandContext());
        //CommandConext()在哪里定义的?
    }

    @Override
    //重写了谁的方法?被实现者类
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
        this.applicationContext = applicationContext;
    }
    //createCommand方法在这里定义
    //本类中的方法可以直接访问本类中的属性
    private Command createCommand(){
        return applicationContext.getBean(Command.class);
    }

}


public class Command implements ApplicationContextAware{
    
    private ApplicationContext applicationContext;
    //定义一个方法
    public void execute(){
        createCommand().execute(new CommandContext());
    }
    //重写一个set方法
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)throws BeansException{
        this.applicationContext = applicationContext;
    }
    //这里的this是指当前类啊

    //定义一个create方法
    //该方法的特殊之处是在本类中调用
    
    private Command createCommand(){
        
       return applicatonContext.getBean(Command.class);
      
    } 
    //观察以上代码可知,无非就是想在本类中调用Command的execute方法
    //为此不得不获取一个实例
    //那为什么不直接New一下这个Command类呢?
    //为什么要用bean的显示查找呢?
    //这里要用到两个知识:1 Bean的依赖项是在Bean创建的时候被注入的 
                        2 Singleton作用域的Bean的创建和其依赖项的注入只会进行一次
    这两个知识信息
    在一起
    再结合其他的信息
    产生了一个这样的知识:
    依赖的Bean只会被创建一次
    当我基于这个sinleton实例多次调用execute()方法时,
    都是在同一个prototype实例上调用execute(new CommandContext())方法
    那如果我们不想在同一个实例上调用这个方法
    而是信息基于一个新的依赖bean实例怎么办呢?
    上述显示查找就是一种解决方法啊
}

<beans xmlns="" xmlns:xsi="" xmlns:aop="" xsi:schemaLocation="">
    //bean的标准配置 id+class
    <bean id="userPreferences" class="com.wiley.beginningspring.ch2~CAUserPreferences">
        <aop:scoped-proxy/>
    </bean>

</beans>

<web-app>

    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        <listener-class>
    </listener>

</web-app>


@Component
@Scope("protoype")
public class Command{


}

@Component
@Scope(value="session",proxyMode=ScopedProxyMode.INTERFACES)
public class UserPreferences{
//...
}


共同点是两个Class上都加了两个注解


注解@Scope可以通过()进行配置
可配置value proxyMode
value可以配置session prototype
如果只配置value
那么value=可以省略

猜你喜欢

转载自blog.csdn.net/weixin_42204641/article/details/81486768