<span id="Label3"></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">What is the use of this interface?</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">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.</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">second, How to use?</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Let's take an example:</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">For example, I have a method class apputil, a bean (companyservice) in the applicationcontext that needs to be used in this method class.</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">1. Because spring wants to build its own container, it has to load its own configuration File.</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">At this point, you need to register Contextloaderlistener or subclasses of this class.</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Add the following information to Web. Xml:</p></p><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span>Listener<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><br><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span>Listener<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">-</span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">class</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span>Org.springframework.web.context.ContextLoaderListener<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">/</span></span>Listener<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">-</span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">class</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><br><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">/</span></span>Listener<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">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</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Add the following Information. You can refer to my example and specify the configuration file as Follows:</p></p><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span>Context<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">-</span></span>Param<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><br><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span>Param<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">-</span></span>Name<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span>Contextconfiglocation<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">/</span></span>Param<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">-</span></span>Name<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><br><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span>Param<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">-</span></span>Value<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span>Classpath<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">:</span></span>Conf<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">/</span></span>App<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">-</span></span>Context.xml<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">/</span></span>Param<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">-</span></span>Value<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><br><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">/</span></span>Context<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">-</span></span>Param<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(255,102,102)">Note That:<param-name>contextconfiglocation</param-name> cannot be changed.</span></p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(255,102,102)"></span></p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">2. Treatment of method class Apputil</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">The method class Apputil implements the Applicationcontextaware interface:</p></p><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)"></span> public</span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">class</span></span>Apputil<br>Implements Applicationcontextaware<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Adds a static member ApplicationContext type object to the method class Apputil. After the method class Apputil gets applicationcontext, It is by reading this</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">of the member Variable. The following are the specific examples:</p></p><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Private</span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Static</span></span>ApplicationContext AppContext<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">;</span></span><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">The default method for implementing the Applicationcontextaware Interface:</p></p><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)"></span> public</span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">void</span></span>Setapplicationcontext<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">(</span></span>ApplicationContext Paramapplicationcontext<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">)</span></span><br>Throws Beansexception<br> <span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">{</span></span><br>AppContext<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">=</span></span>Paramapplicationcontext<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">;</span></span><br><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">}</span></span><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">3, in the spring configuration file, the registration method class Apputil</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">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</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">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</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">By registering the method class, Apputil tells Spring that there is such a class.</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">In fact, the method is simply to register the method class Apputil as a normal bean in the spring configuration file:</p></p><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span>Bean ID<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">=</span></span><span style="color:rgb(255,0,0)"><span style="color:rgb(255,0,0)">"apputil"</span></span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">class</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">=</span></span><span style="color:rgb(255,0,0)"><span style="color:rgb(255,0,0)">"com.htsoft.core.util.AppUtil"</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">/</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">4. Use static member ApplicationContext type of object, appContext, to invoke other Beans. Add the following method to the method class Apputil:</p></p><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)"></span> public</span><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">Static</span></span>Object Getbean<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">(</span></span>String paramstring<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">)</span></span><br><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">{</span></span><br><span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">return</span></span>Appcontext.getbean<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">(</span></span>Paramstring<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">);</span></span><br><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">}</span></span><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">well, in</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">The method class Apputil has the flexibility to invoke any other bean, for example:</p></p>Companyservice Localcompanyservice<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">=</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">(</span></span>Companyservice<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">)</span></span>Getbean<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">(</span></span><span style="color:rgb(255,0,0)"><span style="color:rgb(255,0,0)">"companyservice"</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">);</span></span><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Note: the contents of the configuration file about Companyservice:</p></p><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span>Bean ID<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">=</span></span><span style="color:rgb(255,0,0)"><span style="color:rgb(255,0,0)">"companyservice"</span></span> <span style="color:rgb(0,0,255)"><span style="color:rgb(0,0,255)">class</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">=</span></span><span style="color:rgb(255,0,0)"><span style="color:rgb(255,0,0)">"com.kaiwii.service.system.impl.CompanyServiceImpl"</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><br><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span>Constructor<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">-</span></span>ARG index<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">=</span></span><span style="color:rgb(255,0,0)"><span style="color:rgb(255,0,0)">"0"</span></span>Ref<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">=</span></span><span style="color:rgb(255,0,0)"><span style="color:rgb(255,0,0)">"companydao"</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">/</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><br><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)"><</span></span><span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">/</span></span>Bean<span style="color:rgb(128,0,0)"><span style="color:rgb(128,0,0)">></span></span><p><p>Org.springframework.context.ApplicationContextAware Usage Explanation</p></p></span>
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