servlet 中的自动装配 [英] Autowiring in servlet
本文介绍了servlet 中的自动装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 servlet 中使用 spring 自动装配,所以这是我的代码:
i want to use spring autowiring in servlet so here's my code:
@Configurable
public class ImageServlet extends HttpServlet {
@Autowired
private SystemPropertyDao systemPropertyDao;
@Override
public void init() throws ServletException {
String imagePath = systemPropertyDao.findByID(StaticParam.CONTENT_FOLDER);
}
而 SystemPropertyDao
用 @Repository
和我的applicationContext.xml:
<context:component-scan base-package="com.basepackage" />
<mvc:annotation-driven />
<context:annotation-config />
<context:spring-configured/>
web.xml:
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>com.xeno.basepackage.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/myimages/*</url-pattern>
</servlet-mapping>
有时自动装配有效,有时无效(对 spring bean systemPropertyDao 的引用为空),谁能告诉我我是否遗漏了什么?
sometimes the autowiring works and sometimes it doesn't (the reference to the spring bean systemPropertyDao is null), can anyone please tell me if i am missing something?
推荐答案
我按照以下链接中的解决方案进行操作,效果很好:从 JBoss 中的 servlet 访问 Spring bean
I followed the solution in the following link, and it works fine: Access Spring beans from a servlet in JBoss
public class MyServlet extends HttpServlet {
@Autowired
private MyService myService;
public void init(ServletConfig config) {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}
}
这篇关于servlet 中的自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文