SpringCloud服务拆分遇到的奇怪问题

文章讲述了开发者在将Entity、Mapper、Service拆分到server模块后,遇到SpringBoot启动时依赖注入失败的问题。通过分析报错,发现Service未被扫描到,解决方案是在system模块的启动类中添加ComponentScan和MapperScan注解以指定扫描范围。
摘要由CSDN通过智能技术生成

将与服务的Entity,Mapper,Service拆分成server模块遇到奇怪问题

问题复现:

两个子模块:

        server:含有Entity,Mapper,Service

        system:Controller

在system中调用server中的ChapterService,会出现报错:

2024-02-06 11:34:58.755 ERROR 23276 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'com.ending.server.service.IChapterService' that could not be found.


Action:

Consider defining a bean of type 'com.ending.server.service.IChapterService' in your configuration.

在网上找了很多方法,说加上

@SpringBootApplication(scanBasePackages = "com.ending")在启动类上

还是报错,不过报错信息不一样了,这样一坨东西

2024-02-06 11:37:00.236 ERROR 2316 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testSystemController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'chapterServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ending.server.mapper.ChapterMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:332) ~[spring-context-5.3.21.jar:5.3.21]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.21.jar:5.3.21]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.21.jar:5.3.21]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.21.jar:5.3.21]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.21.jar:5.3.21]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.21.jar:5.3.21]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.21.jar:5.3.21]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.21.jar:5.3.21]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.21.jar:5.3.21]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.21.jar:5.3.21]


分析:

通过报错信息得出Service没有被扫描到,IDEA也提示没有被使用过

所以加上ComponentScan和MapperScan来解决

解决方法:

在需要server子模块的子模块启动类中加入以下内容:

@ComponentScan("com.ending")  // 扫描当前子模块中的@Controller,以及Server中的Service

@MapperScan("com.ending.server.mapper")            // 扫描server模块中的@Mapper

@SpringBootApplication
@ComponentScan("com.ending")
@MapperScan("com.ending.server.mapper")
public class SystemApplication {
	
	private static final Logger LOG = LoggerFactory.getLogger(SystemApplication.class);

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(SystemApplication.class);
		Environment env = app.run(args).getEnvironment();
		LOG.info("启动成功!!");
		LOG.info("system地址:\thttp://127.0.0.1:{}",env.getProperty("server.port"));
	}
}
  • 31
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值