springMVC(13) springMVC和spring上下文关系

本文探讨了Spring MVC框架如何利用Spring上下文实现组件之间的交互,并通过实例展示了如何在Spring MVC上下文中获取并操作Spring Bean。同时介绍了Spring配置的小技巧,即在团队开发环境中通过导入标签来统一管理配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springmvc上下文继承于spring,也就是springmvc的上下文可访问spring上下文,在springmvc的上下文中可取得spring bean.

SpringController

package com.xdy.controller.annotation;

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 {
	@RequestMapping("/spring/get")
	public String get(HttpServletRequest request) {
		// spring上下文
		WebApplicationContext ac1 = WebApplicationContextUtils
				.getWebApplicationContext(request.getSession()
						.getServletContext());

		//springMVC的上下文
		WebApplicationContext ac2 = RequestContextUtils
				.getWebApplicationContext(request);
		
//		ISpring springManager = (ISpring) ac1.getBean("springManager");
		ISpring springManager = (ISpring) ac2.getBean("springManager");
		springManager.get();
		return "/success";
	}
}

spring配置小技巧:import标签

团队开发时,各自维护自己的spring配置文件,这时就可以使用一个公用的spring来进行导入就可以了。

<import
		resource="classpath*:com/xdy/controller/annotation/springAnnotation-import.xml" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值