简体   繁体   English

如何从spring boot服务层调用java类方法

[英]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.

相关问题 Spring Boot - 如何仅通过提供的服务类名称从服务类调用方法? - Spring Boot - How to call method from a service class just by the provided service class name? 在Spring Boot中如何从实体类中调用基于服务的类 - How to call service based classes from entity class in spring boot 将 @Transactional 方法结果从 Service 传递到 Controller 层 Spring Boot - Pass @Transactional method result from Service to Controller Layer Spring Boot 如何从 Spring Boot 调用 SOAP 服务 - How to call SOAP service from Spring boot 如何从java中的REST web服务的Service层中的单元测试中调用@RestControllerAdvice class? - How to call @RestControllerAdvice class from a unit test in the Service layer of a REST web service in java? 如何在spring boot中验证服务层中的数据 - How to validate data in service layer in spring boot 如何再次从java类的服务层函数再次调用ajax以显示页码状态? - How to call ajax again n again from service layer function of java class to show page number status? 在服务 class 中创建方法以从 Spring 引导中的存储库获取数据 - Creating a method in Service class to get data from Repository in Spring Boot Spring 来自服务 class 的引导方法调用问题 - Spring Boot method calling issue from the service class 使用Spring从控制器层调用存储库和服务 - Call repository and service from controller layer with Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM