如何从Spring Boot服务层调用Java类方法

huangapple go评论66阅读模式
英文:

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 
    } 

}

huangapple
  • 本文由 发表于 2020年9月24日 04:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64035524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定