Jul 06 2008

Access the Spring-ApplicationContext from everywhere in your Application

Published by at 9:03 PM under Java,Spring

In this blog i will show you a short hint how you can access your Spring-ApplicationContext from everywhere in your Application.

Imagine you have an application (e.g. a web or swing-application) which you now want to be Spring-enabled. Ok you add the Spring libraries and the Configuration-file and create your Spring-beans. But there are still some old class-files which you can’t use in this way. These files still need access to the Spring-Honeypot where all the goodies exists and you don’t want to redesign your application.

First create the class “ApplicationContextProvider“. This class implements the ApplicationContextAware. A bean which implements the ApplicationContextAware-interface and is deployed into the context, will be called back on creation of the bean, using the interface’s setApplicationContext(…) method, and provided with a reference to the context, which may be stored for later interaction with the context.


package context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

 * This class provides an application-wide access to the
 * Spring ApplicationContext! The ApplicationContext is
 * injected in a static method of the class "AppContext".
 * Use AppContext.getApplicationContext() to get access
 * to all Spring Beans.
 * @author Siegfried Bolz
public class ApplicationContextProvider implements ApplicationContextAware {

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        // Wiring the ApplicationContext into a static method
} // .EOF

This bean must be initialized in the Spring-Configuration file:


<bean id="contextApplicationContextProvider" class="context.ApplicationContextProvider"></bean>

The key for success is located in the in the method “setApplicationContext(…)“, here we are wiring the injected ApplicationContext into the static method “public static void setApplicationContext(ApplicationContext applicationContext) of the class “AppContext“:


package context;

import org.springframework.context.ApplicationContext;

 * This class provides application-wide access to the Spring ApplicationContext.
 * The ApplicationContext is injected by the class "ApplicationContextProvider".
 * @author Siegfried Bolz
public class AppContext {

    private static ApplicationContext ctx;

