Is it possible to expose remote services without using a servlet/JSP container (for example by using the RMI registry) with spring ?
And, is it possible to create a standalone application without a servlet/JSP container ?
I
Is it possible to expose remote services without using a servlet/JSP container (for example by using the RMI registry) with spring ?
And, is it possible to create a standalone application without a servlet/JSP container ?
I
Yes to both questions.
For example, something like:
Code:public class ServerImpl extends UnicastRemoteObject implements ServerIface { public ServerImpl() throws RemoteException { } public static void main(String[] args) { new ClassPathXmlApplicationContext("ServerContext.xml"); } // Remote interface implementation follows here: }
with a Spring context (ServerContext.xml) looking like:
I've tried to cut out extraneous detaiil, but I hope I've got the essentiials there. When you run the main method, your service will be available on the ports you've specified. You don't need to be running a separate registry.Code:<beans> <bean id="pafService" class="foo.bar.ServerImpl" /> <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="serviceName"><value>pafService</value></property> <property name="service"><ref bean="pafService"/></property> <property name="serviceInterface"><value>foo.bar.ServerIface</value></property> <property name="registryPort"><value>yourRegistryPortNo</value></property> <property name="servicePort"><value>yourServicePortNo</value></property> </bean> </beans>
For an ordinary stand alone app, your spring context will be different - you won't have the RmiServiceExporter - and after loading the context, you'll probably want to do a bit more in your main method.
Hope this helps
Chris Harris
Carlisle, UK
It does certainly seems to be a remarkebly easy and flexible -
thanks a lot.
I can't understand why this code registers my bean :
but this one doesn't :Code:new ClassPathXmlApplicationContext("beans.xml");
The fact is I tried the second one first when I read Spring's reference (3.2.1. The BeanFactory).Code:ClassPathResource res = new ClassPathResource("beans.xml"); XmlBeanFactory factory = new XmlBeanFactory(res);
Here is my beans.xml file
Any idea ?Code:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="HelloServer" class="aot.essai.spring.server.HelloServer"/> <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="serviceName"><value>HelloService</value></property> <property name="service"><ref bean="HelloServer"/></property> <property name="serviceInterface"><value>aot.essai.spring.Hello</value></property> <!-- defaults to 1099 --> <property name="registryPort"><value>1199</value></property> </bean> </beans>
The reason is that BeanFactories don't eagerly instanciate singletons, while ApplicationContextes do. When using a BeanFactory, you have to call preInstantiateSingletons() explicitely.
Guillaume