将与服务的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"));
}
}