11 Replies Latest reply on Mar 29, 2008 12:20 AM by matt.drees

    how to inject applicationContext in bean?

    cleverswine

      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()?

        • 1. Re: how to inject applicationContext in bean?
          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

            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

              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

                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

                  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

                    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

                      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

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

                        • 9. Re: how to inject applicationContext in bean?
                          cleverswine

                          Hmmm... nothing is getting injected for me.

                          • 10. Re: how to inject applicationContext in bean?
                            matt.drees

                            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

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