Spring bean

搞了这么久SSM,回过头看看Spring

之前获取一个对象基本就是new,每次调用都会创建一个新的对象;然后Spring IOC搞了个容器存放bean,再次调用会从容器中取bean,这样理论上对象就只创建一次,applicationcontext.getbean(“name”)返回的对象是同一个。

常见的bean配置:

在xml文件里配置<bean id="****" class="com.lee.***" scope="request"/>

自动化装配,先配置扫描路径<context:component-scan base-package="com.lee.impl"/>,使用注解@autowired

bean可以设置自己作用域,配置方式如xml中scope="***",或者在组建声明的@component后加个@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

原文地址:https://www.cnblogs.com/cbxx/p/11934221.html