Spring Framework支持五种作用域,如下图所示:

singleton:表示一个容器中只会存在一个bean实例,无论在多少个其他bean里面依赖singleton bean,整个容器都只会存在一个实例。相当于是容器的全局变量。

prototype:一个容器中可能会存在多个bean实例,prototype bean的实例产生有两种情况,一种是其他bean请求依赖prototype 容器会为其他bean分别创建一个实例。另外一种就是通过ApplicationContextAware 接口的 getBean方法获取 bean的时候 容器也会创建一个新的实例。

request:这个不用多说,即容器会为每一个HTTP请求都会创建一个实例。

session:容器会为每个session创建一个bean实例

那么问题来了,由于bean的作用域不同,其实例创建的时间也不会相同,如果程序中存在一个 singleton bean 依赖了一个 request bean ,直接通过 @Autowired注解项目在启动的时候会报错的,如何解决这个问题呢?Spring 提供了lookup-method方法来解决这个问题。下面来看一个例子:

1 首先新建一个User类

public class User {

private String name;

private String password;

private int age;
//省略setter 和 getter
}
2 将这个User类注册成为 request 作用域的bean 在springContext.xml文件中
<bean id="user" class="com.zsq.cn.login.entity.User" scope="request">
<property name="name" value="zsq" />
</bean>
3 新建一个controller
@Controller
public class LoginController extends BaseException{
@Autowired
private LookupMethodService lookupMethodService;

@RequestMapping("/")
public String home(Model model){
User user = lookupMethodService.getUser();
System.out.println(user.toString());
return "home/index";
}
}
4 新建一个service 以及其实现类
public interface LookupMethodService {

User getUser();
}

@Service
public abstract class LookupMethodServiceImpl implements LookupMethodService {

@Override
public User getUser() {
return creatUser();
}

public abstract User creatUser();
}
5 在springContext.xml文件中配置

<bean id="lookupMethodServiceImpl" class="com.zsq.cn.login.service.impl.LookupMethodServiceImpl">
<lookup-method name="creatUser" bean="user" />
</bean>

通过5 的配置 4中的抽象方法 createUser将返回2中bean定义的一个新的实例。
每次通过1 的请求时容器都会创建一个name=“”zsq“”的新实例。

当然spring提供了一种更加简单的方式来处理作用域不一样时属性注入的问题。

即使用@Scope标签的proxyMode属性。比如我要将一个Student注册为作用域为session的bean,并在单实例的loginController中注入。

1 首先建立一个

@Component
@Scope(value="session",proxyMode=ScopedProxyMode.TARGET_CLASS)
//如果Student是一个类,并没有实现任何接口,那么将proxyMode设置为ScopedProxyMode.TARGET_CLASS,将采用CGLIB代理,
//如果Student实现了一个接口,那么可以将proxyMode设置为ScopedProxyMode.INTERFACES,将采用JDK的动态代理,
public class Student {

private String name;

private int age;

//省略setter、getter
}
如果要在xml文件中实现这一步可以在springContext.xml文件中注入

<bean id="student" calss="com.zsq.cn.login.entity.Student" scope="session">
<property name="name">zsq</property>
<property name="age">22</property>
//下面两个二选一
<aop:scoped-proxy />//如果Student是一个类,并没有实现任何接口这样配置将采用CGLIB代
<aop:scoped-proxy proxy-target-class="false" />//如果Student实现了一个接口,这样配置将采用JDK动态代理
</bean>

2 在loginController中注入student
@Autowired
private Student student;

@RequestMapping("/")
public String home(Model model){
model.addAttribute("user", new User());

student.setAge(22);

return "home/index";
}

原文:https://blog.csdn.net/qq_34310242/article/details/78266035

