【问题标题】:Null Pointer Exception when autowiring beans自动装配 bean 时出现空指针异常
【发布时间】:2015-11-16 23:29:17
【问题描述】:

我正在为学校制作一个简单的项目,但我被卡住了。 我有一个需要处理的 bean FavoriteService。但是,当在我的 servlet 中自动装配 bean 时,我不断收到空指针异常,我不知道为什么。

收藏服务

public class FavoriteService {
    @Autowired
    private Users users;

    public boolean checkLogin(String username, String password) {
        return users.login(username, password);
    }

    public void addUser(String rootUsername, String rootPassword, String username, String password)
    {
        if(rootUsername.equals("root") && rootPassword.equals("rootpasswd")) users.addUser(username, password);
    }

    public List<String> getFavorites(String username, String password)
    {
        List<String> favorites;

        if(checkLogin(username, password))
        {
            favorites = users.getFavorites(username);
        } else {
            favorites = new ArrayList<String>();
        }

        return favorites;
    }

    public void addFavorite(String username, String password, String favorite)
    {
        if(checkLogin(username, password))
        {
            users.addFavorite(username, favorite);
        }
    }

    public void removeFavorite(String username, String password, String favorite)
    {
        if(checkLogin(username, password))
        {
            users.removeFavorite(username, favorite);
        }
    }
}

小服务程序

public class LoginServlet extends HttpServlet {
    @Autowired
    private FavoriteService favoriteService;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println(favoriteService);

        if(favoriteService.checkLogin(username, password))
        {
            resp.sendRedirect("root.jsp");
        } else {
            resp.sendRedirect("index.jsp");
        }
    }
}

springservlet-servlet

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="be.kdg.prog4.tdd"/>
    <bean name="userDao" class="be.kdg.prog4.tdd.UserDaoWithMap" scope="singleton" />
    <bean name="users" class="be.kdg.prog4.tdd.Users" scope="singleton" />
    <bean name="favoriteService" class="be.kdg.prog4.tdd.FavoriteService" scope="singleton" />
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".*"/>
    </bean>
</beans>

Web.xml

<web-app schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <display-name>TDD oefening</display-name>

    <servlet>
        <servlet-name>springservlet</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springservlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>be.kdg.prog4.tdd.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

如果您需要更多信息,请随时询问。 谢谢。

编辑:

我通过在 servlet init 中这样做来解决它

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
}

【问题讨论】:

  • 尝试通过减少问题来更好地定义问题,通过做最少的事情来看到它出现
  • 正如@tomsoft 所说,请尝试将代码减少到它给您带来错误的部分。
  • 或者至少是代码的堆栈跟踪。

标签: java spring servlets autowired


【解决方案1】:

如果您正在调用 LoginServlet,则您不会经历 Spring。您将它直接映射到您的 web.xml 中,并且您的 set let 容器正在初始化您的类而无需通过 Spring,并且自动装配不起作用。你需要通过springservlet的映射来访问服务器。

【讨论】:

    【解决方案2】:

    Servlet 没有被 Spring 初始化,因此 @Autowired 字段没有被 Spring 初始化。

    您可以覆盖 Servlet init() 方法并获取 Spring Web 上下文并使用其 getBean 方法来获取您的 bean,而不是使用 @Autowired

    这是在 init 方法中获取 Spring 上下文的方法:

    ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    

    【讨论】:

    • 感谢您的回复。我尝试了您的解决方案,但现在我在 webapplicationcontext 上遇到空指针异常。这就是我所做的:@Override public void init() throws ServletException { super.init(); WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); favoriteService = (FavoriteService) ctx.getBean("favoriteService"); }
    猜你喜欢
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2017-11-20
    • 2014-10-07
    • 2017-10-22
    • 1970-01-01
    • 2014-11-17
    相关资源
    最近更新 更多