首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用@Autowired注释注入bean时的空指针异常

使用@Autowired注释注入bean时的空指针异常
EN

Stack Overflow用户
提问于 2014-09-24 14:21:22
回答 3查看 4.9K关注 0票数 0

当我使用@Autowired注解将dao注入服务时,就出现了java.lang.NullPointerException,我无法修复it.Please任何人的帮助!

异常

代码语言:javascript
运行
复制
java.lang.NullPointerException
at com.sargeras.login.service.LoginServiceImpl.register(LoginServiceImpl.java:14)

服务

代码语言:javascript
运行
复制
public class LoginServiceImpl implements LoginService{
@Autowired
private LoginDao loginDao;
public int register(UserVo userVo){
    return loginDao.register(userVo);
}
}

dao

代码语言:javascript
运行
复制
@Repository
public class LoginDaoImpl extends JdbcDaoSupport implements LoginDao {
@Override
public int register(UserVo userVo) {
    int result = -1;
    return result;
}

springmvc.xml

代码语言:javascript
运行
复制
<context:component-scan base-package="com.sargeras" />
<mvc:annotation-driven />

applicationContext.xml

代码语言:javascript
运行
复制
<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>

控制器

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
<?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配置**

**问题解决如下:**

代码语言:javascript
运行
复制
<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上下文不能混合,我将把它们保存在mind.Thank中。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-24 14:47:32

您没有发布servlet配置,但我怀疑您正在混合应用程序上下文和servlet上下文。它们是两个独立的bean容器。这里讨论的是:applicationContext not finding Controllers for Servlet context

票数 0
EN

Stack Overflow用户

发布于 2014-09-24 15:12:48

如前所述,您确实混淆了servletContext和applicationContext。要么让您的servletContext(applicationContext.xml文件)导入springmvc.xml文件,要么将mvc注释驱动的标记放在applicationContext.xml文件中。无论哪种方式,你都应该仔细阅读如何正确地构造你的上下文。

票数 0
EN

Stack Overflow用户

发布于 2014-09-24 15:20:33

如前所述,丢弃springmvc.xml并添加

代码语言:javascript
运行
复制
<context:component-scan base-package="com.sargeras" />
<mvc:annotation-driven />

敬你的applicationContext.xml。

他应该是足够的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26019323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档