springboot service层注解失败

本文记录了Spring Boot项目编译报错问题。报错原因包括启动时未扫描到实现类,无法自动注入service bean;未扫描到xml映射文件;包创建方式有误。还指出项目涉及的maven多模块使用、jar运用及模块依赖关系等知识有待巩固,后续计划向dubbo项目延伸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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尴尬了一个星期,谨为记录自己的点点滴滴。写得不好,欢迎来怼。谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值