SSM框架整合错误:Could not autowire field

debug信息:

通过Ctrl/Command + F快速查找错误信息

[2018-09-26 10:29:20,962] Artifact DayBuy:war exploded: Artifact is being deployed, please wait...
ERROR [RMI TCP Connection(3)-127.0.0.1] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.daybuy.obj9527.service.CategoryService com.daybuy.obj9527.controller.CategoryController.categoryService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.daybuy.obj9527.service.CategoryService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

分析:

可以清晰的看出是Could not autowire field错误,错误原因是:com.daybuy.obj9527.service.CategoryService这个类无法自动装配,位于
Error creating bean with name 'categoryController'此处

/**
 * @author Obj9527
 * @date 2018/9/25 11:43
 */

@Controller
@RequestMapping("")
public class CategoryController {
    @Autowired
    CategoryService categoryService;//错误出现在这里

    @RequestMapping("admin_category_list")
    public String list(Model model){
        List<Category> categories = categoryService.list();
        model.addAttribute("cs",categories);
        return "admin/listCategory";
    }
}

无法自动装配一般是spring配置文件未配置扫描,查看spring-dao.xml文件:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.daybuy.obj9527.service" />

    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />

        
        <property name="maxWait" value="60000" />

        
        <property name="timeBetweenEvictionRunsMillis" value="60000" />

        
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="validationQuery" value="SELECT 1" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
                  value="20" />
    bean>

    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.daybuy.obj9527.pojo" />
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.daybuy.obj9527.mapper"/>
    bean>
beans>

发现未扫描impl包,所以加上对应的包地址就行了,注意替换成你自己的包地址

你可能感兴趣的:(SSM框架整合错误:Could not autowire field)