Announcement Announcement Module
Collapse
No announcement yet.
Help:simple RMI example Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help:simple RMI example

    I'm a new comer for Spring RMI and try to finish a simplest example according to Spring DOC.

    The only different thing is I try to use a servlet in the client side.

    The following is the fragment of client web.xml
    Code:
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param>
    The web.xml of server is the same.

    The client side applicationContext.xml:
    Code:
    <beans>
    <bean class="au.com.client.ClientObject">
    	<property name="accountService" ref="remoteAccountService"/>
    </bean>
    
    <bean id="remoteAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    	<property name="serviceUrl" value="rmi://HOST/AccountService"/>
    	<property name="serviceInterface" value="au.com.vodafone.service.AccountService"/>
    </bean>
    
    </beans>
    And in the client side servlet:
    Code:
    		ClientObject c = new ClientObject();
    		AccountService a= c.getAccountService();
    ClientObject:
    Code:
    package au.com.client;
    
    import au.com.vodafone.service.AccountService;
    
    public class ClientObject {
    	private AccountService accountService;
    
    	public AccountService getAccountService() {
    		return accountService;
    	}
    	public void setAccountService(AccountService accountService) {
    		this.accountService = accountService;
    	}
    
    }
    The error message I got shows that "AccountService a is null". I think the problem might be the configuration in web.xml.

    Thanks!

  • #2
    You need to retrieve the ClientObject from a Spring application context, not create it with new. When you create it with new Spring is not involved and therefore the accountService property won't be set.

    Comment


    • #3
      Thanks

      Yes, that‘s right!

      I should use ApplicationContext.getBean() something like that.

      Comment


      • #4
        Yes. And if you initialize a web context in your web.xml you would use a WebApplicationContext which gets bound to your servlet context on startup by a ContextLoaderListener or ContextLoaderServlet (depending on which one you use)

        So you could set your ClientObject as a property of your servlet init:

        Code:
        private ClientObject clientObject;
        private WebApplicationContext context;
        public void init(ServletConfig config) {
          context = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
          clientObject = context.getBean("myClientObject"); // have to set a name in your applicationContext.xml
        }
        Last edited by wpoitras; Aug 22nd, 2006, 09:48 AM.

        Comment

        Working...
        X