ApplicationContextaware Works

I need to know how applicationconcontextaware works. I have applicationContext.xml that has some import resource (another applicationContext). I need to use applicationContext.xml in my java class to use spring beans in it.

I introduced the applicationcontextaware class, which is used to get spring beans inside the java.Applicationaware class has set and getapplicationcontext () methods. getapplicationcontext () is defined as static.

How does an applicationtextware application load applicationContext.xml? Do I need to specify the location of applicationContext.xml in order to download the contextaware application? How can I use it in my java class?

+3
source share
1

. ApplicationContextAware , ? :

setApplicationContext(ApplicationContext applicationContext)

:

public class MyFancyBean implements ApplicationContextAware {

  private ApplicationContext applicationContext;

  void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
  }

  public void businessMethod() {
    //use applicationContext somehow
  }

}

ApplicationContext. , beans .

applicationContext.xml java spring beans .

:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

, , applicationContext.xml. bean :

ctx.getBean("someName")

, spring - ContextLoaderListener, @Configuration ..

+15

All Articles