By
implementing ApplicationContextAware interface, you can access
ApplicationContext. When spring instantiates beans, it looks for a some of
interfaces like ApplicationContextAware. If any class implements ApplicationContextAware
interface, then spring makes ApplicationContext available to the class.
Step 1
: Create new
maven project “spring_tuorial”. Project structure looks like below.
Step 2
: Update
“pom.xml” file for maven dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring_tutorial</groupId> <artifactId>spring_tutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>spring_tutorial</name> <description>spring_tutorial</description> <properties> <org.springframework-version>4.1.5.RELEASE</org.springframework-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> </dependencies> </project>
Step
3: Create new
package “com.springtutorial.model” under “src/main/java”.
Step
4: Create
“Address” class under the package “com.springtutorial.model”.
package com.springtutorial.model; public class Address { private String street; private String city; private String state; private String country; private String pin; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getPin() { return pin; } public void setPin(String pin) { this.pin = pin; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Address [street=").append(street).append(", city=").append(city) .append(", state=").append(state).append(", country=").append(country).append(", pin=") .append(pin).append("]"); return builder.toString(); } }
Step
5: Create
“Employee” class under the package “com.springtutorial.model”.
package com.springtutorial.model; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class Employee implements ApplicationContextAware { private String firstName; private String lastName; private int id; private Address homeAddress; private Address officeAddress; private ApplicationContext applicationContext; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Address getHomeAddress() { return homeAddress; } public Address getOfficeAddress() { return officeAddress; } public String getDetails() { return applicationContext.getBean("homeAddress") + "\n" + applicationContext.getBean("officeAddress"); } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Employee [firstName=").append(firstName).append(", lastName=").append(lastName) .append(", id=").append(id).append(", homeAddress=").append(homeAddress) .append(", officeAddress=").append(officeAddress).append("]"); return builder.toString(); } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
Step
6: Create
“spring.xml” file in “src/main/resources”.
spring.xml
<?xml version="1.0" encoding="UTF-8"?> <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="employee1" class="com.springtutorial.model.Employee"> <property name="firstName" value="Hari Krishna" /> <property name="lastName" value="Gurram" /> <property name="id" value="553" /> </bean> <bean id="homeAddress" class="com.springtutorial.model.Address"> <property name="street" value="Chowdeswari temple" /> <property name="city" value="Bangalore" /> <property name="state" value="Karnataka" /> <property name="country" value="India" /> <property name="pin" value="560037" /> </bean> <bean id="officeAddress" class="com.springtutorial.model.Address"> <property name="street" value="Jakkasandra" /> <property name="city" value="Bangalore" /> <property name="state" value="Karnataka" /> <property name="country" value="India" /> <property name="pin" value="560031" /> </bean> </beans>
"spring.xml"
is used to assign unique IDs to different beans, to control the creation of
objects with different values. You have to make sure that “spring.xml” file is
available in CLASSPATH and use the same name in main application while creating
application context as shown in Main.java file.
Step 7
: Create package
“com.springtutorial.main” under “src/main/java”.
Step 8
: Create Main
class under package “com.springtutorial.main”.
package com.springtutorial.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.springtutorial.model.Employee; public class Main { public static void main(String args[]) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Employee emp1 = (Employee) context.getBean("employee1"); System.out.println(emp1.getDetails()); } }
Step 9
: Run
Main.java,you will get output like below.
Mar 23, 2015 1:42:49 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1509f8b: startup date [Mon Mar 23 01:42:49 IST 2015]; root of context hierarchy Mar 23, 2015 1:42:49 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring.xml] Address [street=Chowdeswari temple, city=Bangalore, state=Karnataka, country=India, pin=560037] Address [street=Jakkasandra, city=Bangalore, state=Karnataka, country=India, pin=560031]
No comments:
Post a Comment