     * Injected from the class "ApplicationContextProvider" which is automatically
     * loaded during Spring-Initialization.
    public static void setApplicationContext(ApplicationContext applicationContext) {
        ctx = applicationContext;

     * Get access to the Spring ApplicationContext from everywhere in your Application.
     * @return
    public static ApplicationContext getApplicationContext() {
        return ctx;
} // .EOF

Now you have access from all class-files to the Spring-ApplicationContext in your application, if you call the static method:


ApplicationContext ctx = AppContext.getApplicationContext();
Honeypotbean honey = (HoneyPotBean) ctx.getBean("honey");

Do you have any improvements? Please drop a comment below.

Technorati Tags: , ,

76 responses so far

76 Responses to “Access the Spring-ApplicationContext from everywhere in your Application”

  1. Tomas Jurman sagt:

    Hi Siegfried Bolz

    Thank for you article about Spring applicationContex. Today I use your classes it in me application. It work perfect.

    See you
    Tomas Jurman
    Czech Republic

  2. Kal sagt:

    It’s not really necessary to create a separate AppContext class, right? Why not let the ApplicationContextProvider have a static field to hold the application context?

  3. Hello Kal,
    yes you can use a static field in the ApplicationContextProvider, my way was to use a separate class.

  4. Prasanna sagt:

    Very neat program. I was looking for something like this to be injected from a webservice end-point. Works very well. Thanks

  5. Long sagt:

    Can I ask you one question.

    - The application context can be changed every where in the application ?

    I have problems in connecting to DB. I can not find any way to change hibernate.default_schema property in my application.

    Can you help me ?

  6. Ratna sagt:

    Hello Bolz,
    Thanks alot for this useful blog. I was trying to get currrent apllication context using many approches,but I could nto succed. Getting applucation context was very import for some complex requirements. Your code solved my issue of application context.


  7. Juanma sagt:

    Menudo crack !!!!!!!

    thanks a lot


  8. Valentyna sagt:

    Thank you so much!!!

  9. Ali sagt:

    i wanna ask u what do u mean by:

    Honeypotbean honey = (HoneyPotBean) ctx.getBean(“honey”);


  10. Hello Ali,
    this is just an information how you can get access to all beans stored in the Spring-ApplicationContext (the Honeypot).

  11. thiago diniz sagt:

    Hello Bolz,
    i am getting a problem with your code, the method AppContext.getApplicationContext() is returning null, do you have any ideia?


  12. Marcia Piccione sagt:

    This solution keeps recreating the beans in the application context over and over again. Obviously a big problem for connection pools. Is there a way to access the application context without creating a new one?

  13. Marcia, the Context will be only once created on the initialization of the application (done by the ApplicationContextAware). If you access the Context, it is always the same and not a new one.

  14. Mauricio sagt:

    Thanks a lot!!!
    It’s working for my application, and it is very usefull.
    What other kind of magik can we do with the ApplicationContextAware?? :>
    if any have an awnser to my question, please post it.

  15. walsen sagt:

    Thanks a lot too.
    Very usefull and nice code.

  16. Pankaj sagt:

    Wonderful post. I didn’t knew about ApplicationContextAware. Good work.

  17. Brett sagt:

    Thanks Siegfried, A Great post;

    A good solution for those who are forced to use someone elses container.


  18. Vinaya sagt:


    I was using,

    ctx=new ClassPathXmlApplicationContext(“WEB-INF/applicationContext.xml”);
    In this case, it used to create all the beans again. I was able to see that in the log which was as follows…

    NFO [org.springframework.context.support.ClassPathXmlApplicationContext] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4605c7: display name [org.springframework.context.support.ClassPathXmlApplicationContext@4605c7]; startup date [Thu Aug 06 16:31:47 EDT 2009]; root of context hierarchy
    2009-08-06 16:31:47,779 [,] INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [WEB-INF/applicationContext.xml]
    2009-08-06 16:31:48,295 [,] INFO [org.springframework.context.support.ClassPathXmlApplicationContext] Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@4605c7]: org.springframework.beans.factory.support.DefaultListableBeanFactory@179b905
    2009-08-06 16:31:48,373 [,] INFO [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] Loading properties file from class path resource [WEB-INF/classes/trade.properties]
    2009-08-06 16:31:48,483 [,] INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@179b905: defining beans [propertyConfigurer,tradeConfig,transactionManager,contextApplicationContextProvider,fileUploadForm,multiPartSearch,mPContainer,applicationBean,screenReturnPath,BaseListBean,dataTable,voaDataTable,noemDemandTable,oemDemandTable,noemReqLineTable,oemReqLineTable,noemSupplyTable,oemSupplyTable,quoteTable,noemQuoteTable,oemQuoteTable,poHistoryTable,poHistorySngTable,poHistoryAMTable,noemPoHistoryTable,oemPoHistoryTable,noemsoHistoryTable,oemsoHistoryTable,stockTable,searchReqTable,supplyHistTable,supplyNoteTable,demandHistTable,demandNoteTable,myBuyerQuotes,otherBuyerQuotes,custShipToAdd,custBillToAdd,acctSearchTable2,mfrSearchTable2,cnoSearchTable,stocklocationTable,stockNoteTable,arsSearch1,arsSearch2,arsSearch3,poSearch,vendorSearch,soSearchTable,userApprovalLocationsBean,uplLEntryHandler,secRoleHandler,secRoleAdmin,webAppHandler,adminProfileHandler,userLoginHandler,workGroupAdmin,adminWgHandler,SequenceBean,tradeSessionBean,menuNavigation,userBean,tradeAuthenticationController,loginBean,tradeLoginUser,tradeRules,tradeProfiles,tradeProfilesHandler,userAdminBean,userLoginAdminBean,addEditUserLogin,stockItemsVwHandler,multiviewSearch,ddlBean,clearCache,accountVwBean,accountSearchHandler,customerSearchHandler,customerAddressSearchHandler,contactDetailsVwBean,contactDetailsVwHandler,buyerQuoteLinesHandler,otherQuoteLinesHandler,addQuote,buyerQuotesDetails,editQuote,searchBuyerReqReqLinesHandler,viewQuote,viewReqLine,viewBuyerCustomerRequirement,customerReqLinesBean,customerRequirementsBean,upLoadReqLines,uploadSig,custReqLineHistHandler,requirementBuyersBean,requirementBuyersHandler,buyerQuotesBean,quoteLinesHandler,quoteVwBeanHandler,addReqLine,closeRequirement,createCustomerRequirement,customerReqLinesHandler,editCustomerRequirement,editReqLine,searchReqReqLinesHandler,quoteAcceptance,sellerQuoteDetail,arsSupplyHandler,searchArsHandler,arsDemandHandler,stockHandler,controlNoSearchHandler,demandHistVwBean,demandHistVwHandler,demandHistoryDetail,demandNotesBean,demandNotesHandler,demandReqLineHandler,demandSearch,addDemand,editDemand,editSupply,demandSearchHandler,demandSearchVwBean,demandSearchVwHandler,demandBean,demandLBBean,inventoryItemNotesBean,notesBean,mfgSearchHandler,poSearchHandler,poDetail,soDetail,openPoOrdersHandler,hPoDetail,poHistoryBean,poHistoryHandler,poHistorySngHandler,poHistoryAcctMateHandler,searchPOHistory,poHItemHistoryHandler,poReceiptsHandler,itemHistoryVwBean,poNonStockPoolHandler,recievedOrdersHandler,nonStockSearchHandler,openSoOrdersHandler,shippedOrdersHandler,receivedPoOrdersHandler,soHistoryDetail,salesOrderInvoicesHandler,soHistoryHandler,stockItemsDetailBean,stockItemsVwBean,stockItemQuantitiesVwBean,stockLocationList,addSupply,supplyBean,supplyLBBean,supplyHistVwBean,supplyHistVwHandler,supplyHistoryDetail,supplyNotesHandler,supplyNotesBean,supplySearch,supplySearchHandler,vendorSearchHandler,vendorAddressesVwBean,supplySearchVwBean,supplySearchVwHandler,tradeUserBean,soSearchHandler,NextPhase,Trade,ERP,datawarehouse,sqlMapClient,jdbcUtil,jdbcSignatureDAO,pilotUsersVwDAO,soLinesAllDAO,securityRoleDAO,userLoginDAO,userLoginSignaturesDAO,profileLookupValuesDAO,userProfilesDAO,profilesDAO,userRolesDAO,userRolesVwDAO,userTradeGroupsDAO,partVwDAO,mfrDemandSummaryVwDAO,mfrSupplySummaryVwDAO,dwhAccountNotesVwDAO,userLoginTarget,mailService,version,mailSender,email,userLoginService,securityRoleService,acegiFilterChainProxy,httpSessionContextIntegrationFilter,log4jMDCUserFilter,securityRequestFilter,exceptionTranslationFilter,authenticationManager,daoAuthenticationProvider,rememberMeAuthenticationProvider,formAuthenticationProcessingFilter,jdbcLogError,accountAgentDAO,accountDAO,accountsVwDAO,addressDAO,addressTypeDAO,applicationsDAO,businessTypeDAO,businessTypeVwDAO,buyerQuotesDAO,buyerWorkgroupsVwDAO,buyersVwDAO,buyerQuotesVwDAO,commodityTypesVwDAO,commodityManagersVwDAO,contactDAO,contactAddressDAO,contactDetailsVwDAO,contactSearchVwDAO,countriesVwDAO,customerBuyerRequirementsVwDAO,customerRequirementsDAO,customerRequirementLinesDAO,customersVwDAO,customerAddressesVwDAO,erpCustomerContactsVwDAO,requirementLineAvlPartsDAO,customerReqLinesHistVwDAO,customerRequirementsVwDAO,demandVDAO,dateCodesVwDAO,demandHistVwDAO,demandStatusHistDAO,demandBuyersDAO,demandTypesVwDAO,demandNotesDAO,demandSearchVwDAO,erpProcedureDAO,erpPoHeaderDAO,erpPoLinesDAO,salesTermsVwDAO,shipMethodsVwDAO,itemHistoryVwDAO,inventoryItemNotesDAO,jdbcTradeSequenceDAO,jdbcTradeUtilDAO,jdbcControlNoDAO,locationsDAO,lightBoardDAO,manufacturersVwDAO,necxadmCacheObjDAO,xtradeCacheObjDAO,openSoLinesVwDAO,openPoLinesVwDAO,partClassVwDAO,packagingVwDAO,poLineSearchVwDAO,poHeaderDAO,poLineDAO,poSpecialInstructionsVwDAO,specialInstructionsVwDAO,poLineReceiptsVwDAO,phoneDAO,phoneTypeDAO,procedureDAO,productConditionsVwDAO,poHeaderVwDAO,purchaseOrdersAcctmateVwDAO,purchaseOrdersErpVwDAO,purchaseOrdersSngerpVwDAO,purchaseOrderReceiptsVwDAO,quoteStatusesDAO,quoteStatusHistDAO,reqBuyerStatusHistVwDAO,requirementBuyerStatusesDAO,requirementBuyersDAO,requirementStatusesDAO,requirementStatusHistDAO,rohsTypesVwDAO,salespersonsVwDAO,salesOrdersVwDAO,salesOrderInvoicesVwDAO,salesOrderShipmentsVwDAO,erpSoHeadersVwDAO,soHeaderDAO,erpSoLinesVwDAO,shippingLocationsVwDAO,soLineShipmentsVwDAO,stockItemQuantitiesVwDAO,stockItemsVwDAO,supplyVDAO,supplyHistVwDAO,supplyNotesDAO,supplySearchVwDAO,tradeOrganizationsMvDAO,userAccountsDAO,userApprovalLocationDAO,userApprovalLocationVwDAO,userAccountsVwDAO,vendorsVwDAO,vendorAddressesVwDAO,userSearchVwDAO,workGroupUsersVwDAO,workGroupsDAO,workGroupUsersDAO,soHeaderVwDAO,accountService,businessTypeTarget,buyersTarget,buyerQuotesTarget,cacheObjectTarget,contactTarget,commodityManagerTarget,commonTarget,customerService,customerAddressesService,partVwTarget,customerRequirementsTarget,demandTarget,lightBoardTarget,manufacturerTarget,poHistoryService,poService,soService,siService,purchasingTarget,stockItemTarget,userTarget,userAdminTarget,userAccountsServiceTarget,userApprovalLocationsTarget,userRolesTarget,userTradeGroupsTarget,vendorsServiceTarget,dbAdminTarget,salesTarget,arsSupplyDemandTarget,supplyTarget,soLinesAllTarget,validationServiceTarget,workGroupTarget,businessTypeService,buyersService,cacheObjectService,commodityService,contactService,commonService,buyerQuotesService,customerRequirementsService,arsSupplyDemandService,dbAdminService,demandService,lightBoardService,manufacturerService,salesService,purchasingService,supplyService,stockItemsService,userAccountsService,userAdminService,userApprovalLocationsService,userService,userRolesService,userTradeGroupsService,validationService,vendorsService,workGroupService,partVwService,soLinesAllService]; root of factory hierarchy
    2009-08-06 16:31:48,483 [,] INFO [org.springframework.transaction.jta.JtaTransactionManager] Using JTA UserTransaction: org.jboss.tm.usertx.client.ServerVMClientUserTransaction@6a21b2
    2009-08-06 16:31:48,483 [,] INFO [org.springframework.transaction.jta.JtaTransactionManager] Using JTA TransactionManager: com.arjuna.ats.jbossatx.jta.TransactionManagerDelegate@f3e54b


