spring和springMVC的上下文
2016-06-22 14:21
706 查看
上下文可以替代注解, 但是注解更方便
package com.tgb.web.controller; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.servlet.support.RequestContextUtils; @Controller public class SpringController { // @Resource(name="springService") // private ISpring springService; @RequestMapping("/spring/get") public String get(HttpServletRequest request){ //spring的上下文 WebApplicationContext ac1 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); //springMVC的上下文 WebApplicationContext ac2 = RequestContextUtils.getWebApplicationContext(request); //通过spring上下文拿到bean //ISpring springService = (ISpring)ac1.getBean("springService"); //通过springMVC上下文拿到bean ISpring springService = (ISpring)ac2.getBean("springService"); System.out.println(springService.get()); return "/success"; } }
相关文章推荐
- Eclipse Maven Jetty集成
- Java代码实现设置系统时间
- Java编程中“为了性能”需做的26件事
- spring mvc中的@PathVariable
- java学习笔记:final关键字 与 array
- 在Spring MVC中测试Servlet3.0异步
- 9个基于Java的搜索引擎框架
- JAVA2048小游戏
- java反射机制
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
- Spring整合Hibernate,事务的几种传播特性,数据库事务隔离级别(脏读幻读不可重复读)
- 【Java并发编程实战】—–synchronized
- Hprose-java之异常处理
- springmvc 上传下载
- 【Java并发编程实战】-----线程基本概念
- @SuppressWarnings 批注 含义和用法
- java自定义注解和运行时靠反射机制获取注解
- java实现标准化考试系统详解(一)-----软件结构介绍
- kafka安装(一)
- Java日历时间处理解析