Spring Bean的作用域以及lookup-method标签的使用的更多相关文章

  1. spring bean 的作用域之间有什么区别

    spring bean 的作用域之间有什么区别? spring容器中的bean可以分为五个范围.所有范围的名称都是说明的, 1.singleton:这种bean范围是默认的,这种范围确保不管接受到多个 ...

  2. Spring Bean的作用域(转)

    Spring Bean的作用域 .singleton  [单例] eg:<bean id="personService" class="com.yinger.ser ...

  3. Spring bean的作用域以及生命周期

    一.request与session的区别 request简介 request范围较小一些,只是一个请求. request对象的生命周期是针对一个客户端(说确切点就是一个浏览器应用程序)的一次请求,当请 ...

  4. spring bean 的作用域

    spring bean 的作用域: 1.单例(singleton):默认是单例模式,也就是说不管给定的bean被注入到其他bean多少次,注入的都是同一个实例. 2.原型(prototype):每次注 ...

  5. Spring bean的作用域和生命周期

    bean的作用域 1.singleton,prototype, web环境下:request,session,gloab session 2.通过scope="" 来进行配置 3. ...

  6. [跟我学spring][Bean的作用域]

    Bean的作用域 什么是作用域呢?即“scope”,在面向对象程序设计中一般指对象或变量之间的可见范围.而在Spring容器中是指其创建的Bean对象相对于其他Bean对象的请求可见范围. Sprin ...

  7. Spring Bean的作用域类型

    Bean的作用域类型 singleton :在Spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在; prototype :每次从容器中调用Bean时,都返回一个新的实例,即每 ...

  8. spring bean的作用域和自动装配

    1 Bean的作用域 l  singleton单列:整个容器中只有一个对象实例,每次去访问都是访问同一个对象  默认是单列 l  prototype原型: 每次获取bean都产生一个新的对象,比如Ac ...

  9. Spring Bean Scope (作用域)

    singleton: 单例模式,针对每个spring容器,只有一个该类的实例被管理,每次调用此实例都是同一个对象被返回,所以适用于无状态bean.默认情况下,singleton作为spring容器中b ...

随机推荐

  1. phpexcel 导出xsl乱码

    在header前面加上 ob_end_clean(); ob_end_clean();//清除缓冲区,避免乱码 header('Content-Type: application/vnd.ms-exc ...

  2. futex的设计与实现

    介绍 futex(快速用户空间互斥)是Linux的一个基础组件,可以用来构建各种更高级别的同步机制,比如锁或者信号量等等,POSIX信号量就是基于futex构建的.大多数时候编写应用程序并不需要直接使 ...

  3. windows下新增项目本地通过git bash推送至远程github

    本地E盘workspace目录下新增了spring-cloud-alibaba-demo项目,还没有编译过,没有target等不需要推送至git的文件,所以就直接用git bash丢到github了. ...

  4. 0010.Regular Expression Matching(H)

    jjc . Regular Expression Matching(hard) Given an input string (s) and a pattern (p), implement regul ...

  5. Docker 安装运行MySQL

    1.镜像主页 https://hub.docker.com/_/mysql 2.拉取5.7版本 docker pull mysql:5.7 3.或者拉取最新8.x版本 docker pull mysq ...

  6. 常见问题:MySQL/排序

    MySQL的排序分为两种,通过排序操作和按索引扫描排序. 按索引顺序扫描是一种很高效的方式,但使用的条件较为严格,只有orderby语句使用索引最左前列,或where语句与orderby语句条件列组合 ...

  7. 【VS开发】从sockaddr中取得客户端或者数据源的Ip地址和端口号

    在socket编程中,服务器端accept()等待一个客户端的连接,当连接成功后,accept拷贝客户端的地址信息到sin_addr里面,我们如何从sin_addr取得此客户端的Ip地址和端口号呢? ...

  8. css 修改placeholder样式

    input::-webkit-input-placeholder{ color:red; } input::-moz-placeholder{ /* Mozilla Firefox 19+ */ co ...

  9. python 爬虫实例(一)

    一个简单的爬虫工程 环境: OS:Window10 python:3.7 安装一些库文件 pip install requests pip install beautifulsoup4 pip ins ...

  10. STL常用

    nth_element(first,nth,last) first,last 第一个和最后一个迭代器,也可以直接用数组的位置. 将第n个大的元素放到nth位置上,左边元素都小于它,右边元素都大于它. ...