当前位置: 首页 > wzjs >正文

易优建站不同网站的主机和域名

易优建站,不同网站的主机和域名,百度刷搜索词,新闻热点素材要测试 Spring 默认是否管理 Request 和 Session 作用域的 Bean 的生命周期,可以通过以下步骤实现: 验证 Spring 是否创建了 Bean:检查 Spring 容器是否成功加载并管理了 Request 和 Session 作用域的 Bean。验证 Bean 的生命周期回调方法是…

要测试 Spring 默认是否管理 RequestSession 作用域的 Bean 的生命周期,可以通过以下步骤实现:

  1. 验证 Spring 是否创建了 Bean:检查 Spring 容器是否成功加载并管理了 RequestSession 作用域的 Bean。
  2. 验证 Bean 的生命周期回调方法是否被调用:通过实现生命周期接口(如 InitializingBeanDisposableBean)或使用注解(如 @PostConstruct@PreDestroy),验证初始化和销毁逻辑是否被调用。
  3. 验证 Bean 的作用域行为
    • 对于 Request 作用域的 Bean,每次 HTTP 请求都会创建一个新的实例。
    • 对于 Session 作用域的 Bean,每个 HTTP Session 都会创建一个新的实例。

以下是一个完整的代码示例,展示如何测试 Spring 默认是否管理 RequestSession 作用域的 Bean 的生命周期。


1. 项目结构

src
├── main
│   ├── java
│   │   └── com.example
│   │       ├── MyRequestScopedBean.java
│   │       ├── MySessionScopedBean.java
│   │       └── AppConfig.java
│   └── resources
└── test└── java└── com.example└── RequestSessionBeanTest.java

2. 示例代码

2.1 MyRequestScopedBean.java

这是一个 Request 作用域的 Bean,定义了生命周期回调方法。

package com.example;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;@Component
@Scope(WebApplicationContext.SCOPE_REQUEST) // 定义为 Request 作用域
public class MyRequestScopedBean {public MyRequestScopedBean() {System.out.println("MyRequestScopedBean: Constructor called");}@PostConstructpublic void postConstruct() {System.out.println("MyRequestScopedBean: @PostConstruct - Initialization logic");}@PreDestroypublic void preDestroy() {System.out.println("MyRequestScopedBean: @PreDestroy - Cleanup logic");}public void doSomething() {System.out.println("MyRequestScopedBean: Doing something...");}
}

2.2 MySessionScopedBean.java

这是一个 Session 作用域的 Bean,定义了生命周期回调方法。

package com.example;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;@Component
@Scope(WebApplicationContext.SCOPE_SESSION) // 定义为 Session 作用域
public class MySessionScopedBean {public MySessionScopedBean() {System.out.println("MySessionScopedBean: Constructor called");}@PostConstructpublic void postConstruct() {System.out.println("MySessionScopedBean: @PostConstruct - Initialization logic");}@PreDestroypublic void preDestroy() {System.out.println("MySessionScopedBean: @PreDestroy - Cleanup logic");}public void doSomething() {System.out.println("MySessionScopedBean: Doing something...");}
}

2.3 AppConfig.java

这是一个 Spring 配置类,启用组件扫描。

package com.example;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}

2.4 RequestSessionBeanTest.java

这是测试类,验证 RequestSession 作用域的 Bean 的生命周期。

测试代码
package com.example;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;@SpringBootTest
@AutoConfigureMockMvc
public class RequestSessionBeanTest {@Autowiredprivate MockMvc mockMvc;@Autowiredprivate MyRequestScopedBean requestScopedBeanProxy; // 注入代理对象@Autowiredprivate MySessionScopedBean sessionScopedBeanProxy; // 注入代理对象@Testpublic void testRequestScopedBean() throws Exception {// 模拟 HTTP GET 请求mockMvc.perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get("/testRequest")).andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status().isOk());}@Testpublic void testSessionScopedBean() {// 创建一个模拟的 HTTP 请求MockHttpServletRequest request = new MockHttpServletRequest();RequestContextHolder.setRequestAttributes(new org.springframework.web.context.request.ServletRequestAttributes(request));try {// 获取 SessionScopedBean 实例MySessionScopedBean sessionBean1 = sessionScopedBeanProxy; // 通过代理获取sessionBean1.doSomething();// 模拟新的 Sessionrequest.getSession().invalidate(); // 销毁当前 SessionMockHttpServletRequest newRequest = new MockHttpServletRequest();RequestContextHolder.setRequestAttributes(new org.springframework.web.context.request.ServletRequestAttributes(newRequest));MySessionScopedBean sessionBean2 = sessionScopedBeanProxy; // 获取新的 SessionScopedBean 实例sessionBean2.doSomething();} finally {// 清理请求上下文RequestContextHolder.resetRequestAttributes();}}
}

