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.
分享到:
相关推荐
If you want to learn how to get around the Spring framework and use it to build your own amazing applications, then this book is for you. Beginning with an introduction to Spring and setting up the ...
injectdll远程线程注入
exe将dll注入到explorer.exe资源管理器进程_DLL注入示例.zip
Dll注入 InjectDLL 一个把DLL注入到其它进程的程序
* Dependency Injection (To inject objects into constructors) * Tag Helper (to clean up the HTML and enable re-use) * HTML Helper methods (to clean up your HTML and enable re-use) * Bower/NuGet (To ...
To inject your code into foreign executables you only need to make a DLL file with functions you want to inject into PE file, call PE-inject function to place the DLL into the executable and the file ...
赠送jar包:guice-servlet-4.0.jar; 赠送原API文档:guice-servlet-4.0-javadoc.jar; 赠送源代码:guice-servlet-4.0-sources.jar; 赠送Maven依赖信息文件:guice-servlet-4.0.pom; 包含翻译后的API文档:guice-...
spring 三种注入(就是从spring容器中将bean放入对象属性值中) Spring下描述依赖关系@Resource, @Autowired和@Inject的区别与联系 Spring中BeanFactory和ApplicationContext的区别 谈谈Spring IOC的理解,原理与...
介绍spring注入技术和spring aop 以及一些代码的百年之
赠送jar包:guice-servlet-4.0.jar; 赠送原API文档:guice-servlet-4.0-javadoc.jar; 赠送源代码:guice-servlet-4.0-sources.jar; 赠送Maven依赖信息文件:guice-servlet-4.0.pom; 包含翻译后的API文档:guice-...
@Service在业务逻辑层(service层) @Repository在数据访问层(dao层) @Controller在展现层(MVC→SpringMVC) 注入Bean的注解 @Autowired:Spring提供的注解 @Inject:JSR-330提供的注解 ...
In Pro CDI 2 in Java EE 8, use CDI and the CDI 2.0 to automatically manage the life cycle of your enterprise Java, Java EE, or Jakarta EE application’s beans using predefined scopes and define custom...
In this book, we focus on how to use biomimicry of the functional operation of the “hardware and software”of biological systems for the development of optimization algorithms and feedback control ...
网上的东西好大多都不能直接用,自己结合网上资料做了一个Struts2+Spring3+MyBatis3的测试工程,JUnit测试用例和WEB服务。 内涵完整jar包,解压直接可用,包括一个表文件。 Eclipse3.2+Tomcat/5.5+jdk1.5.0_17 - ...
This allows a process to access any portion of the physical memory by mapping it into the applications window. When used in combination with Intel’s Physical Addressing Extensions (PAE) on Windows ...
Chapter 3, Assembly Language in Linux, explains how to use the assembly language on Linux to build shellcode. Chapter 4, Reverse Engineering, shows how to use debuggers to perform reverse engineering ...
Chapter 10 takes you into the world of GWT-RPC, where you’ll learn how to pass Java objects between the web browser and your Java servlets. ABOUT THIS BOOK xxvii Chapter 11 expands on the previous ...
从Spring 3.0开始,Spring提供了...注意:如果你使用Maven,javax.inject子包是需要在标准Maven仓库中用到的http://repo1.maven.org/maven2/javax/inject/javax.inject/1/,你需要添加下面的依赖到你的pom.xml文件中。
一个webpack插件,用于将代码动态注入到包中。 您可以查看示例 用法 # webpack . config . js const InjectPlugin = require ( 'webpack-inject-plugin' ) . default ; module . exports = { // Other stuff in ...
Spring 依赖的 javax.inject.Provider