springboot service层注解失败

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_28068985/article/details/82251795

springboot 项目编译报错 
报错信息如下

Description:

Field blogArticleService in com.xgc.controller.BlogArticleController required a bean of type 'com.xgc.service.IBlogArticleService' that could not be found.


Action:

Consider defining a bean of type 'com.xgc.service.IBlogArticleService' in your configuration.


Process finished with exit code 1

springboot启动文件

package com.xgc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
//@MapperScan({"com.xgc.service"})
@MapperScan("com.xgc.mapper")
@ComponentScan(basePackages={"com.xgc"})
//@ComponentScan(basePackages={"com.xgc.mapper","com.xgc.service.impl","com.xgc.service"})
//@EnableSwagger2
public class XgcApplication {

    public static void main(String[] args) {
        SpringApplication.run(XgcApplication.class, args);
    }
}

启动文件模块项目Web的maven配置

<dependencies>
    <dependency>
        <groupId>com.xgc</groupId>
        <artifactId>xgc-common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.xgc</groupId>
        <artifactId>xgc-user-center-api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.xgc</groupId>
        <artifactId>xgc-blog-center-api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!-- alibaba的druid数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.9</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.35</version>
    </dependency>
    <!--pagehelper-->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.3</version>
        <exclusions>
            <exclusion>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

项目结构

启动失败原因 解释

    xgc-web 是我项目的启动 APP所在模块

    xgc-blog  xgc-user 是我的业务模块

    api是业务对外的开放的service接口,center是业务的实现类

     ①、在xgc-web模块中引用了api模块,没有引入center模块,在启动项目时,项目没有扫描到center里的是实现类,无法实现自动注入service bean,顾报了没有找bean的错误。

    ②  xml映射文件没有扫描到。需要在maven中pom.xml配置

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

注意包的创建,使用package,不要使用文件夹(directory)创建包。使用文件夹,扫描的时候没有扫描到类

项目涉及的知识有待巩固:

   1、maven 多模块的使用,及jar的运用。因做项目时想做一个公共的jar供其他模块调用,没有实现成功。

    2、此项目是为了延伸向dubbo的项目,顾模块之间的依赖关系没有做好,后期先实现springboot项目,在向dubbo进发。

bug尴尬了一个星期,谨为记录自己的点点滴滴。写得不好,欢迎来怼。谢谢

猜你喜欢

转载自blog.csdn.net/baidu_28068985/article/details/82251795