解决方案 »

  1.   

    自动注入userService的时候错了。你看看那里用了,贴出代码大家看看
      

  2.   


    package com.baobaotao.web;import java.util.Date;import javax.servlet.http.HttpServletRequest;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;import com.baobaotao.domain.User;
    import com.baobaotao.service.UserService;@Controller("loginController")
    public class LoginController{

    @Autowired
    @Qualifier("userService")
    private UserService userService;
        
    @RequestMapping(value = "/index.html")
    public String loginPage(){
    return "login";
    }

    @RequestMapping(value = "/loginCheck.html")
    public ModelAndView loginCheck(HttpServletRequest request,LoginCommand loginCommand){
    boolean isValidUser = 
       userService.hasMatchUser(loginCommand.getUserName(),
                        loginCommand.getPassword());
    if (!isValidUser) {
    return new ModelAndView("login", "error", "用户名或密码错误。");
    } else {
    User user = userService.findUserByUserName(loginCommand
    .getUserName());
    user.setLastIp(request.getLocalAddr());
    user.setLastVisit(new Date());
    userService.loginSuccess(user);
    request.getSession().setAttribute("user", user);
    return new ModelAndView("main");
    }
    }


    public UserService getUserService() {
    return userService;
    }

    public void setUserService(UserService userService) {
    this.userService = userService;
    }
    }
    这是LoginController的全部代码
      

  3.   


    配置文件的代码如下<?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        
        <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
        <context:component-scan base-package="com.baobaotao.dao"/>
        <context:component-scan base-package="com.baobaotao.service"/>
        <context:component-scan base-package="com.baobaotao.web"/>
        
        <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" 
    p:driverClassName="com.mysql.jdbc.Driver"
    p:url="jdbc:mysql://localhost:3306/sampledb" 
    p:username="root"
    p:password="1234" /> <!-- 配置Jdbc模板  -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
    p:dataSource-ref="dataSource" />

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    p:dataSource-ref="dataSource" />

    <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
    <aop:config proxy-target-class="true">
    <aop:pointcut id="serviceMethod"
    expression=" execution(* com.baobaotao.service..*(..))" />
    <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="*" />
    </tx:attributes>
    </tx:advice>
    </beans>
      

  4.   

    看看是不是UserService 没有声明注册成springbean
      

  5.   

    我在上面加了 @Service("userService")  
      

  6.   

    还想到2中可能性啊,经供参考,一个是你们UserService是接口类,注解放到实现类,还有就是是不是你的类没部署到tomcat里面啊?
      

  7.   


    第一种可能性,之前研究过 , 有做单元测试的 没问题 .  第二种可能性应该不会吧 ,我是用myeclipse发布到tomcat下的 , 发布到weblogic下也是报同样的错. 
      

  8.   

    问题终于解决了  !!!~~    是web.xml中的  
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </context-param>  <param-value>classpath:applicationContext.xml</param-value>  这个写错了 我晕哦