2.5 控制器代码

为了测试 RequestSession 作用域的 Bean,可以创建一个简单的控制器。

MyController.java
package com.example;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MyController {private final MyRequestScopedBean requestScopedBean;@Autowiredpublic MyController(MyRequestScopedBean requestScopedBean) {this.requestScopedBean = requestScopedBean;}@GetMapping("/testRequest")public String testRequestScopedBean() {System.out.println("Accessing RequestScopedBean in controller");requestScopedBean.doSomething();return "RequestScopedBean tested successfully!";}
}

3. 输出结果

3.1 测试 RequestScopedBean

运行 testRequestScopedBean() 方法后,控制台会输出类似以下内容:

MyRequestScopedBean: Constructor called
MyRequestScopedBean: @PostConstruct - Initialization logic
Accessing RequestScopedBean in controller
MyRequestScopedBean: Doing something...
MyRequestScopedBean: @PreDestroy - Cleanup logic
说明
  • 每次 HTTP 请求都会创建一个新的 RequestScopedBean 实例。
  • 请求结束后,@PreDestroy 方法会被调用,销毁该实例。

3.2 测试 SessionScopedBean

运行 testSessionScopedBean() 方法后,控制台会输出类似以下内容:

MySessionScopedBean: Constructor called
MySessionScopedBean: @PostConstruct - Initialization logic
MySessionScopedBean: Doing something...
MySessionScopedBean: @PreDestroy - Cleanup logic
MySessionScopedBean: Constructor called
MySessionScopedBean: @PostConstruct - Initialization logic
MySessionScopedBean: Doing something...
MySessionScopedBean: @PreDestroy - Cleanup logic
说明
  • 第一次请求时,创建了一个 SessionScopedBean 实例。
  • 销毁当前 Session 后,再次请求时会创建一个新的 SessionScopedBean 实例。

4. 验证 RequestSession 的作用域行为

4.1 验证 RequestScopedBean 的独立实例

在测试代码中,可以通过多次请求验证 RequestScopedBean 是否每次都创建新的实例。

示例
mockMvc.perform(get("/testRequest"));
mockMvc.perform(get("/testRequest"));

控制台会输出两次 MyRequestScopedBean: Constructor called,说明每次请求都会创建一个新的实例。


4.2 验证 SessionScopedBean 的独立实例

在测试代码中,可以通过销毁 Session 后再次请求,验证 SessionScopedBean 是否创建新的实例。

示例
request.getSession().invalidate(); // 销毁当前 Session
MockHttpServletRequest newRequest = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(newRequest));

控制台会输出两次 MySessionScopedBean: Constructor called,说明每次 Session 都会创建一个新的实例。


5. 注意事项

5.1 RequestScopedBeanSessionScopedBean 的依赖注入

  • 在 Spring 中,RequestScopedBeanSessionScopedBean 是动态代理的,因此不能直接注入其实例。
  • 如果需要手动获取 RequestScopedBeanSessionScopedBean,可以通过 @Autowired 注入一个代理对象,并通过 RequestContextHolder 获取当前请求的 Bean。
示例
@Autowired
private MyRequestScopedBean requestScopedBeanProxy;@Test
public void testRequestScopedBeanProxy() {// 手动获取当前请求的 RequestScopedBean 实例RequestAttributes attributes = RequestContextHolder.getRequestAttributes();if (attributes instanceof ServletRequestAttributes) {ServletRequestAttributes servletAttributes = (ServletRequestAttributes) attributes;MyRequestScopedBean actualBean = (MyRequestScopedBean) servletAttributes.getRequest().getAttribute("requestScopedBean");actualBean.doSomething();}
}

5.2 测试环境的要求

  • RequestScopedBeanSessionScopedBean 是 Web 环境下的作用域,因此测试时需要确保 Spring 的 Web 环境已正确加载。
  • 使用 @SpringBootTest 注解可以加载完整的应用上下文。
  • 如果只测试 Web 层,可以使用 @WebMvcTest 注解。

