【发布时间】: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