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.)
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
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.
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