Discussion:
[jboss-user] [Snowdrop] - Accessing spring beans that are deployed in a war.
Bas Ven
2013-07-24 15:54:01 UTC
Permalink
Bas Ven [https://community.jboss.org/people/batter] created the discussion

"Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829560#829560

--------------------------------------------------------------
We are converting a huge Spring 3 based application to eventually be a JEE application using jBoss 7.1.1.Final.
As a first step we wrapped our application inside a war file which deploys inside jboss and works well.
I am trying to see if I can inject some of the spring beans inside this jar into a simple EJB. Both the war and EJB are packaged in an ear and I moved all jars from the war to the ear lib.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <description>BeanFactory=(*Commander*)</description>
    <bean id="*dataSourceManager*" class="database.DataSourceManagerImpl" />
</beans>
09:01:46,211 INFO  [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC service thread 1-2) Loading XML bean definitions from "/content/assembly.ear/commander.war/META-INF/jboss-spring.xml"
Caused by: java.lang.IllegalStateException: javax.naming.NameNotFoundException: *Commander* -- service jboss.naming.context.java.jboss.Commander
    at org.jboss.spring.support.SpringInjectionSupport.lookup(SpringInjectionSupport.java:180) [snowdrop-deployers.jar:2.1.0.Final]
    at org.jboss.spring.support.SpringInjectionSupport.getObjectFromBeanFactory(SpringInjectionSupport.java:136) [snowdrop-deployers.jar:2.1.0.Final]
Any ideas?  Is this setup even supported or instead of wrapping our spring application inside a servlet should we 'just' make its jars avaliable and have some dummy EJB or sar start the application?

As a side note, I tried to build the sportsclub example but it can't find the jpl-util 1.0 (which is not avaliable on maven central); it also looks very outdated and I am wondering if there is a newer example distribution for snowdrop 2.x?
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829560#829560]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Bas Ven
2013-07-24 16:14:23 UTC
Permalink
Bas Ven [https://community.jboss.org/people/batter] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829565#829565

--------------------------------------------------------------
Quick update.  Adding the META-INF/jboss-spring.xml file caused my war to stop deploying because it couldn't find other beans it uses to start.  So I guess that changes my question to 'why?'
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829565#829565]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Tejas Mehta
2013-07-24 20:42:36 UTC
Permalink
Tejas Mehta [https://community.jboss.org/people/tmehta] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829606#829606

--------------------------------------------------------------
Hello Bas Ven,

For your first post could you check in your jboss management ( http://127.0.0.1:9990/management http://127.0.0.1:9990/management) to make sure that the Commander factory is indeed registered with JNDI. Also for sportsclub, I am not sure where you downloaded the sportsclub example, but you could try from here: https://github.com/snowdrop/snowdrop-examples/tree/master/sportsclub https://github.com/snowdrop/snowdrop-examples/tree/master/sportsclub (with the spring version you are using), these are the most up to date.

As for the spring jars conflicting, you could try adding a jboss-deployment-structure.xml file in your WEB-INF dir, with contents:

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
    <deployment>
        <exclusions>
            <module name="org.springframework.spring" slot="snowdrop"/>
        </exclusions>
    </deployment>
</jboss-deployment-structure>

Cheers,

Tejas M.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829606#829606]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Bas Ven
2013-07-24 22:05:08 UTC
Permalink
Bas Ven [https://community.jboss.org/people/batter] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829615#829615

--------------------------------------------------------------
Thanks for the response.  I downloaded the example from your main page: http://downloads.jboss.org/snowdrop/examples/sportsclub/1.0/sportsclub-1.0.zip http://downloads.jboss.org/snowdrop/examples/sportsclub/1.0/sportsclub-1.0.zip
When I go to http://localhost:9990/console/App.html#naming http://localhost:9990/console/App.html#naming I do not see that the commander factory is registered.
The class it is complaining about is part of the spring-core jar which is defined in the snowdrop module above.

At first I thought that maybe that was because that jar also lived in my ear, but I both removed it from the ear lib and I used your suggestion regarding the jboss structure file, and still get this exception.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829615#829615]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Tejas Mehta
2013-07-24 22:30:50 UTC
Permalink
Tejas Mehta [https://community.jboss.org/people/tmehta] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829617#829617

--------------------------------------------------------------
Thanks for pointing out the example was out of date, we will upload the latest version asap.

The error is because incompatibility of spring versions or the presence of spring-asm.jar. If I recall correctly this the same error message as seen because of this: https://issues.jboss.org/browse/SNOWDROP-59 https://issues.jboss.org/browse/SNOWDROP-59. I believe you can rectify this by downloading the latest version of snowdrop with the spring modules: http://downloads.jboss.org/snowdrop/2.1.0.Final/jboss-spring-subsystem-as7-2.1.0.Final.zip http://downloads.jboss.org/snowdrop/2.1.0.Final/jboss-spring-subsystem-as7-2.1.0.Final.zip, and using the spring version that matches the one your project uses.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829617#829617]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Bas Ven
2013-07-24 23:28:43 UTC
Permalink
Bas Ven [https://community.jboss.org/people/batter] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829622#829622

--------------------------------------------------------------
We are indeed using spring version 3.2.3 which has inlined spring-asm in its core jar and we don't have the spring-asm.jar nor did I defined it in the snowdrop module.  Are you saying that snowdrop does not support spring 3.2.x ?   Your zip seems to have modules for 2.5, 3 and 3.1 but nothing higher.
On github however I do see a 3.2 module defined.  Should I try to build from source?

I am trying to figure out if for our situation snowdrop is the way to go, or if we should try the SpringBeanAutowiringInterceptor interceptor.
Thanks for all the help.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829622#829622]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Bas Ven
2013-07-25 00:02:41 UTC
Permalink
Bas Ven [https://community.jboss.org/people/batter] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829626#829626

--------------------------------------------------------------
At least I am getting further.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829626#829626]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Tejas Mehta
2013-07-25 01:15:18 UTC
Permalink
Tejas Mehta [https://community.jboss.org/people/tmehta] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829628#829628

--------------------------------------------------------------
Sorry I didn't realize 2.1.0.Final did not contain the 3.2 modules, another thing to be fixed :) . Could try with this: http://downloads.jboss.org/snowdrop/2.0.5.Final/jboss-spring-subsystem-as7-2.0.5.Final.zip http://downloads.jboss.org/snowdrop/2.0.5.Final/jboss-spring-subsystem-as7-2.0.5.Final.zip, I checked it does contain the spring 3.2 modules. As far as I know snowdrop should not require a class from spring-tx, but if for some reason your project makes it reference something from it then you could download it ( http://search.maven.org/remotecontent?filepath=org/springframework/spring-tx/3.2.3.RELEASE/spring-tx-3.2.3.RELEASE.jar http://search.maven.org/remotecontent?filepath=org/springframework/spring-tx/3.2.3.RELEASE/spring-tx-3.2.3.RELEASE.jar) and put it in $JBOSS_HOME/modules/org/springframework/spring/snowdrop. Finally add <resource-root path="spring-tx.jar" /> to module.xml.

Hopefully that will get you past that error.

Cheers,

Tejas M.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829628#829628]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Bas Ven
2013-07-25 15:57:54 UTC
Permalink
Bas Ven [https://community.jboss.org/people/batter] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829806#829806

--------------------------------------------------------------
java.lang.IllegalStateException: javax.naming.NameNotFoundException: SpringCommander -- service jboss.naming.context.java.jboss.SpringCommander
org.jboss.spring.support.SpringInjectionSupport.lookup(SpringInjectionSupport.java:180)
org.jboss.spring.support.SpringInjectionSupport.getObjectFromBeanFactory(SpringInjectionSupport.java:136)
org.jboss.spring.support.SpringInjectionSupport.injectToField(SpringInjectionSupport.java:202)
org.jboss.spring.support.SpringInjectionSupport.inject(SpringInjectionSupport.java:80)
org.jboss.spring.callback.SpringLifecycleInterceptor.postConstruct(SpringLifecycleInterceptor.java:43)
09:36:53,492 INFO  [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC service thread 1-14) Loading XML bean definitions from "/content/commander-server.war/META-INF/jboss-spring.xml"
Although that does seem to come from the spring framework.  Is there anything I can turn on logging wise to see if snowdrop is actually trying to register beans with JNDI?
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829806#829806]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Bas Ven
2013-07-25 20:55:15 UTC
Permalink
Bas Ven [https://community.jboss.org/people/batter] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829855#829855

--------------------------------------------------------------
Ok, getting furhter once again.  The problem about the BeanFactory not being registered was due to the fact that your jboss-spring.xml definition has changed from the examples I found on the site to what is actually needed.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd http://www.springframework.org/dtd/spring-beans.dtd">
*<beans>*
    <description>BeanFactory=(Commander)</description>
    <bean id="dataSourceManager" class="database.DataSourceManagerImpl" />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
*<beans xmlns=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans"*
*       xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance"*
*       xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true">*
    <description>BeanFactory=(SpringCommander)</description>
    <bean id="dataSourceManager" class="com.electriccloud.database.DataSourceManagerImpl" />
    <bean id="commanderServer" class="com.electriccloud.commander.server.CommanderServerImpl" />
</beans>
Now when I look in the jBoss console I indeed see that the factory is registered with JNDI.
java.lang.IllegalArgumentException: Cannot retrieve an org.springframework.beans.factory.BeanFactory from java:jboss/SpringCommander - a org.jboss.spring.factory.NamedXmlApplicationContext found instead
org.jboss.spring.support.SpringInjectionSupport.lookup(SpringInjectionSupport.java:175)
org.jboss.spring.support.SpringInjectionSupport.getObjectFromBeanFactory(SpringInjectionSupport.java:136)
org.jboss.spring.support.SpringInjectionSupport.injectToField(SpringInjectionSupport.java:202)
org.jboss.spring.support.SpringInjectionSupport.inject(SpringInjectionSupport.java:80)
org.jboss.spring.callback.SpringLifecycleInterceptor.postConstruct(SpringLifecycleInterceptor.java:43)
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829855#829855]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Joshua Wilson
2013-07-25 21:32:58 UTC
Permalink
Joshua Wilson [https://community.jboss.org/people/jowilson] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829858#829858

--------------------------------------------------------------
Do you have "server" and "database" at the root level of your package structure?  I would guess you had something like "org.companyname.projectname.server".  If this is the case then you will want to use the full class path in the jboss-spring.xml.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829858#829858]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Bas Ven
2013-07-25 22:12:50 UTC
Permalink
Bas Ven [https://community.jboss.org/people/batter] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829859#829859

--------------------------------------------------------------
I do have that but just removed it from the posting to protect the innocent :-)
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829859#829859]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Joshua Wilson
2013-07-25 22:30:52 UTC
Permalink
Joshua Wilson [https://community.jboss.org/people/jowilson] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829861#829861

--------------------------------------------------------------
And here I thought I solved all your problems. :)   We'll keep looking into this.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829861#829861]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Bas Ven
2013-07-25 23:37:26 UTC
Permalink
Bas Ven [https://community.jboss.org/people/batter] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829866#829866

--------------------------------------------------------------
So am I :-).  I got past the exceptions by (I think) copying your jars into the jboss module and making sure that my ear is using that module (and does provide its own snowdrop jar for the annotations).
However, now the Spring beans that get injected in the EJB are null objects.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829866#829866]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Joshua Wilson
2013-07-26 00:03:38 UTC
Permalink
Joshua Wilson [https://community.jboss.org/people/jowilson] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829868#829868

--------------------------------------------------------------
Were you able to get Sportsclub working? Would it be possible to see your code to help debug it?  If not, can you post the stack trace?
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829868#829868]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Bas Ven
2013-07-26 13:44:09 UTC
Permalink
Bas Ven [https://community.jboss.org/people/batter] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829987#829987

--------------------------------------------------------------
I got sportsclub working after I went to github to get the latest version of it. 
|
|
| private commander.server.CommanderServer commanderServer; |
| public String getCommanderServer(){ |
|     String status = "Did not get a server object"; |
|     try { |
|         if ( commanderServer != null){  *<== Always null* |
|             status = "Hey I got something"; |
|         } |
|     } catch (Exception e) { |
|         status = e.getLocalizedMessage(); |
|     } |
|     return status; |
|
| } |
One difference I have is that my EJB's live in one ear and the Spring beans in a separate war, each having their own jars.  Not sure if this bean injection has a container (as in each ear, war etc) or JVM scope.
@Configuration
public class MyConfiguration{
     public MyInterface MyBean(){
          return new MyInterfaceImpl();
     }
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829987#829987]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Joshua Wilson
2013-07-26 14:15:57 UTC
Permalink
Joshua Wilson [https://community.jboss.org/people/jowilson] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829994#829994

--------------------------------------------------------------
Which way are you trying to go Spring bean into EJB or EJB into Spring?

If are injecting a Spring Bean into an EJB Using Snowdrop then:

{code}
@Stateless
@Interceptors(SpringLifecycleInterceptor.class)
public class InjectedEjbImpl implements InjectedEjb
{
@Spring(bean = "springBean", jndiName = "MyApp")
private SpringBean springBean;

}
{code}

OR if you are injecting an EJB Using Annotations then:

{code}
public class ConsumerBean {

    @EJB(mappedName="ejb/exampleEJB")
    ExampleEjb exampleEJB;

}
{code}
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829994#829994]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Tejas Mehta
2013-07-26 22:26:49 UTC
Permalink
Tejas Mehta [https://community.jboss.org/people/tmehta] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/830053#830053

--------------------------------------------------------------
@Stateful
@Interceptors({SpringLifecycleInterceptor.class})
public class GreeterEJB implements IGreeterEJB {
    private Object memberDao;
/** rest of class **/
<dependency>
     <groupId>org.jboss.snowdrop</groupId
     <artifactId>snowdrop-deployers-core</artifactId>
      <version>2.1.1.SNAPSHOT</version>
</dependency>
Content of resources/META-INF/jboss-deployment-structure.xml:
Make sure this at the top level of the ear.
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
    <deployment>
        <dependencies>
            <module name="org.springframework.spring" slot="snowdrop" export="true">
                <exports>
                    <include path="META-INF**"/>
                    <include path="org**"/>
                </exports>
                <imports>
                    <include path="META-INF**"/>
                    <include path="org**"/>
                </imports>
            </module>
            <module name="org.jboss.snowdrop" export="true"/>
        </dependencies>
    </deployment> 
</jboss-deployment-structure>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans"
       xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/beans/spring-beans.xsd">
          <description>BeanFactory=(MyApp)</description>
/** beans or imports etc. **/
</beans>
Hopefully this helps you out. If not I can send over this sample working solution (much smaller than sporstclub) and hopefully you can take it from there.

Cheers,

Tejas M.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/830053#830053]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Joshua Wilson
2013-07-25 16:50:21 UTC
Permalink
Joshua Wilson [https://community.jboss.org/people/jowilson] created the discussion

"Re: Accessing spring beans that are deployed in a war."

To view the discussion, visit: https://community.jboss.org/message/829814#829814

--------------------------------------------------------------
I just fixed the download that was missing Spring 3.2.  Sorry about that.

http://downloads.jboss.org/snowdrop/2.1.0.Final/jboss-spring-subsystem-as7-2.1.0.Final.zip http://downloads.jboss.org/snowdrop/2.1.0.Final/jboss-spring-subsystem-as7-2.1.0.Final.zip
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/829814#829814]

Start a new discussion in Snowdrop at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2082]
Loading...