canghaizhiyi 2016-06-12 14:46 采纳率: 0%
浏览 1658

springmvc+mybatis调用controller报autowired失败

@service、@controller都写了,@service也写在了实现类而不是接口上,controller中autowired标在了set方法上,单元测试中service和serviceImpl没有问题,但页面调用controller却宝座autowired失败找不到对应service网上找了很多问题我都没有,大概是我检查不够仔细?求大神帮助

添加了一下spring配置和web.xml

异常信息:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void controller.LoginController.setLoginService(service.LoginService); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.LoginService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

controller

 @Controller("loginController")
@RequestMapping("/User")
@Component
public class LoginController {

    private LoginService loginService;

    public LoginService getLoginService() {
        return loginService;
    }

    @Autowired
    @Qualifier("loginService")
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    @RequestMapping(value = "/Login",method=RequestMethod.POST)
    public String userLogin(HttpSession session,@RequestParam("userid") String userid,
            @RequestParam("password") String password, @RequestParam("perflg") int perflg,Model model
            ) {
        User user = loginService.Login(userid, password, perflg);

        if (loginService.Login(userid, password, perflg) != null) {
            session.setAttribute("userid", user.getUserId());
            session.setAttribute("username",user.getUserName());
            session.setAttribute("permission", user.getPermission());
            session.removeAttribute("nologin");
            if (perflg == 0) {
                return "adminhome";
            }
            if (perflg == 1) {
                return "goverhome";
            }
            if (perflg == 2) {
                return "compahome";
            }
            if (perflg == 3) {
                return "consehome";
            }
            if (perflg == 4) {
                return "peoplehome";
            }
        }
        model.addAttribute("Lb_alert", "用户名或密码不正确");
        session.setAttribute("nologin", "你还没有登陆.");
        return "forward:/Login";
    }

}

serviceimpl:

 @Service("loginService")
@Transactional
@Component
public class LoginServiceImpl implements LoginService {


    private LoginMapper loginMapper;


    public LoginMapper getLoginMapper() {
        return loginMapper;
    }

    @Autowired
    public void setLoginMapper(LoginMapper loginMapper) {
        this.loginMapper = loginMapper;
    }


    @Override
    public User Login(String userid, String password, int perflg){
        return loginMapper.Login(userid, password, perflg);
    }

}

service

 public interface LoginService {

    public User Login(String userid,String password,int perflg);

}

spring.xml

     <!-- 引入属性文件 -->
    <context:property-placeholder location="classpath:mysqldb.properties" />

    <!-- 自动注入 -->
    <context:component-scan base-package="service" />
        <!-- 加载properties文件 -->
        <!-- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
            <property name="locations"> <list> <value>classpath:mysqldb.properties</value> 
            </list> </property> </bean> -->

spring-mvc.xml

 <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
    <context:component-scan base-package="controller" />

    <!-- 配置静态资源吗,直接映射到对应的文件夹,不被DispatcherServlet处理,需要重新spring-mvc-3.0.xsd -->
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/css/**" location="/css/" />

    <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/" p:suffix=".jsp" />

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
        <property name="maxUploadSize">
            <value>32505856</value><!-- 上传文件 大小限制为31M,31*1024*1024 -->
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>

spring-mybatis.xml

 <!-- 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- 基本属性 url、user、password -->  
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
        <property name="url" value="jdbc:mysql://localhost:3306/electric" />  
        <property name="username" value="root" />  
        <property name="password" value="root" />  
        <property name="initialSize" value="1" />  
        <property name="minIdle" value="1" />   
        <property name="maxActive" value="20" />  
        <property name="maxWait" value="60000" />
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超过时间限制多长; -->
        <property name="removeAbandonedTimeout" value="180" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <!-- 用来检测连接是否有效的sql,要求是一个查询语句-->
        <property name="validationQuery" value="SELECT 1" />
        <!-- 申请连接的时候检测 -->
        <property name="testWhileIdle" value="true" />
        <!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
        <property name="testOnBorrow" value="false" />
        <!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能  -->
        <property name="testOnReturn" value="false" />
    </bean>

    <!-- Mybatis文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 映射文件路径 -->
        <property name="mapperLocations" value="classpath:mapping/*.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

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

web.xml

 <display-name>Archetype Created Web Application</display-name>
  <!-- Spring和mybatis的配置文件 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>  
    </context-param>  

    <!-- spring mvc servlet -->
    <servlet>
        <description>spring mvc servlet</description>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <description>spring mvc 配置文件</description>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 配置session超时时间,单位分钟 -->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
  • 写回答

2条回答

  • lzj0327 2016-06-12 15:15
    关注
    @Autowired
     private LoginService loginService;
     自动注入我记得不要写set和get方法的,只要在定义变量的时候写个注解就行了
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?