---
title: "spring注入servlet"
author: "g21121"
date: "2012-03-08T14:17:06Z"
tags: []
categories: ["企业架构"]
slug: "spring-servlet"
draft: false
lastmod: "2012-03-08T14:17:06Z"
id: 1446327
url: "https://286.iteye.com/blog/1446327"
---

传统的配置方法是无法将bean或属性直接注入到servlet中的，配置代理servlet亦比较麻烦，这里其实有比较简单的方法，其实就是在servlet的init()方法中加入要注入的内容：

```
ServletContext application = getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);// 获取spring的context
xxxInterface= (xxxInterfaceImpl) wac.getBean("xxxInterface");
```

 

这样就把xxxInterfaceImpl注入进来了。