如何使用spring将service层注入到servlet中去(how to use Spring to inject ur service layer into the servlet )
2011-04-13 13:59
841 查看
In a typical struts+spring framework, we know how to inject our “service”
into the “action”. But sometime we have to use the “servlet”.
I mean the real servlet, not the struts’s action-servlet!
For example:
We have a servlet name is “UserServlet”, we want to inject the service
“MyTaskService” into it as following sample shows:
public class userServlet extends HttpServlet {
@Resource
MyTaskService myTaskService;
}
We will have two method:
Method 1:
we can directly override the servlet’s “init” method as following sample:
public void init(ServletConfig servletConfig) throws ServletException
{
ServletContext servletContext =
servletConfig.getServletContext();
WebApplicationContext
webApplicationContext =
WebApplicationContextUtils.
getWebApplicationContext(servletContext);
AutowireCapableBeanFactory
autowireCapableBeanFactory
=
webApplicationContext.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.configureBean(this,
BEAN_NAME);
}
The “BEAN_NAME” is the name which we want to inject thru the Spring, e.g.
“MyTaskService”.
But this is not a graceful method.
Method 2:
We write a Delegate Bean, like the
“org.springframework.web.struts.DelegatingRequestProcessor”
, then we
can thru configurable method to inject our service into the servlet, here is our
Delegate Bean:
public class DelegatingServletProxy extends GenericServlet {
private
String targetBean;
private Servlet proxy;
@Override
public void service(ServletRequest req, ServletResponse
res)
throws ServletException, IOException {
proxy.service(req, res);
}
public void init() throws ServletException {
this.targetBean =
getServletName();
getServletBean();
proxy.init(getServletConfig());
}
private void getServletBean() {
WebApplicationContext wac =
WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext());
this.proxy
= (Servlet) wac.getBean(targetBean);
}
}
With this DelegatingServletProxy, we should make a little change of the “”
cofig in our “web.xml”.
Normally, we config our “servlet” like this:
userServlet
com.sample.UserServlet
Now, we config our “servlet” in this way:
userServlet
com.sample.DelegatingServletProxy
No changes for “”, only for “”.
And the last thing is:
Pls don’t forget to add the “@Component” into the “servlet” to tell ur Spring
container that this servlet will be regarded as a “spring-bean”. Here is a
sample:
@Component
public class UserServlet extends HttpServlet {
private
static final long serialVersionUID = 1L;
@Resource
MyTaskService
myTaskService;
}
Ok, done, now we can enjoy the “IoC” into our servlet.
into the “action”. But sometime we have to use the “servlet”.
I mean the real servlet, not the struts’s action-servlet!
For example:
We have a servlet name is “UserServlet”, we want to inject the service
“MyTaskService” into it as following sample shows:
public class userServlet extends HttpServlet {
@Resource
MyTaskService myTaskService;
}
We will have two method:
Method 1:
we can directly override the servlet’s “init” method as following sample:
public void init(ServletConfig servletConfig) throws ServletException
{
ServletContext servletContext =
servletConfig.getServletContext();
WebApplicationContext
webApplicationContext =
WebApplicationContextUtils.
getWebApplicationContext(servletContext);
AutowireCapableBeanFactory
autowireCapableBeanFactory
=
webApplicationContext.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.configureBean(this,
BEAN_NAME);
}
The “BEAN_NAME” is the name which we want to inject thru the Spring, e.g.
“MyTaskService”.
But this is not a graceful method.
Method 2:
We write a Delegate Bean, like the
“org.springframework.web.struts.DelegatingRequestProcessor”
, then we
can thru configurable method to inject our service into the servlet, here is our
Delegate Bean:
public class DelegatingServletProxy extends GenericServlet {
private
String targetBean;
private Servlet proxy;
@Override
public void service(ServletRequest req, ServletResponse
res)
throws ServletException, IOException {
proxy.service(req, res);
}
public void init() throws ServletException {
this.targetBean =
getServletName();
getServletBean();
proxy.init(getServletConfig());
}
private void getServletBean() {
WebApplicationContext wac =
WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext());
this.proxy
= (Servlet) wac.getBean(targetBean);
}
}
With this DelegatingServletProxy, we should make a little change of the “”
cofig in our “web.xml”.
Normally, we config our “servlet” like this:
userServlet
com.sample.UserServlet
Now, we config our “servlet” in this way:
userServlet
com.sample.DelegatingServletProxy
No changes for “”, only for “”.
And the last thing is:
Pls don’t forget to add the “@Component” into the “servlet” to tell ur Spring
container that this servlet will be regarded as a “spring-bean”. Here is a
sample:
@Component
public class UserServlet extends HttpServlet {
private
static final long serialVersionUID = 1L;
@Resource
MyTaskService
myTaskService;
}
Ok, done, now we can enjoy the “IoC” into our servlet.
相关文章推荐
- 如何使用spring将service层注入到servlet中去(how to use Spring to inject ur service layer into the servlet )
- How to use the windows active directory to authenticate user via logon form 如何自定义权限系统,使用 active directory验证用户登录
- [Ubuntu] 如何在VirtualBox OSE中使用share folders -- How to use the share folders in VirtualBox OSE
- 译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- [转帖]译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- [转载]如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- 译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class) (转)
- 如何使用OpenCV的处理图像,文字变得锐利和清晰?(How to use OpenCV to process image so that the text become sharp and clea
- 如何使用表维护工具。how to use the table extent maintenance tool
- 译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- (译)如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- 如何使用springmvc框架在普通的servlet注入service
- 在servlet中如何使用被Spring管理的service
- How to use the ZooKeeper driver for ServiceGroup in OpenStack Nova
- [转]How to use String.Format in PowerShell?(如何在PowerShell中使用string.format?)
- 为何以及如何使用Netlink Socket ( Why and How to Use Netlink Socket)
- [DeepLearning]如何使用对比散度(How to use Contrastive Divergence)
- 如何用Spring将Service注入到Servlet中
- 如何使用SSH向IPOD Touch2传输文件 - How to SSH into iPod Touch 2G [Transfer Files, Apps via SSH Access] 如何使用SSH向IPOD Touch2传输文件 - How to S
- All-In-One Code Framework(AIO): 如何编写VC++代码来使用COM组件 (How to use COM in a VC++ client)