Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

load applicationContext from business layer

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I want to getBean(s) from springs applicationContext. Since this is from the business layer I dont want to getServletContext() etc.and of course injection is not an option here.

Right now I am using ClasspathXmlApplicationContext and it works fine.

Is there a better way to do this?
Also when i do ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml"); is a new context created?(i hope only the context loaded at startup is returned.)

thanks
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No that creates an actual new instance of an ApplicationContext. Why does your Business Server need access to it. I am sure there must be a better design approach so that your service classes won't need direct access to the ApplicationContext.

Anyway, besides there most likely being a design problem here, you can access the ApplicationContext in your service class by making your Service class ApplicationContextAware. This is an interface that you can implement, and you have a setAppplicationContext method to implement, that takes the ApplicationContext. That is how you can access it directly, and not use "new ApplicationContext...." code

Mark
 
praveen Shangunathan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Thanks. that worked.
going to the design issue you had mentioned, i did not want spring access code lying around, but did not find an alternative.
i want to inject one of a dozen reports at runtime depending on something. i am not sure how to have spring do this. i could use a factory but did not want to use "new" and want spring inject a report.
kindly advice.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All...
I faced the same problem about get the applicationContext from the business layer and i follow the solution above and it worked correctly, thank a lot for your help. But my question now is :
What if a want to get a session scoped bean from the application context in business layer

In other word and with some details:
My Application implements the 3 layers as follow:
  • Icefaces for presentation layer
  • Spring for business layer
  • Hibernate for persistence layer


  • and i used acegi security to secure my application...

    Now after the user enter the login information the acegi started the authentication process and i use a custom LDAP authentication provider to check the login information in the Active Directory, and this custom provider is registered in the applicationContext.xml file.. In this case i suppose that this provider is in my business layer. In this provider and after he finish his work with Active Directory i need to get the application context and get the session scoped bean "user", and inside this bean i need to save my login information..

    So what i did till now: create my custom provider which is worked successfully and I implement the Interface "ApplicationContextAware" and override the method "setApplicationContext" and I get a reference to the application context which held in the "appContext" variable, after that I let the Provider to do his work and after that I invoke the method appContext.getBean("user") and i got an error message that told me :
    "Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to req...."

    So what is the solution .... ??? .. anyone can help my .... every suggestion or help will be appreciated
     
    Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!