7 Replies Latest reply on Dec 30, 2006 9:12 AM by lajoie

    Injecting beans defined in jboss-beans.xml

    lajoie

      I'm sure this is answered somewhere in here but I couldn't find the correct search criteria to find it. So, sorry in advance for what is likely a repeat.

      I have some beans (just POJOs) within my SEAM app, for example one is a configuration properties object. I'd like to inject them, via @In, into things like my session beans. Currently, just defining them in jboss-beans.xml leads to an error when the session beans startup that indicates that the particular beans aren't found. This makes sense since to me since as far as I understand the objects defined in jboss-beans.xml aren't bound to the contexts searched by @In.

      So, what is the most appropriate way to do this?

      Thanks.

        • 1. Re: Injecting beans defined in jboss-beans.xml

          @In(create=true) will tell Seam to create the instance if it doesn't yet exist.

          • 2. Re: Injecting beans defined in jboss-beans.xml
            lajoie

            Sorry to be dense. I'm just not sure what the relation between beans created by the micro-container config (jboss-beans) an those created/managed by Seam. Will this use the instance configured in by the micro-container?

            • 3. Re: Injecting beans defined in jboss-beans.xml

              Sorry - I read your message too quickly and didn't realize what you were talking about. :)

              Seam can't directly inject microcontainer components. I actually haven't used the microcontainer, so I have no idea how you look up a component outside of referencing a JNDI binding for it. It seems to me that you probably need to write a Seam component with an @Unwrap method that does the component lookup.

              • 4. Re: Injecting beans defined in jboss-beans.xml
                lajoie

                Can you point me to some documents about where in the JNDI tree Seam binds objects for the various contexts? It seems like a generic bean that could be declared in the micro-container and bind beans to JNDI would be useful. Then people could use the micro-container to setup and configure beans and make use them within Seam components. Is this reasonable?

                • 5. Re: Injecting beans defined in jboss-beans.xml

                  Seam doesn't bind anything into JNDI. It only uses JNDI to lookup EJB3 components. If you have other things in JNDI, I think the best strategy is to use an @Unwrap wrapper around your lookup code. If this were something that people would generally want to do, I'm sure we could provide some sort of shortcut for configuring that. But I'm not sure I believe anyone would really want to do that. In fact, I'm still trying to figure out why you want to do that. :)

                  Is it hard to bind a microcontainer component into JNDI? As I said, I haven't really used the microcontainer for anything. I did notice this forum posting on the topic: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3984848 Does that look like it would work for you?

                  • 6. Re: Injecting beans defined in jboss-beans.xml
                    lajoie

                    Sorry, I thought Seam looked up most of its components from the JNDI tree, not just the EJB3 stuff. Again, I don't know much about how Seam works internally. I do see how I could use the @Unwrap to take something bound in JNDI and expose it though. The ultimate goal is simply to be able to expose mico-container created beans to Seam components via the @In annotation. I'll look to see if I can cut out JNDI lookup part totally an report back on that.

                    Now, as to why I actually want to do this, it's pretty simple. My customer deploys in a plain old Tomcat container and the particular application I'm working on has a couple of things that need to be initialized and available to Seam components. For example, one part of the application uses XFire to contact a web service and stream in data so I'm using the micro-container to get that client set up. This client is then used by a couple of stateless session bean Seam components. Another example is an application configuration bean that holds information used by multiple Seam components.

                    While Seam 1.1 is probably to new to say for sure I can imagine people running it in other containers will want to take advantage of services offered by those containers. Often these services are available from JNDI or would need to be setup programmatically, some subset of which could be done using something like the micro-container (or other IoC containers like spring).

                    • 7. Re: Injecting beans defined in jboss-beans.xml
                      lajoie

                      Turns out the seam component with an @Unwrap method doesn't work.

                      The component needs to depend on the "org.jboss.seam.core.ejb" component since thats what is starting up the micro-container, however beans started up in the EJB3 container are what requires the stuff we're trying to expose via the @Unwrap method so it turns out to be a cyclic dependency.