Help

Controls

PermLinkWikiLink

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

Forum: Seam Users Forum ListTopic List
21. Mar 2008, 20:19 CET | Link

Hi,

How can I inject the applicationContext? I tried


	@In
	private Context applicationContext;

but this doesn't work. Do I have to use Component.getInstance()?

11 Replies:
21. Mar 2008, 20:21 CET | Link

p.s. I don't mean Spring's applicationContext, I mean the application scope (ServletConfig) of the application

21. Mar 2008, 21:34 CET | Link

The various contexts don't exist in a context themselves. If you want a reference to the application context, simply use Contexts.getApplicationContext().

Rating:  * * * * *
21. Mar 2008, 21:42 CET | Link

Thank you, Shane. That worked. Is there any way to inject Contexts.getApplicationContext() into my component?

21. Mar 2008, 21:53 CET | Link

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"/>

22. Mar 2008, 06:40 CET | Link

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.

23. Mar 2008, 02:16 CET | Link
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"/>
23. Mar 2008, 02:10 CET | Link
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.

24. Mar 2008, 14:10 CET | Link

It's null for me. Can you post your test case?

24. Mar 2008, 14:56 CET | Link

Hmmm... nothing is getting injected for me.

29. Mar 2008, 00:20 CET | Link

Have you been able to figure out what's going on?

25. Mar 2008, 07:30 CET | Link

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