如何使用spring将service层注入到servlet中去(how to use Spring to inject ur service layer into the servlet )
2012-07-06 17:32
696 查看
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.
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 )
- (译)如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- 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)
- 如何使用代码美化器Uncrustify (How to use code beautifier Uncrustify)
- [Windows Azure] How to use the Windows Azure Blob Storage Service in .NET
- How to use logrotate to manage log files in linux(如何在Linux中使用logrotate去管理日志文件)
- How to use user script (custom script)如何使用用户脚本之Chrome
- 如何使用ODB(How to use odb On windows)
- [Windows Azure] How to use the Table Storage Service
- How Do I Use the Windows System Restore Utility?——如何使用window 系统还原工具?
- How to use toolbar control on dialog using CToolBar class in VC++ - VC++ 中用CToolBar 类如何使用ToolBar控件
- [Windows Azure] How to use the Queue Storage Service