Aware Injection | Interface Injection
ApplicationContextAware Injection Example:-
If underlying container injects objects/values to target class only when target class implements certain interface either directly or indirectly then it is called interface injection.
It is also known as Aware Injection because by implementing certain interface in target class, we made the container aware that it has to inject objects/values to target class.
Here target class(Vehicle) is implementing ApplicationContextAware interface so it is also known as ApplicationContextAware Injection.
Here Aware injection is not used to inject simple and regular values like name, age, driver class name, datasource etc. It is used only to inject container managed special object and values like bean ids, BeanFactory object, ApplicationContext object and etc...
It is also known as Aware Injection because by implementing certain interface in target class, we made the container aware that it has to inject objects/values to target class.
Here target class(Vehicle) is implementing ApplicationContextAware interface so it is also known as ApplicationContextAware Injection.
Here Aware injection is not used to inject simple and regular values like name, age, driver class name, datasource etc. It is used only to inject container managed special object and values like bean ids, BeanFactory object, ApplicationContext object and etc...
Project Structure :- (Three packages)
com.nt.beans
--> Engine.java --> Vehicle.java com.nt.cfgs --> applicationContext.xml com.nt.client --> ClientTest.java |
Engine.java
Vehicle.java
package com.nt.beans;
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class Vehicle implements ApplicationContextAware { private ApplicationContext ctx=null; public Vehicle() { System.out.println("Vehicle ::0-param constructor"); } public void setApplicationContext(ApplicationContext ctx) throws BeansException { System.out.println("Vehicle.setApplicationContext()"); this.ctx=ctx; } public void journey(String source, String destination) { Engine engine=null; engine=ctx.getBean("engg",Engine.class); engine.start(); System.out.println("Journey is going on from "+source+" to "+destination); engine.stop(); } } |
applicationContext.xml
ClientTest.java
package com.nt.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.nt.beans.Vehicle;
public class ClientTest
{
public static void main(String[] args)
{
ApplicationContext ctx=null;
ctx=new ClassPathXmlApplicationContext("com/nt/cfgs/applicationContext.xml");
Vehicle v= ctx.getBean("vehicle",Vehicle.class);
v.journey("Hyderabad", "Banglore");
((AbstractApplicationContext) ctx).close();
}
}
|
Output :-
Engine ::0-param constructor
Vehicle ::0-param constructor
Vehicle.setApplicationContext()
Engine started....
Journey is going on from Hyderabad to Banglore
Engine stopped....
|
Post a Comment