在一个springboot模块里面调用另外一个模块的方法

文章讲述了在SpringBoot项目中,当一个模块试图调用另一个模块的服务时遇到的问题,需在主类中添加包扫描注解以使Spring自动发现并注入依赖。作者强调了对service和mapper的扫描配置是必要的。

bug描述:

在一个springboot模块里面调用另外一个模块的方法

通过下面的代码可以看到,我的方法所在的模块是在com.bpmn.camunda.sync.provider里面,而我导入的包是在另外一个模块里面 com.bpmn.camunda.common.service 如果直接启动项目会报错。

package com.bpmn.camunda.sync.provider.server.impl;

import com.bpmn.camunda.common.service.IActHiCommentService;
@Service
public class CActIdUserServiceImpl extends AbstractSupperService<CActIdUserMapper, CActIdUserModel, CActIdUserDTO> implements CActIdUserService {
		
    @Autowired
    private IActHiCommentService iActHiCommentService;

项目背景:

普通的springclou项目。

解决方法:

我们应该在主方法里面加上包的扫描,springbooot会自动扫描到bean,并注入到Ioc容器里面。

一个是 加上componentScan 加上了 "com.bpmn.camunda.common.service"

@ComponentScan(basePackages = {"com.bpmn.camunda.auth","com.bpmn.camunda.sync","com.bpmn.camunda.common.service"})

@SpringBootApplication
@ComponentScan(basePackages = {"com.bpmn.camunda.auth","com.bpmn.camunda.sync","com.bpmn.camunda.common.service"})
@MapperScan({"com.bpmn.camunda.sync.provider.mapper","com.bpmn.camunda.common.mapper"})
@EnableDiscoveryClient
@EnableProcessApplication
@EnableFeignClients(basePackages = {"com.bpmn.camunda","com.focusin.bpmn"})
@EnableAopLog

public class BpmnCamundaSyncApplication {

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

其实只加上了service方法还不够,因为service大概率会调用mapper方法,所以还需要加上mapper扫描。

@MapperScan({"com.bpmn.camunda.sync.provider.mapper","com.bpmn.camunda.common.mapper"})

总结:

添加其他模块的bean时,spring并不能直接扫描到该bean,需要我们手动设置扫描路径。

Java 后端 Spring Boot 项目中,一个模块调用一个模块接口通常可按以下步骤进行: ### 1. 设计项目多模块依赖关系 要确保调用模块依赖于被调用模块。在 Maven 项目里,可在调用模块的 `pom.xml` 文件中添加被调用模块的依赖。例如: ```xml <dependency> <groupId>com.example</groupId> <artifactId>mengxuegu-member-api</artifactId> <version>1.0.0</version> </dependency> ``` ### 2. 创建接口和服务 在被调用模块中定义接口和实现类。以 `B2cDeptApi` 为例: ```java package org.openjweb.b2c.api; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RequestMapping("/api/b2b2c/dept") @RestController public class B2cDeptApi { //localhost:8001/api/b2b2c/dept/query @RequestMapping("query") public String getDeptName(){ String name = "研发部1"; return name; } @Resource private CommDeptService commDeptService; //localhost:8001/api/b2b2c/dept/queryService @RequestMapping("queryService") public String getDeptName2(){ String name = commDeptService.getDeptDemoName(""); return name; } } ``` ### 3. 调用模块调用接口 在调用模块中,使用 Spring 的 `RestTemplate` 或 `WebClient` 来调用调用模块的接口。 #### 使用 `RestTemplate` ```java import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; public class ModuleCaller { public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); String url = "http://localhost:8001/api/b2b2c/dept/query"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); String deptName = response.getBody(); System.out.println("部门名称: " + deptName); } } ``` #### 使用 `WebClient` ```java import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; public class ModuleCallerWebClient { public static void main(String[] args) { WebClient webClient = WebClient.create(); Mono<String> deptNameMono = webClient.get() .uri("http://localhost:8001/api/b2b2c/dept/query") .retrieve() .bodyToMono(String.class); String deptName = deptNameMono.block(); System.out.println("部门名称: " + deptName); } } ``` ### 4. 启动应用 确保被调用模块的服务已经启动,调用模块才能成功调用接口。以 `MemberApplication` 为例: ```java package com.mengxuegu.member; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MemberApplication { public static void main(String[] args) { SpringApplication.run(MemberApplication.class, args); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dlage

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值