View Full Version : Is a servlet container a prerequisite for running a spring ?
joergen
Oct 5th, 2004, 07:56 AM
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
cmgharris
Oct 5th, 2004, 08:56 AM
Yes to both questions.
For example, something like:
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:
<beans>
<bean id="pafService" class="foo.bar.ServerImpl" />
<bean class="org.springframework.remoting.rmi.RmiServiceExporte r">
<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>
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.
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
joergen
Oct 5th, 2004, 03:43 PM
It does certainly seems to be a remarkebly easy and flexible -
thanks a lot.
alexis
Nov 22nd, 2004, 05:45 PM
I can't understand why this code registers my bean :
new ClassPathXmlApplicationContext("beans.xml");
but this one doesn't :
ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
The fact is I tried the second one first when I read Spring's reference (3.2.1. The BeanFactory).
Here is my beans.xml file
<?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.RmiServiceExporte r">
<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>
Any idea ?
gpoirier
Dec 7th, 2004, 10:50 AM
The reason is that BeanFactories don't eagerly instanciate singletons, while ApplicationContextes do. When using a BeanFactory, you have to call preInstantiateSingletons() explicitely.
Guillaume
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.