我想在 servlet 中使用 spring 自动装配,所以这是我的代码:
@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/>
网页.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 的引用为空),谁能告诉我是否遗漏了什么?