2014-09-24 1 views
0

, java.lang.NullPointerException이 occurred.I 당당히 공개하기 위해 출연 해 주신 사람이 도움을 고칠 수!널 포인터 예외 콩을 주입하기 위해 @Autowired 어노테이션을 사용할 때이 서비스에 DAO를 주입하기 위해 @Autowired 어노테이션을 사용하는 경우

예외

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

DAO

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

S

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

서비스 제어기에서 사용되며

성공적으로 주입 될 수 pringmvc.xml 0

의 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> 

응용 프로그램 컨텍스트와 서블릿을 다음과 컨텍스트를 혼합 할 수 없다는 것을 명심하십시오. 감사합니다!

+0

'loginService' 빈을 어디에 사용하고 있는지 표시하십시오. –

+0

이제 샘플 필드에 Autowire 비공개 필드 또는 설정자가 표시되지 않을 수 있습니까? –

+0

@LorenzoBoccaccia 예, 비공개 필드 주사가 지원됩니다 – luboskrnac

답변

0

이미 제안한 것처럼 실제로 servletContext와 applicationContext가 섞여 있습니다. servletContext (applicationContext.xml 파일)에서 springmvc.xml 파일을 가져 오거나 mvc-annotation-driven 태그를 applicationContext.xml 파일에 넣으십시오. 어느 쪽이든 당신은 컨텍스트를 올바르게 구조화하는 방법을 읽어야합니다.

0

전술 한 바와 같이, springmvc.xml 도랑과 applicationContext.xml에

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

를 추가합니다.

충분해야합니다.

관련 문제