Java Spring在初始化后返回空bean
我有一个无法解释的情况。 我试图创建一个调用Spring服务的RMI服务器,但是我无法将bean绑定到RMI注册表,因为它们都是空的。 代码如下所示:Java Spring在初始化后返回空bean,java,spring,rmi,Java,Spring,Rmi,我有一个无法解释的情况。 我试图创建一个调用Spring服务的RMI服务器,但是我无法将bean绑定到RMI注册表,因为它们都是空的。 代码如下所示: public class RMIServer { private static final Logger LOG = LoggerFactory.getLogger(RMIServer.class); public static void main(final String[] args) throws RemoteExcep
public class RMIServer {
private static final Logger LOG = LoggerFactory.getLogger(RMIServer.class);
public static void main(final String[] args) throws RemoteException, AlreadyBoundException {
final Registry registry = LocateRegistry.createRegistry(RmiConstants.RMI_PORT);
final ApplicationContext ctx = new AnnotationConfigApplicationContext(RmiConfiguration.class);
for (final String key : ctx.getBeansOfType(BaseRmi.class).keySet()) {
LOG.info("Registering {}...", key);
registry.bind(key, (BaseRmi) ctx.getBean(key));
}
LOG.info("RMI server was started...");
}
}
Spring配置类是:
@Configuration
@ImportResource({ "classpath*:app-context.xml" })
public class RmiConfiguration {
@Bean
AccountRmi accountRmi() {
try {
return new AccountRmiImpl();
} catch (final RemoteException e) {
return null;
}
}
}
我要实例化的bean如下:
public class AccountRmiImpl extends BaseRmi implements AccountRmi {
private static final long serialVersionUID = 5798106327227442204L;
private final static Logger LOG = LoggerFactory.getLogger(AccountRmiImpl.class.getName());
@Autowired
private AccountService accountService;
public AccountRmiImpl() throws RemoteException {
super();
}
@Override
public List<PersonType> getPersonTypes() throws AppException {
return accountService.getPersonTypes();
}
}
此bean的接口如下所示:
public interface AccountRmi extends AccountFacade, java.rmi.Remote {
}
其中AccountFacade包含业务逻辑
我看到的是,如果我删除AccountRmi interfaces声明上的java.rmi.Remote接口,bean将被实例化,但我需要该接口进行远程查找。日志中未显示任何错误。spring对bean声明中的多个接口有限制吗?还是仅仅因为java.rmi.Remote接口?
如有要求,我可以提供更多细节
非常感谢,,
Daniel您在任何地方都有@组件吗?配置会创建bean。我不认为我需要在accountrmimpl上添加注释,但我会尝试。感谢您在@Bean方法中屏蔽了RemoteException异常。这似乎不谨慎。如果事实上bean是必需的,那么为什么要隐藏任何构造函数异常呢。我将对此进行一些更改。另外,facade方法需要抛出RemoteException异常,即使我有AppException扩展RemoteException。为什么呢?谢谢
public interface AccountRmi extends AccountFacade, java.rmi.Remote {
}