Announcement Announcement Module
Collapse
No announcement yet.
Is a servlet container a prerequisite for running a spring ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is a servlet container a prerequisite for running a spring ?

    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

  • #2
    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:
    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>
    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

    Comment


    • #3
      It does certainly seems to be a remarkebly easy and flexible -
      thanks a lot.

      Comment


      • #4
        I can't understand why this code registers my bean :
        Code:
        new ClassPathXmlApplicationContext&#40;"beans.xml"&#41;;
        but this one doesn't :
        Code:
        ClassPathResource res = new ClassPathResource&#40;"beans.xml"&#41;;
        XmlBeanFactory factory = new XmlBeanFactory&#40;res&#41;;
        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
        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
        	"http&#58;//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>
        Any idea ?

        Comment


        • #5
          The reason is that BeanFactories don't eagerly instanciate singletons, while ApplicationContextes do. When using a BeanFactory, you have to call preInstantiateSingletons() explicitely.

          Guillaume

          Comment

          Working...
          X