2014-09-24 92 views
0

当我使用@Autowired注释注入DAO服务显示java.lang.NullPointerException occurred.I无法修复it.Please任何人的帮助!空指针异常使用@Autowired注释注入bean时

例外

java.lang.NullPointerException 
at com.sargeras.login.service.LoginServiceImpl.register(LoginServiceImpl.java:14) 

服务

public class LoginServiceImpl implements LoginService{ 
@Autowired 
private LoginDao loginDao; 
public int register(UserVo userVo){ 
    return loginDao.register(userVo); 
} 
} 

@Repository 
public class LoginDaoImpl extends JdbcDaoSupport implements LoginDao { 
@Override 
public int register(UserVo userVo) { 
    int result = -1; 
    return result; 
} 

小号pringmvc.xml

<context:component-scan base-package="com.sargeras" /> 
<mvc:annotation-driven /> 

的applicationContext.xml

<bean name="loginService" class="com.sargeras.login.service.LoginServiceImpl"> 
</bean> 
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close"> 
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" /> 
    <property name="username" value="sargeras" /> 
    <property name="password" value="sargeras" /> 
</bean> 
<bean name="loginDao" class="com.sargeras.login.repository.LoginDaoImpl"> 
    <property name="dataSource" ref="dataSource"></property> 
</bean> 

控制器

@Controller 
@RequestMapping(value = "/login") 
public class LoginController extends BaseController{ 
@Autowired 
private LoginService service; 
@RequestMapping(value = "validate") 
public void toLogin(HttpServletResponse response) throws Exception { 
    logger.error("hhhh"); 
    response.getWriter().print(" this is to logging 1"); 
} 
@RequestMapping(value="register") 
public void register(HttpServletRequest request,HttpServletResponse response) throws Exception{ 
    UserVo user = new UserVo(); 
    int result = 0; 
    String userName = (String)request.getParameter("usernamesignup"); 
    String userPassword = (String)request.getParameter("passwordsignup_confirm"); 
    String userEmail = (String)request.getParameter("emailsignup"); 
    user.setUserName(userName); 
    user.setUserPassword(userPassword); 
    user.setUserEmail(userEmail); 
    result = service.register(user); 
    if(result == 1){ 
     response.getWriter().print("Success"); 
    } 
} 
} 

服务是在控制器中使用并且可以被成功注入

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5"> 
<display-name>spring_test</display-name> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:/config/applicationContext*.xml</param-value> 
</context-param> 
<context-param> 
    <param-name>log4jConfigLocation</param-name> 
    <param-value>classpath:log4j.xml</param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
</listener> 

<filter> 
    <filter-name>characterEncodingFilter</filter-name> 
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
    <init-param> 
     <param-name>encoding</param-name> 
     <param-value>UTF-8</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>characterEncodingFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:/config/spring-servlet.xml</param-value> 
    </init-param> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
</web-app> 

**这是我的web.xml配置**

**解析为以下问题**

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:/config/spring-servlet.xml, 
     classpath:/config/applicationContext*.xml 
     </param-value> 
    </init-param> 
</servlet> 

应用程序上下文和servlet上下文不能混杂在一起,我会记住这一点。谢谢大家!

+0

向我们展示您使用'loginService' bean的位置。 – 2014-09-24 14:24:22

+0

现在可以弹出Autowire私人字段或者样本中没有显示setter? – 2014-09-24 14:34:17

+0

@LorenzoBoccaccia是的,支持私人域注入 – luboskrnac 2014-09-24 14:36:46

回答

0

正如已经建议你确实混合了servletContext和applicationContext。您可以使用servletContext(applicationContext.xml文件)导入springmvc.xml文件,也可以将mvc-annotation-driven标签放入applicationContext.xml文件中。无论哪种方式,您都应该阅读如何正确构建您的上下文。

0

如上所述,沟springmvc.xml并添加

<context:component-scan base-package="com.sargeras" /> 
<mvc:annotation-driven /> 

到你的applicationContext.xml。

taht应该足够