How to inject spring application context

Spring application context can be injected into class and this can be achieved by interface provided in the spring. There is an interface called ApplicationContextAware ,by implementing this interface we can have spring context injected into the class.

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
class MyClass implements ApplicationContextAware
 private  ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
spring context is injected when container loads, by using setter method available in this class.


Post a Comment

Powered by Blogger.