Org.springframework.context.ApplicationContextAware Use understanding

Source: Internet
Author: User

What is the use of this interface?

Once a class implements this interface (Applicationcontextaware), this class can easily get all the beans in the ApplicationContext. In other words, this class can get directly from the spring configuration file, all the bean objects that are referenced to.

Second, how to use?

Let's take an example:

For example, I have a method class Apputil, a bean (companyservice) in the applicationcontext that needs to be used in this method class.

1. Because spring wants to build its own container, it has to load its own configuration file.

At this point, you need to register Contextloaderlistener or subclasses of this class.

Add the following information to Web. XML:

< Listener >        < Listener-class >org.springframework.web.context.ContextLoaderListener</listener-class  ></listener>

Of course, this will only read the Application.xml configuration file under the default path. If you need to read the configuration file under a specific path. Need to be in Web. xml

Add the following information. You can refer to my example and specify the configuration file as follows:

< Context-param >        < Param-name >contextconfiglocation</param-name>        <  Param-value>classpath:conf/app-context.xml</param-value>  </context-param>

Note that:<param-name>contextconfiglocation</param-name> cannot be changed.

2. Treatment of method class Apputil

The method class Apputil implements the Applicationcontextaware interface:

public class Apputil    implements Applicationcontextaware

Adds a static member ApplicationContext type object to the method class Apputil. After the method class Apputil gets ApplicationContext, it is by reading this

of the member variable. The following are the specific examples:

private static ApplicationContext AppContext;

The default method for implementing the Applicationcontextaware interface:

public void Setapplicationcontext (ApplicationContext paramapplicationcontext)     throws Beansexception     {        AppContext = Paramapplicationcontext;     }        

  

3, in the spring configuration file, the Registration method class Apputil

Strictly speaking, the method class Apputil is a bean, and it is not difficult to find out from step 2 that the method class Apputil has the flexibility to get ApplicationContext

It's because spring can automate the setapplicationcontext for us. However, Spring does not perform its methods for a class for no reason, so it is necessary

By registering the method class, Apputil tells Spring that there is such a class.

In fact, the method is simply to register the method class Apputil as a normal bean in the spring configuration file:

<id= "Apputil"  class= "Com.htsoft.core.util.AppUtil"/ >

4. Use static member ApplicationContext type of object, AppContext, to invoke other beans. Add the following method to the method class Apputil:

public static Object Getbean (String paramstring)  {    return Appcontext.getbean (paramstring);  }

Well, in

The method class Apputil has the flexibility to invoke any other bean, for example:

Companyservice Localcompanyservice = (companyservice) getbean ("Companyservice");

Note: The contents of the configuration file about Companyservice:

<id= "Companyservice"  class= " Com.kaiwii.service.system.impl.CompanyServiceImpl ">        <Index  = "0"  ref= "Companydao"/>      </Bean  >

Can be combined with the following links to see:

Get spring's ApplicationContext and Bean objects manually

Turn from: Org.springframework.context.ApplicationContextAware Use understanding

Org.springframework.context.ApplicationContextAware Use understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.