    Why is that?

  19. ankit sagt:

    What will happen once when this static context doesn’t created initially and remains null.
    Next time, it will not created and beans will not be loaded again…

    I think it might create big problem………

  20. Pratap Shinde sagt:


    This works great.
    Thanks for the solution.

  21. Guambino sagt:

    Hi, how I do get WebApplicationContext, cause the scopes such as session available?


  22. [...] access the bean with Groovy. Here’s how (it’s more or less a wrapper or extension of this post. The main trick is to implement ApplicationContextAware, which let Spring call the [...]

  23. Dan sagt:

    Was having trouble accessing spring beans from a web filter – had a method that worked fine in WebWork (Using DelegatingFilterProxy), but same model did not seem to be working with Struts2. This method worked fine for accessing spring beans from a filter in struts2.

  24. Bey sagt:

    Hi Siegfried Bolz,

    thinks for this solution

    I was lost a lot of time to found this solution

  25. Sonali sagt:

    Please make sure that the lazy-initialization in the contextApplicationContextProvider bean is set to false.

  26. Niko sagt:

    Thanks – you made my day!!! :-)

  27. sten17 sagt:

    Thank you very much.

  28. Bhushan sagt:

    nice thorough explanation, thanks

  29. Rene sagt:

    Thanks for that, Sigi – YMMD!

