英文:
How to call a java class method from spring boot service layer
问题
以下是翻译好的部分:
我有一个 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 类的方法呢?
谢谢。
英文:
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. I have the dependency added to my project pom for that external module. The external module is not Spring boot application. 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. 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. I tried the debugger in eclipse and I won't see the break points in the Implementation getting hit. 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.
Thanks.
答案1
得分: 0
@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 
    } 
}
英文:
@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 
    } 
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论