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

如何从spring引导服务层调用java类方法,java,spring-boot,Java,Spring Boot,我有我的Spring boot应用程序,在这里我试图从我的应用程序之外的另一个模块中的java类调用一个方法。我已将该依赖项添加到该外部模块的项目pom中。外部模块不是Spring引导应用程序。我的代码如下 @Service public class MyServiceImpl { private MyInterface myInterface; public Response callMethod1() { Response response = ne

我有我的Spring boot应用程序,在这里我试图从我的应用程序之外的另一个模块中的java类调用一个方法。我已将该依赖项添加到该外部模块的项目pom中。外部模块不是Spring引导应用程序。我的代码如下

@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 interface MyInterface {
    Response callMethod1();
}
实施:

public class InterfaceImpl implements MyInterface {
    Response callMethod1() {
        Response response = doSomething();
        return response;
    }
}

@Service
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 class InterfaceImpl implements MyInterface {
    Response callMethod1() {
        Response response = doSomething();
        return response;
    }
}

@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 
    } 

}