    I was really lost getting out the spring application context. That helped me out.

    See you,

  30. Anh Ha sagt:

    You can use the WebApplicationContextUtils to get the loaded Spring context in every where you want in the ServletContext, follow this example:

    ServletContext servletContext =this.getServletContext();

    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

    User user = (User)wac.getBean(“user”);

  31. Eric sagt:

    One of the great helpful tips! Clean, concise, and complete. Thank you.

  32. acaciobernardo sagt:

    I used this in the validate method in my DTO bean associated with the form.
    Unfortunely I coud not understand the standard way of getting the context:

    ServletContext servletContext =this.getServletContext();
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    The problem is thar I can’t get the this.getServletContext(); to compile…

    In my method all I have is a ValidationContext:
    public void validateEdicao(ValidationContext context).

    So, using the solution posted in this article did work. Thank you.

  33. Tor Arne Kvaløy sagt:

    Hey guys,

    I much easier way to get the spring application context from anywhere is to just have it autoinjected like this:

    private ApplicationContext applicationContext;

    Tor :)

  34. nicknish sagt:

    In the code when I do
    ApplicationContext ctx = AppContext.getApplicationContext();
    ctx is null,

    I have set lazy-init=false in applicationContext.xml

  35. nicknish sagt:

    I think I figured it out, I hadn’t added


    to my web.xml

  36. Eric M sagt:


    Your solution doesn’t apply in this situation. The whole point is to be able to access the application context from a class that isn’t managed by Spring. You can’t ask Spring to inject dependencies into objects it doesn’t know anything about. ;)

    Eric M.

  37. Miguel sagt:

    Thank you. Simple and elegant solution.

  38. Rodrigo sagt:

    It is working , thanks a lot!!

  39. Sheyla sagt:


    I have a problem, ctx is null. Why. I think that spring file is incomplete. Can someone help me.



  40. SS sagt:

    You are the best. It works Gr8!! Perfecto :-)

  41. Jane sagt:

    ctx is null. Bolz, can u pls help me out.

  42. Pablo sagt:

    Thanks!!!!!! Very useful and elegant

  43. Mei Lin sagt:

    I have a question. I have this dataSource on my applicationContext.xml.

    < bean id=”dataSourceArchive” class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>
    <property name=”driverClassName” value=”oracle.jdbc.OracleDriver”/>
    <property name=”url” value=”jdbc:oracle:thin:@EMDSSERVER2:1521:EMDS2″/>
    <property name=”username” value=”dbo”/>
    <property name=”password” value=”dbo”/>
    </bean >

    What I wanna do is to set the “url” property of that “dataSourceArchive” and have it point to another database (EMDSQA instead of EMDS2).


    Mei Lin

  44. Amado sagt:

    hello, I hv a problem when calling :
    Honeypotbean honey = (HoneyPotBean) ctx.getBean(”honey”);
    if the class honey.java have a constructor that takes arguments, and there is no empty constructor, i’m receiving an error that says :
    failed to load the bean.
    can i load a bean that the correspondant java class takes arguments?

  45. Mark sagt:

    The code could be simplified to:

    === AppContext.java ===
    package com.abc;
    import org.springframework.context.ApplicationContext;
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContextAware;
    public class AppContext implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    * Loaded during Spring initialization.
    public void setApplicationContext(ApplicationContext ctx)
    throws BeansException {
    applicationContext = ctx;

    * Get access to the Spring ApplicationContext from anywhere in application.
    * @return Spring ApplicationContext
    public static ApplicationContext getApplicationContext() {
    return applicationContext;
    === applicationContext.xml ===
    <bean id=”contextApplicationContextProvider” class=”com.abc.AppContext” . . .

  46. Anamika sagt:

    Thanks, Good one

  47. Tech sagt:

    Thanks a lot for the post, it helped greatly!!!!

  48. BeHappy25 sagt:

    Thanks a lot Siegfried Bolz

  49. RKR sagt:

    Thanks a lot. Very useful solution.

  50. purushoth sagt:

    Thanks boss… It is helpful for me…

  51. Sudar sagt:

    Nice, clean, simple and Great article!!!

  52. Java Coder sagt:

    This article is very good, clean and helpful

  53. Jaos sagt:

    It seems it is great but I afraid my context is null as well. Do you have any idea why ?
    My web.xml contains:
    < context-param>
    < param-name>contextConfigLocation< /param-name>
    < param-value>WEB-INF/applicationContext.xml< /param-value>
    < /context-param>
    Thanks Jaos (beginner in spring)

  54. maria cristina sagt:


  55. Mikhail L. sagt:

    when applicationContext has default-lazy-init=”true”, you can specify lazy init on bean level.

    <bean id=”contextApplicationContextProvider” class=”context.ApplicationContextProvider” lazy-init=”false”>

  56. SAMPATH sagt:

    Thanks alot for this post…….

  57. Blacky sagt:

    Very n1ce! Exactly what i need! Thanks a lot!

  58. Anoop sagt:

    Hello Bolz

    Thanks!!! It was a very helpful solution.

    @Kal, thanks for your comment also.

  59. Stefan sagt:

    Hi Siegfried Bolz
    Nice one, It works out of the box ;-)
    I am currently setting up my junit tests. I have a separate spring configuration for the tests.
    From my Test class, in my setUp method I call; ApplicationContextProvider().getBean(“beanName”); but the ApplicationContext is always null!!

    Do you have any hints ;-)


  60. Zolou sagt:

    Another happy customer :)

  61. Suresh Siva sagt:

    This is a very good idea…
    Thanks a lot…

  62. Gooly sagt:

    All that doesnt work if 2 web-applications are using the spring container, because of the Class Loaders e.g. in Tomca, is there any solution to 2 or more webapps that want to share their ApplicationContext? Because I get linkage problems because ApplicationCOntext is loaded by different WebCLassLoaders and hence are never the same. ANY hints?

  63. Stef sagt:

    Hi there,

    You don’t need to implement ApplicationContextAware. Using a basic Singleton pattern that is initialized in your main class would work much better for a non Spring-MVC application.

    If you have a web application and use Spring-MVC, you can easily autowire your services inside your controllers and thus have access to them. You will likely never need to work directly with ApplicationContext object directly.

    Cheers, Stef.

  64. Owen sagt:


  65. Jani sagt:

    Solved my problem:)

    Good post and thanks a lot.

  66. tib sagt:


    recommend this way:

    ApplicationContext ctx = AppContext.getApplicationContext();
    Honeypotbean honey = ctx.getBean(HoneyPotBean.class);

    Useful when you use @autowired annotation

  67. Guru sagt:

    Null pointer exception while retriving app context

    This app context has to be used in a web service.

    Followed the steps

    1. Impletmented app context aware
    2. Added implemented class in bean with lazy-init = false
    3. Web.xml is already configured

    Log while loading:
    AppContext.setAppContext():Root WebApplicationContext: startup date [Thu Feb 23 11:33:26 CST 2012]; root of context hierarchy

    Log while getting:

    Can you please help ?


  68. Ruby sagt:

    thanks a lot for the post

  69. ugur sagt:

    you saved my day.


  70. David sagt:

    Works great!!

  71. Nader Aeinehchi sagt:

    Thank you Siegfried Bolz for providing this mechanism.

    The original AppContext does not seem to be thread safe. It may potentially fail in production under higher contentions. Please see the attached revised code. Here, we use a simple locking mechanism that is tuned for high read through-put but with lower write performance. A better approach could be a CAS alogrithm, but unless very high contention, I believe simple locking as attached is good enough.

    public class SpringAppContext {

    public final static SpringAppContext INSTANCE = new SpringAppContext();

    private final ReadWriteLock lock = new ReentrantReadWriteLock();

    private final Lock readLock = lock.readLock();

    private final Lock writeLock = lock.writeLock();

    private SpringAppContext() {


    private ApplicationContext ctx;

    * Injected from the class “ApplicationContextProvider” which is automatically loaded during Spring-Initialization.
    public void setApplicationContext(ApplicationContext applicationContext) {
    try {
    this.ctx = applicationContext;
    } finally {

    * Get access to the Spring ApplicationContext from everywhere in your Application.
    * @return
    public ApplicationContext getApplicationContext() {
    try {
    return this.ctx;
    } finally {

  72. Paul sagt:

    Excellent, exactly what I needed and worked on first deploy. Thank you!!

  73. Adelmo sagt:

    Works great!!
    I love this classes … :-)


  74. Aayush sagt:

    Great post and thanks Mark for your revised solution.

  75. Hey There. I found your blog using msn. This is a really
    well written article. I will be sure to bookmark it and return to read more of your useful information.
    Thanks for the post. I will definitely comeback.

    Also visit my web-site … restaurant pests