Bean 的六种作用域
- .
- Bean的作用域
- 属性注入和content获取Bean
-
- 单例作用域:http://127.0.0.1:8080/single1
- [多例作用域: http://127.0.0.1:8080/prototype](#多例作用域: http://127.0.0.1:8080/prototype)
- [请求作用域: http://127.0.0.1:8080/request](#请求作用域: http://127.0.0.1:8080/request)
- [会话作用域: http://127.0.0.1:8080/session](#会话作用域: http://127.0.0.1:8080/session)
- [Application作用域: http://127.0.0.1:8080/application](#Application作用域: http://127.0.0.1:8080/application)
.
Bean的作用域
作用域 | 说明 |
---|---|
singleton | 单例作用域, 每个Spring IoC容器内同名称的bean只有⼀个实例(单例)(默认) |
prototype | 原型作用域,每次使用该bean时会创建新的实例(非单例) |
request | 请求作用域 ,每个HTTP 请求生命周期内, 创建新的实例(web环境中) |
session | 会话作用域 ,每个HTTP Session生命周期内, 创建新的实例(web环境中) |
application | 全局作用域,每个ServletContext生命周期内, 创建新的实例(web环境中) |
websocket | HTTP WebSocket 作用域 ,每个WebSocket生命周期内, 创建新的实例(web环境中) |
java
package com.example.demo.config;
import com.example.demo.model.User;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.annotation.ApplicationScope;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.context.annotation.SessionScope;
@Configuration
public class BeanConfig {
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public User single1(){
return new User();
}
@Bean
public User single2(){
return new User();
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public User prototype(){
return new User();
}
@Bean
@RequestScope
public User request(){
return new User();
}
@Bean
@SessionScope
public User session(){
return new User();
}
@Bean
@ApplicationScope
public User application(){
return new User();
}
}
java
package com.example.demo.controller;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private User single1;
@Autowired
private User single2;
@Autowired
private User prototype;
@Autowired
private User request;
@Autowired
private User session;
@Autowired
private User application;
@Autowired
ApplicationContext applicationContext;
@RequestMapping("/single1")
public String single1(){
User user = (User) applicationContext.getBean("single1");
return user.toString()+single1.toString();
}
@RequestMapping("/single2")
public String single2(){
User user = (User) applicationContext.getBean("single2");
return user.toString()+single2.toString();
}
@RequestMapping("/prototype")
public String prototype(){
User user = (User) applicationContext.getBean("prototype");
return user.toString()+prototype.toString();
}@RequestMapping("/request")
public String request(){
User user = (User) applicationContext.getBean("request");
return user.toString()+request.toString();
}@RequestMapping("/session")
public String session(){
User user = (User) applicationContext.getBean("session");
return user.toString()+session.toString();
}@RequestMapping("/application")
public String application(){
User user = (User) applicationContext.getBean("application");
return user.toString()+application.toString();
}
}
属性注入和content获取Bean
我们可以通过浏览器可以访问上述代码中的url,每个请求都至少请求两次
可以得到以下结论:
单例作用域:http://127.0.0.1:8080/single1
多次访问, 得到的都是同⼀个对象, 并且 @Autowired 和applicationContext.getBean() 也是同⼀个对象.
多例作用域: http://127.0.0.1:8080/prototype
applicationContext.getBean()每次获取的对象都不⼀样,属性注入的对象在Spring容器启动时, 就已经注入了, 所以多次请求也不会发生变化
请求作用域: http://127.0.0.1:8080/request
在每⼀次请求中, @Autowired 和 applicationContext.getBean() 都是同⼀个对象.
但是每次请求, 都会重新创建对象
会话作用域: http://127.0.0.1:8080/session
在⼀个session中, 多次请求, 获取到的对象都是同⼀个
换⼀个浏览器访问, 发现会重新创建对象.(另⼀个Session)
Application作用域: http://127.0.0.1:8080/application
在⼀个应用中, 多次访问都是同⼀个对象,即使是不同的浏览器