我有一个spring引导应用程序,在那里我尝试注入rmi服务。
目前我的rmi服务器运行的代码如下:
public static Registry registry;
public static void main(String[] args) {
try
{
// Create a server registry at default port 1099
registry = LocateRegistry.createRegistry(1099);
// Create engine of remote methods, running on the server
RMIMethodsImpl remoteMethods = new RMIMethodsImpl();
// Give a name to this engine
String engineName = "moonlodge";
// Register the engine by the name, which later will be given to the clients
Naming.rebind("//localhost/" + engineName, remoteMethods);
}
然后,我尝试在我的一个服务中示例化rmi服务,我用 @Component
注解:
@Component
public class HotelManagerService extends UnicastRemoteObject implements HotelManagerInterface {
HotelManagerInterface hotelManagerService;
protected HotelManagerService() throws RemoteException, MalformedURLException, NotBoundException {
String remoteEngine = "rmi://localhost:1099/moonlodge";
this.hotelManagerService = (HotelManagerInterface) Naming.lookup(remoteEngine);
}
我也试过了 @Autowired
注解,将其创建为依赖项注入,但没有任何运气。
为了确保springboot应用程序“知道”我将服务接口放在 @ComponentScan
我的主类中的注解,在这里我开始我的应用程序:
@SpringBootApplication
@ComponentScan(basePackageClasses = {HotelManagerController.class, HotelManagerInterface.class, HotelManagerService.class})
public class MoonlodgeFrontendApplication {
public static void main(String[] args) {
SpringApplication.run(MoonlodgeFrontendApplication.class, args).getBean(HotelManagerInterface.class);
}
我还确保(服务的)接口扩展了 Remote
像这样的接口
...
我得到一个例外:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hotelManagerController': Unsatisfied dependency expressed through field 'hotelManagerService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelManagerService' defined in file [/home/kristoffer/Desktop/projects/schoolwork/moonlodge-frontend/target/classes/moonlodge/frontend/moonlodgefrontend/service/HotelManagerService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [moonlodge.frontend.moonlodgefrontend.service.HotelManagerService]: Constructor threw exception; nested exception is java.lang.ClassCastException: class com.sun.proxy.$Proxy52 cannot be cast to class moonlodge.frontend.moonlodgefrontend.contract.interfaces.HotelManagerInterface (com.sun.proxy.$Proxy52 and moonlodge.frontend.moonlodgefrontend.contract.interfaces.HotelManagerInterface are in unnamed module of loader 'app')
这是什么 $Proxy52
,为什么我不能得到真正的服务??
暂无答案!
目前还没有任何答案,快来回答吧!