Loading the spring context through Applicationcontextaware

Source: Internet
Author: User

Loading the spring context through Applicationcontextawareoriginal September 05, 2013 18:15:44
    • Label:
    • Spring/
    • Applicationcontextaw

The project uses Applicationcontextaware, through which the spring container automatically calls the context object to the Setapplicationcontext method in the Applicationcontextaware interface.

In the Applicationcontextaware implementation class, we can get the beans in the spring container through this context object.

Here's how to use it:

1. Implement the Applicationcontextaware interface:

[Java]View PlainCopy
  1. Package com.bis.majian.practice.module.spring.util;
  2. Import org.springframework.beans.BeansException;
  3. Import Org.springframework.context.ApplicationContext;
  4. Import Org.springframework.context.ApplicationContextAware;
  5. Public class Springcontexthelper implements Applicationcontextaware {
  6. private static ApplicationContext context = null;
  7. @Override
  8. public void Setapplicationcontext (ApplicationContext applicationcontext)
  9. throws Beansexception {
  10. context = ApplicationContext;
  11. }
  12. public static Object Getbean (String name) {
  13. return Context.getbean (name);
  14. }
  15. }


2. Configure this class in the spring configuration file, and the spring container will invoke the Setapplicationcontext method in the object after loading the spring container:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http// Www.springframework.org/schema/tx "
  4. xmlns:context="Http://www.springframework.org/schema/context"
  5. xsi:schemalocation= "Http://www.springframework.org/schema/beans
  6. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. Http://www.springframework.org/schema/tx
  8. Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  9. Http://www.springframework.org/schema/context
  10. Http://www.springframework.org/schema/context/spring-context-3.0.xsd " default-autowire="byname ">
  11. <Bean id= "springcontexthelper" class=" Com.bis.majian.practice.module.spring.util.SpringContextHelper "></bean>
  12. <context:component-scan base-package="com.bis.majian.practice.module.*" />
  13. </Beans>


3. Configure the listener to load the spring container in Web. XML in a website project:

[HTML]View PlainCopy
    1. <!--initialize the spring container so that the spring container starts automatically with the launch of the Web App-
    2. <listener>
    3. <listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class>
    4. </Listener>


4. The object in the spring container can be obtained through this springcontexthelper call Getbean () method in the project.

Loading the spring context through Applicationcontextaware

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.