`
m635674608
  • 浏览: 4937761 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

spring 获取bean 测试

 
阅读更多
package com.jayway.springsessionexample;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.support.RequestContextUtils;
/**
 * webTest 在子容器
 * webTest2 在父容器
 * @author root
 *
 */
public class HelloServlet extends HttpServlet {

    private static final String NAME = "name";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    	
    	
    	
    	/*
    	 * 1.ApplicationContextAware 之能获取父容器中的
    	 * WebTest webTest = (WebTest) SpringContextHelper.getBean("webTest2");
    	System.out.println("webTest2 :"+webTest);
    	
    	
    	WebTest webTest2 = (WebTest) SpringContextHelper.getBean("webTest");
    	System.out.println("webTest :"+webTest2);*/
    	
    	//2.只能获取父容器中的,
    	/*WebTest webTest = (WebTest) ContextLoader.getCurrentWebApplicationContext().getBean("webTest2");
    	System.out.println("webTest :"+webTest);
    	
    	
    	WebTest webTest2 = (WebTest) ContextLoader.getCurrentWebApplicationContext().getBean("webTest");
    	System.out.println("webTest2 :"+webTest2);*/
    	
    	//3.只能获取父容器中的,
    	/*WebTest webTest = (WebTest) WebApplicationContextUtils.getWebApplicationContext(req.getServletContext()).getBean("webTest2");
    	System.out.println("webTest :"+webTest);
    	
    	
    	WebTest webTest2 = (WebTest) WebApplicationContextUtils.getWebApplicationContext(req.getServletContext()).getBean("webTest");
    	System.out.println("webTest2 :"+webTest2);*/
    	
    	
    /*	4. 
     *  WebTest webTest = (WebTest) RequestContextUtils.getWebApplicationContext(req).getBean("webTest2");
    	System.out.println("webTest :"+webTest);
    	
    	
    	WebTest webTest2 = (WebTest) RequestContextUtils.getWebApplicationContext(req).getBean("webTest");
    	System.out.println("webTest2 :"+webTest2);*/
    	
    	
    	/*// 5.WebApplicationObjectSupport  之能获取父容器中的
    	 WebTest webTest = (WebTest) ApplicationContextUtils.getBean("webTest2");
    	System.out.println("webTest2 :"+webTest);
    	
    	
    	WebTest webTest2 = (WebTest) ApplicationContextUtils.getBean("webTest");
    	System.out.println("webTest :"+webTest2);*/
    	
    	
    	
        String name = Optional.ofNullable(req.getSession(false))
                .map(session -> (String) session.getAttribute(NAME))
                .orElse("World");
        String greeting = String.format("Hello %s!", name);
        System.out.println("get====greeting  "+greeting);
        try (ServletOutputStream out = resp.getOutputStream()) {
            out.write(greeting.getBytes(StandardCharsets.UTF_8));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        String name = req.getParameter(NAME);
        System.out.println("post===="+name);
        req.getSession().setAttribute(NAME, name);
    }
}

 

分享到:
评论

相关推荐

    用于获取Spring Bean依赖关系图的工具(高分毕设).zip

    它包括IoC(Inverse of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)等特性,可以简化开发过程、提高代码的可维护性和可测试性。 2. Spring MVC框架:Spring MVC是基于Spring框架的...

    idea +junit单元测试获取不到bean注入的解决方式

    主要介绍了idea +junit单元测试获取不到bean注入的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    struts2+spring+hibernate整合示例

    c 测试类中 主动解析applicationContext.xml ,获取bean 执行dao层方法进行测试 2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts ...

    spring boot实战.pdf高清无水印

    1.1 Spring风云再起 1 1.1.1 重新认识Spring 2 1.1.2 Spring Boot精要 3 1.1.3 Spring Boot不是什么 6 1.2 Spring Boot入门 6 1.2.1 安装Spring Boot CLI 7 1.2.2 使用Spring Initializr初始化Spring ...

    Spring.3.x企业应用开发实战(完整版).part2

    1.7 如何获取Spring 1.8 小结 第2章 快速入门 2.1 实例功能概述 2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 ...

    Spring in Action(第2版)中文版

    3.5使用spring的特殊bean 3.5.1后处理bean 3.5.2bean工厂的后处理 3.5.3配置属性的外在化 3.5.4提取文本消息 3.5.5程序事件的解耦 3.5.6让bean了解容器 3.6脚本化的bean 3.6.1给椰子上lime 3.6.2脚本化bean...

    Spring in Action(第二版 中文高清版).part2

    3.5 使用Spring的特殊Bean 3.5.1 后处理Bean 3.5.2 Bean工厂的后处理 3.5.3 配置属性的外在化 3.5.4 提取文本消息 3.5.5 程序事件的解耦 3.5.6 让Bean了解容器 3.6 脚本化的Bean 3.6.1 给椰子上Lime 3.6.2...

    Spring in Action(第二版 中文高清版).part1

    3.5 使用Spring的特殊Bean 3.5.1 后处理Bean 3.5.2 Bean工厂的后处理 3.5.3 配置属性的外在化 3.5.4 提取文本消息 3.5.5 程序事件的解耦 3.5.6 让Bean了解容器 3.6 脚本化的Bean 3.6.1 给椰子上Lime 3.6.2...

    Spring+3.x企业应用开发实战光盘源码(全)

     第11章:讲解了如何使用Spring JDBC进行数据访问操作,我们还重点讲述了LOB字段处理、主键产生和获取等难点知识。  第12章:讲解了如何在Spring中集成Hibernate、myBatis等数据访问框架,同时,读者还将学习到ORM...

    Spring-Reference_zh_CN(Spring中文参考手册)

    5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 5.4.2.1. 注册用户自定义的PropertyEditor 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 ...

    Spring攻略(第二版 中文高清版).part1

    1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用构造程序创建Bean 14 1.3.1 问题 14 1.3.2 解决方案 14 1.3.3 工作原理 14 1.4 解决构造程序歧义 17 ...

    Spring 2.0 开发参考手册

    5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @...

    Spring Boot实战 ,丁雪丰 (译者) 中文版

    1.1 Spring风云再起 1 1.1.1 重新认识Spring 2 1.1.2 Spring Boot精要 3 1.1.3 Spring Boot不是什么 6 1.2 Spring Boot入门 6 1.2.1 安装Spring Boot CLI 7 1.2.2 使用Spring Initializr初始化...

    干它Spring.pdf

    完成 Spring 的框架雏形后,通过单元测试进行手动操作 Bean 对象的定义、注册和属性填充,以及最终获取对象调用方法..... 以开发简化版Spring学习其原理和内核的知识内容,不仅是代码编写实现也更注重内容上的需求...

    Spring中文帮助文档

    2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving)的支持 2.4. 中间层 2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. 对Websphere ...

    Spring API

    2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving)的支持 2.4. 中间层 2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. 对Websphere ...

    Spring攻略(第二版 中文高清版).part2

    1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用构造程序创建Bean 14 1.3.1 问题 14 1.3.2 解决方案 14 1.3.3 工作原理 14 1.4 解决构造程序歧义 17 ...

    spring chm文档

    5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @...

    Spring3.x企业应用开发实战(完整版) part1

    1.7 如何获取Spring 1.8 小结 第2章 快速入门 2.1 实例功能概述 2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 ...

    Spring.html

    ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时就会创建里面bean的实例,如果是多例在获取使用时才会创建bean实例 ...

Global site tag (gtag.js) - Google Analytics