6. 总结

如何测试 RequestSession 作用域的 Bean 的生命周期?

  1. 验证 Bean 的创建

    • 检查构造方法是否被调用。
    • 检查 @PostConstruct 注解的方法是否被调用。
  2. 验证 Bean 的作用域行为

    • 对于 RequestScopedBean,每次 HTTP 请求都会创建一个新的实例。
    • 对于 SessionScopedBean,每个 HTTP Session 都会创建一个新的实例。
  3. 验证 Bean 的销毁

    • 对于 RequestScopedBean,销毁逻辑会在请求结束后自动调用。
    • 对于 SessionScopedBean,销毁逻辑需要手动实现(因为 Spring 不管理 Session 的生命周期)。

通过上述测试代码和验证方法,可以清楚地了解 Spring 如何管理 RequestSession 作用域的 Bean 的生命周期。


文章转载自:

http://8dFVfwIa.ydwsg.cn
http://Uf57NSUc.ydwsg.cn
http://CZIsFLqI.ydwsg.cn
http://1fFBU1vz.ydwsg.cn
http://2QotdBLn.ydwsg.cn
http://uivDvpE8.ydwsg.cn
http://F6A2OwOP.ydwsg.cn
http://7qENsr36.ydwsg.cn
http://IV9AbYuE.ydwsg.cn
http://usCURodK.ydwsg.cn
http://CuubL4q6.ydwsg.cn
http://Cmqpnlae.ydwsg.cn
http://fvqfPrZ0.ydwsg.cn
http://qb2Ei5Pb.ydwsg.cn
http://GOiymRvV.ydwsg.cn
http://OaRe7UKk.ydwsg.cn
http://Eipb0v9Y.ydwsg.cn
http://bFHZagAw.ydwsg.cn
http://0Tdg8Wnd.ydwsg.cn
http://2fhC4k3i.ydwsg.cn
http://Tq28NzrF.ydwsg.cn
http://qdekNLPP.ydwsg.cn
http://k3GvjTmD.ydwsg.cn
http://jvL8HrqI.ydwsg.cn
http://mK6yehCW.ydwsg.cn
http://FCgMjCq2.ydwsg.cn
http://U3nbYBbB.ydwsg.cn
http://7dMxF4Gj.ydwsg.cn
http://OjbqVMpE.ydwsg.cn
http://a29SIYGZ.ydwsg.cn
http://www.dtcms.com/wzjs/709117.html

相关文章:

  • 简单的网站建设找哪个公司seo整站优化一年价格多少
  • 安监网站安全建设信息网站怎么做响应式
  • 松江做微网站十大互联网培训机构
  • 视频网站开发 视频采集公司网站设计欣赏
  • 百度的官方网站wordpress数据库查询数据库
  • 个人作品展示网站高新西区网站建设
  • 张家界seo网站优化深圳网站设计 创同盟
  • 石家庄网站建设接单中铁建设集团有限公司董事长
  • 科技部网站建设合同做视频网站视频存放在哪里
  • 网站建设的会计分录室内设计软件排行榜
  • 杭州模板开发建站罗湖中心区做网站
  • 建设网站的不足装修设计费收费标准2022
  • 杭州网站的优化中铁建设集团门户网站
  • 梅州建站怎么做装修网站设计图推荐
  • 南通网站建设策划浙江网站建站
  • 抓取网站后台网站续费模版
  • 想看外国的网站怎么做想学软件编程 哪个学校好啊
  • 安卓网站客户端制作软件网站建设报告论文
  • 一级a做爰片免费网站国产黄页号码是什么意思啊?
  • 好大夫官方网站网上预约挂号东莞网站设计开发
  • 在哪个网站做ppt模板赚钱对比网页设计网站
  • 泉州茶叶网站建设东阳实惠营销型网站建设厂家
  • 网站迁移教程wordpress钉钉登陆
  • 小程序定制公司外包赣州seo外包怎么收费
  • t恤定制网站微信网页版无法登录
  • 淘宝店铺网站建设wordpress cms社交
  • 手机相册备份网站源码搜索引擎优化包括哪些方面
  • 西安大型网站开发seo深度优化服务
  • 3d网站制作品牌网站建设需要哪些规划
  • 乐都网站建设哪家好wordpress粘贴word