我想在spring cloud alibaba里面,让其他业务模块 的entity mapper service 都集中到 common 里面?

使用的是 spring cloud alibaba 2021.0.1 + spring boot 2.6.4
目录布局:
1、common模块:存放 model、mapper、service、impl、连接数据库、统一异常处理、redis josn数据格式化、统一响应、swagger、mp、cors
2、merchant模块(商户端):主要编辑controller对外开放api接口
3、supply模块(供货商):主要编辑controller对外开放api接口

问题描述:
目前在做 merchant模块,启动时提示错误:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication
de153284ee0293c1957a1adb5b4189c.png
尝试3~5小时没解决,特来思否求助大佬们

特别描述:
1、目前swagger2实际测试成功。将swagger2全部内容放到common模块,在商户端编写controller时,进行添加注解接口:启动 swagger2可以显示商户端的端口中打开文档地址显示controller中定义的接口文档

尝试失败过的方法:
1、在启动类上加包扫描:结果失败,提示另一个错误

// @SpringBootApplication
@SpringBootApplication(scanBasePackages = "com.quanneng")
@Slf4j
@MapperScan("com.quanneng.mapper")
public class MerchantApiApplication {
 ……
}

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication
-->后百度,解决无果

2、尝试

@ComponentScan({"com.*"})
public class MerchantApiApplication {
    ……
}

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'merchantController': Unsatisfied dependency expressed through field 'merchantService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'merchantService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'merchantMapper' defined in file [D:\biancheng\git_cangku_java_spring_cloud_alibaba\liuliang_mall_java\common\target\classes\com\quanneng\mapper\MerchantMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

-->配置了 mybatisPlusConfig 无果、添加 datasource 到 merchant 模块yml文件 无果。

阅读 475
1 个回答

看不到你的代码,不太具体知道什么问题。

根据你的描述,是能够实现的。

简单来说,就是 merchant 和 supply 都依赖 common,你不想写重复的代码。

第一个,spring 包扫描配这个: scanBasePackages = "com.quanneng",注意,如果有多个模块,都需要配。

第二个,mapper 接口配置:@MapperScan("com.quanneng.mapper"),

第三个,swagger,要注意接口扫描配置,这个其实可以放在端上

第4个,统一异常处理,只能有一个

另外,值得一提的是,你还可以这样,你可以把 common 写成一个 spring-boot-starter,这样你就不用处理包扫描的问题了。我目前就是这样,写一些业务组件。

推荐问题
宣传栏