A simple example to demonstrate, how to use Aliases when referring to a bean configuration file in Spring Framework
1. Create a Dog class
2. Create a Spring configuration file say spring.xml and place it in classpath i.e src folder of your Eclipse
3. Create a test class and run to see the output below :
Output of the program :
1. Create a Dog class
package com.hubberspot.spring;
public class Dog {
private String tail;
private String coat;
private String breed;
public String getTail() {
return tail;
}
public void setTail(String tail) {
this.tail = tail;
}
public String getCoat() {
return coat;
}
public void setCoat(String coat) {
this.coat = coat;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void move() {
System.out.println("A Dog of breed "+ getBreed() +", " +
"having "+ getCoat() +"\nand "+ getTail() + " " +
"tail moves slowly ...");
}
}
2. Create a Spring configuration file say spring.xml and place it in classpath i.e src folder of your Eclipse
|
3. Create a test class and run to see the output below :
package com.hubberspot.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WildLifeApplication {
public static void main(String[] args) {
// ApplicationContext is a Spring interface which
// provides with the configuration for an application.
// It provides us with all the methods that BeanFactory
// provides. It loads the file resources in a older
// and generic manner. It helps us to publish events to the
// listener registered to it. It also provides quick support
// for internationalization. It provides us with the object
// requested, it reads the configuration file and provides
// us with the necessary object required.
// We are using concrete implementation of ApplicationContext
// here called as ClassPathXmlApplicationContext because this
// bean factory reads the xml file placed in the classpath of
// our application. We provide ClassPathXmlApplicationContext
// with a configuration file called as spring.xml placed
// at classpath of our application.
ApplicationContext context =
new ClassPathXmlApplicationContext(("spring.xml"));
// In order to get a object instantiated for a particular bean
// we call getBean() method of ClassPathXmlApplicationContext
// passing it the id for which the object is to be needed.
// Here getBean() returns an Object. We need to cast it back
// to the Dog object. Without implementing new keyword we
// have injected object of Dog just by reading an xml
// configuration file.
Dog dog = (Dog)context.getBean("dog");
// Calling our functionality
dog.move();
// getting the same bean of dog by calling the alias of dog
// alias help us by providing different name to a single
// bean as per our design needs.
// check for changes done in spring.xml file
dog = (Dog)context.getBean("dog-alias");
// Calling our functionality
dog.move();
}
}
Output of the program :
