如何使用spring将service层注入到servlet中去(how to use Spring to inject ur service layer into the servlet )
来源:互联网 发布:维基百科 知乎 编辑:程序博客网 时间:2024/05/22 06:06
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.
- 如何使用spring将service层注入到servlet中去(how to use Spring to inject ur service layer into the servlet )
- 如何使用spring将service层注入到servlet中去(how to use Spring to inject ur service layer into the servlet )
- Servlet中使用Spring注入Service层
- 如何用Spring将Service注入到Servlet中
- 用Spring将Service注入到Servlet中
- 如何用Spring将Service注入到Servlet中(注解模式)
- 如何用Spring将Service注入到Servlet中(注解模式)
- How to use Spring @Component, @Repository, @Service and @Controller Annotations?
- 在servlet中如何使用被Spring管理的service
- How to inject value into bean properties in Spring
- How to use the Service Bus relay service
- Spring中,将request和response由servlet传入service
- 如何用Spring将Service注入到ServletContextListener中
- servlet中如何获取spring管理的service
- Spring容器启动后注入service到Servlet并自动执行
- spring 如何将spring中的dao、service等注入到普通的Java类
- how to use spring job
- Servlet中调用 Spring管理的 Service层方法(SSH框架中)
- hadoop和hive结合使用总结(关于一个稳定性的问题)
- lfs----------
- android设置背景平铺
- codeblocks开发android o 文件环境搭建指南
- 红蓝3D的原理
- 如何使用spring将service层注入到servlet中去(how to use Spring to inject ur service layer into the servlet )
- java进制转换
- javascript注册多事件
- log4J简单介绍
- java线程面试精选
- Android char16进制
- Windows下Eclipse开发基于Sailfin的SipServlet应用
- > 与 < 等HTML不转译为<>
- Android实现XML解析技术