java - 如何从spring boot服务层调用java类方法
问题描述
我有我的 Spring Boot 应用程序,我试图从我的应用程序之外的不同模块中的 java 类调用方法。我已将依赖项添加到该外部模块的项目 pom 中。外部模块不是 Spring Boot 应用程序。我的代码如下
@Service
public class MyServiceImpl {
private MyInterface myInterface;
public Response callMethod1() {
Response response = new Response();
response = myInterface.callMethod1();
return response;
}
}
接口 MyInterface 及其实现是外部模块的一部分。代码是这样的。
public interface MyInterface {
Response callMethod1();
}
执行:
public class InterfaceImpl implements MyInterface {
Response callMethod1() {
Response response = doSomething();
return response;
}
}
当我调用服务方法时,我看到它因 InvocationTargetException 而失败。我在 Eclipse 中尝试了调试器,但我不会看到实现中的断点被击中。我尝试将接口和实现的代码移动到我的项目中,并进行一些修改,看看我是否可以让它工作。这是我的更改。
服务等级:
@Service
public class MyServiceImpl {
@Autowired
private MyInterface myInterface;
public Response callMethod1() {
Response response = new Response();
response = myInterface.callMethod1();
return response;
}
}
界面:
public interface MyInterface {
Response callMethod1();
}
执行:
@Service
public class InterfaceImpl implements MyInterface {
Response callMethod1() {
Response response = doSomething();
return response;
}
}
如何在不更改实现并将其保留在其原始模块中的情况下调用 java 类方法。
谢谢。
解决方案
@Configuration
public class Config {
@Bean
public Foo createFoo() {
Foo foo = new Foo(); // this is POJO
return foo; // after return it will be saved in Spring Context and become a Bean
}
}
推荐阅读
- c - 创建一个 ASIO DSD 播放器
- javascript - 如何在 Angular 中获取活动路由快照 onpopstate 事件
- arrays - ReactJS如何在分页中一次显示5个页码
- javascript - 输入上的 onChange 不使用 setState 更新状态
- woocommerce - 如何在woocommerce中显示销售日期
- python - 将自定义字段从 MO BoM 行发送到 Odoo 中的 PO 订单行
- excel - 如何释放形状对象变量或重置它?
- javascript - 如何使用 Promise 返回函数调用
- c# - 使用库中的 netcoreapp2.1 API
- go - 如何创建两个列表的地图,其中一个列表又是地图?