-
1. Re: how to inject applicationContext in bean?
cleverswine Mar 21, 2008 8:21 PM (in response to cleverswine)p.s. I don't mean Spring's applicationContext, I mean the application scope (ServletConfig) of the application
-
2. Re: how to inject applicationContext in bean?
shane.bryzak Mar 21, 2008 9:34 PM (in response to cleverswine)The various contexts don't exist in a context themselves. If you want a reference to the application context, simply use Contexts.getApplicationContext().
-
3. Re: how to inject applicationContext in bean?
cleverswine Mar 21, 2008 9:42 PM (in response to cleverswine)Thank you, Shane. That worked. Is there any way to inject Contexts.getApplicationContext() into my component?
-
4. Re: how to inject applicationContext in bean?
cleverswine Mar 21, 2008 9:53 PM (in response to cleverswine)Or better yet, any way to set values on the application context from components.xml? Something like:
<core:contexts scope="application" name="myProperty" value="true"/>
-
5. Re: how to inject applicationContext in bean?
shane.bryzak Mar 22, 2008 6:40 AM (in response to cleverswine)You can only inject something if it already exists in a context, or if it has a @Factory or if it's an auto-create component. So what you could probably do if you want is create a factory method that returns the application context and annotate it with @Factory(
applicationContext
). A bit of a hack, but it would work.You can't set ad-hoc values in the application context using components.xml, only declare application-scoped components.
-
6. Re: how to inject applicationContext in bean?
matt.drees Mar 23, 2008 2:10 AM (in response to cleverswine)
Bill Levitt wrote on Mar 21, 2008 08:19 PM:
I tried@In private Context applicationContext;
but this doesn't work.Odd... that should definitely work, and works in a test case I just put together. I'm not sure why it's not working for you.
-
7. Re: how to inject applicationContext in bean?
matt.drees Mar 23, 2008 2:16 AM (in response to cleverswine)
Shane Bryzak wrote on Mar 22, 2008 06:40 AM:
You can only inject something if it already exists in a context, or if it has a @Factory or if it's an auto-create component. So what you could probably do if you want is create a factory method that returns the application context and annotate it with @Factory(applicationContext
). A bit of a hack, but it would work.I don't know if I'd call it a hack; there's a built in Seam component that does exactly this (org.jboss.seam.core.Contexts). So, Bill's code should run as-is.
You can't set ad-hoc values in the application context using components.xml, only declare application-scoped components.Well, you can do something like this, though it's slightly noisy:
<factory name="myProperty" value="#{true}" scope="application" auto-create="true"/>
-
8. Re: how to inject applicationContext in bean?
cleverswine Mar 24, 2008 2:10 PM (in response to cleverswine)It's null for me. Can you post your test case?
-
9. Re: how to inject applicationContext in bean?
cleverswine Mar 24, 2008 2:56 PM (in response to cleverswine)Hmmm... nothing is getting injected for me.
-
10. Re: how to inject applicationContext in bean?
matt.drees Mar 25, 2008 7:30 AM (in response to cleverswine)Sure. Here it is:
package com.example.sandbox; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Logger; import org.jboss.seam.annotations.Name; import org.jboss.seam.contexts.Context; import org.jboss.seam.log.Log; import org.jboss.seam.mock.SeamTest; import org.testng.Assert; import org.testng.annotations.Test; public class ApplicationScopeTest extends SeamTest { @Name("applicationScopeHolder") public static class ApplicationScopeHolder { @Logger Log log; @In Context applicationContext; //specified in components.xml @In boolean testProperty; public void verify() { log.info("verifying applicationContext not null"); Assert.assertNotNull(applicationContext); Assert.assertEquals(testProperty, true); } } @Test public void testApplicationScopeFactory() throws Exception { new ComponentTest() { @Override protected void testComponents() throws Exception { invokeMethod("#{applicationScopeHolder.verify}"); } }.run(); } }
(make sure your test src folder has a seam.properties file).
-
11. Re: how to inject applicationContext in bean?
matt.drees Mar 29, 2008 12:20 AM (in response to cleverswine)Have you been able to figure out what's going on?