gpt4 book ai didi

java - Servlet 内的 @Autowire null

转载 作者:行者123 更新时间:2023-12-01 06:43:50 24 4
gpt4 key购买 nike

如果我使用 @Autowired,这个 servlet 内的服务为 null,使用 context.getBean() 可以正常工作;此外,与/floorOperationWS/的映射尚未完成,我必须在 web.xml 中定义映射。

package com.confloorapp.services.endpoint;

import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;



@WebServlet(value = "/floorOperationWS/")
public class UpdateFloorEventServlet extends HttpServlet {


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());


FloorService floorService = (DoorService)context.getBean("floorService");

}



}

这里是applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:sws="http://www.springframework.org/schema/web-services"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd">

<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />

<!-- Scans the classpath for annotated components that will be auto-registered
as Spring beans. For example @Controller and @Service. Make sure to set the
correct base-package -->
<context:component-scan base-package="com.confloorapp" />
<context:annotation-config/>

<!-- Configures the annotation-driven Spring MVC Controller programming
model. Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />

<!-- Load Hibernate related configuration -->
<!-- Here you can also add spring security context, if exist -->
<import resource="hibernate-context.xml" />
<import resource="spring-security.xml" />

</beans>

最佳答案

由于 servlet 不是由 Spring 容器处理,因此 Autowiring 注释无法立即使用。为了克服这个问题,您可以 Autowiring servlet 中的所有 Spring bean,如下所示:

public class UpdateFloorEventServlet extends HttpServlet {

@Autowired
private FloorService floorService;

private WebApplicationContext springContext;

@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
final AutowireCapableBeanFactory beanFactory = springContext.getAutowireCapableBeanFactory();
beanFactory.autowireBean(this);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}

现在,FloorService 对象将由 Spring 正确 Autowiring ,并且不会有空值。

当应用程序使用bean时,spring上下文文件必须包含所有bean定义。

顺便说一句,init 方法本身可以在扩展 HttpServlet 的单独 Servlet 类中实现。然后您可以让所有 Servlet 扩展该类,这样它们都可以使用 init 方法,从而避免代码重复。

关于java - Servlet 内的 @Autowire null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23839226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com