Wednesday, August 1, 2012

Spring Lookup Method


Contact:
package com.lnn.method.lookup;

public abstract class Contact {
      public abstract Name getName();
}
Name:
package com.lnn.method.lookup;

public class Name {
      private String first;
      private String last;

      public Name(String first, String last) {
            super();
            this.first = first;
            this.last = last;
      }

      @Override
      public String toString() {
            return "Name [first=" + first + ", last=" + last + "]";
      }
}

lookup.xml
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
      <bean id="name" class="com.lnn.method.lookup.Name">
            <constructor-arg value="Leninkumar" />
            <constructor-arg value="Koppoju" />
      </bean>
      <bean id="contact" class="com.lnn.method.lookup.Contact">
            <lookup-method name="getName" bean="name" />
      </bean>
</beans>
Test:
package com.lnn.method.lookup;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
      public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("lookup.xml");
            Contact contact = context.getBean("contact", Contact.class);
            System.out.println(contact.getName());
      }
}

No comments:

Post a Comment