[英]How to call a java class method from spring boot service layer
I have my Spring boot application from where I am trying to call a method from a java class in a different module outside of my application.我有我的 Spring Boot 应用程序,我试图从我的应用程序外部不同模块中的 java 类调用方法。 I have the dependency added to my project pom for that external module.我已将依赖项添加到该外部模块的项目 pom 中。 The external module is not Spring boot application.外部模块不是 Spring Boot 应用程序。 I have my code as below我的代码如下
@Service
public class MyServiceImpl {
private MyInterface myInterface;
public Response callMethod1() {
Response response = new Response();
response = myInterface.callMethod1();
return response;
}
}
Interface MyInterface and its implementation are part of the external module.接口 MyInterface 及其实现是外部模块的一部分。 Code is something like this.代码是这样的。
public interface MyInterface {
Response callMethod1();
}
Implementation:执行:
public class InterfaceImpl implements MyInterface {
Response callMethod1() {
Response response = doSomething();
return response;
}
}
When I call the service method, I see it failing with InvocationTargetException.当我调用服务方法时,我看到它因 InvocationTargetException 而失败。 I tried the debugger in eclipse and I won't see the break points in the Implementation getting hit.我在 eclipse 中尝试了调试器,但我看不到实现中的断点。 I tried by moving the code of Interface and Implementation into my project with some modifications to see if I can make it work.我尝试将接口和实现的代码移动到我的项目中并进行一些修改,看看我是否可以使其工作。 Here are my changes.这是我的更改。
Service Class:服务等级:
@Service
public class MyServiceImpl {
@Autowired
private MyInterface myInterface;
public Response callMethod1() {
Response response = new Response();
response = myInterface.callMethod1();
return response;
}
}
Interface:界面:
public interface MyInterface {
Response callMethod1();
}
Implementation:执行:
@Service
public class InterfaceImpl implements MyInterface {
Response callMethod1() {
Response response = doSomething();
return response;
}
}
How can I call the java class methods without changing the implementation and keeping it in its original module.如何在不更改实现并将其保留在其原始模块中的情况下调用 java 类方法。
Thanks.谢谢。
@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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.