自动装配bean时出现空指针异常

时间:2015-08-23 13:20:45

标签: java spring servlets autowired

我正在为学校做一个简单的项目,但我被困住了。 我有一个我需要解决的bean FavoriteService。但是当我在我的servlet中自动装配bean时,我一直得到一个Null指针异常,我无法弄清楚原因。

FavoriteService

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);
        }
    }
}

的Servlet

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());
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

而不是使用getBean

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

ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
相关问题