aspose file tools*
The moose likes Other Application Frameworks and the fly likes ejb-spring-hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "ejb-spring-hibernate" Watch "ejb-spring-hibernate" New topic
Author

ejb-spring-hibernate

Gigi Kent
Ranch Hand

Joined: Nov 30, 2004
Posts: 40
Hi guys.

For a few day I'm trying to solve a small problem.
So, I have a Spring-Hibernate application.
I've done one EJB to expose the services.
With jUnit everything is OK. I'm loading the application context like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = false)
public class TestPatientServiceImpl {
.......
}

The EJB is in one modul, the rest in another modul. (jar file)
The problem is that I don't know how to load the applicationContext.xml from my EJB (with the spring beans and hibernate sessionFactory declaration).

Now when I'm trying to call my services from EJB, this (this is in my spring module, another modul than the ejb modul) does not work anymore

@Autowired
@Qualifier("xmlLanguageService")
private XmlLanguageService xmlLanguageServ;


this above does not work.


Any ideas?

Thanks.
Corneliu.
[ October 20, 2008: Message edited by: Corneliu Croitoru ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10102
    
165

Corneliu,

This looks very specific to Spring framework than EJB. I will move this to an appropriate forum where you might get an answer.


[My Blog] [JavaRanch Journal]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well, usually Spring apps might call EJBs, but Very rarely the other way around, because people go to Spring so that they do not need to use EJBs.

SPring has <jee> namespace for looking up EJBs in your Spring application, but it can't do anything the other way around. You can expose your services as Corba. "RMI over IIOP", which is what EJBs use.

However, I would more question the design of using EJBs to front Spring, when you can expose Spring beans remotely in many other POJO based ways.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Gigi Kent
Ranch Hand

Joined: Nov 30, 2004
Posts: 40
Actually, I'm using ejb through spring.
In my ejb bean I have something like


and a method to call something from my app
Gigi Kent
Ranch Hand

Joined: Nov 30, 2004
Posts: 40
Actually I think I'm managed to do it.

In my ejb bean I have this:

code:

public void setSessionContext(SessionContext sessionContext) {
super.setSessionContext(sessionContext);
setBeanFactoryLocator(ContextSingletonBeanFactoryLocator.getInstance());
setBeanFactoryLocatorKey(PRIMARY_CONTEXT_ID);
}



Im my beanRefContext.xml I have this:

code:



<beans>
<bean id="businessBeanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>applicationContext.xml</value>
</list>
</constructor-arg>
</bean>
</beans>



applicationContext is the xml with all my bean to use. I think Spring finds this file, because I have a

code:


getBeanFactory().getBean(BEAN_NAME);



and I have no error, so it must find the xml file.

Now in my service layer I have something like

code:


@Autowired
@Qualifier("detailedPatientDaoService")
private DetailedPatientDao detailedPatientDao;



and I have a error java.lang.NullPointerException

So I'm guessing that it cannot do the injection.

Any ideas?

Thanks.
Corneliu
Gigi Kent
Ranch Hand

Joined: Nov 30, 2004
Posts: 40
I've managet to see the log from my weblogic, and I see the folowing error:



In my service layer, my methode have this



I don't know what I'm missing.

Please, any ideas?

Thanks.
Corneliu
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I have never seen an ApplicationContext object declared as a <bean> in xml, it is the ApplicationContext object that reads the xml file for bean declaration, not reading in itself to itself. So, why are you doing it that way? Just curious.

What object are you creating in code to read in that xml file?

Unless you are in the Web environment, code is usually

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");

Mark
Gigi Kent
Ranch Hand

Joined: Nov 30, 2004
Posts: 40
Juste curios. Do you know EJB?
If yes, you should know that using setBeanFactoryLocator we are loading the xml file .

I apreciate to have answers from people who know not from people that never worked with it.

Corneliu.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by Corneliu Croitoru:
Juste curios. Do you know EJB?
If yes, you should know that using setBeanFactoryLocator we are loading the xml file .

I apreciate to have answers from people who know not from people that never worked with it.

Corneliu.


8 Years of EJB experience form 1.x to 2.x to EJB3, And I find your comment a little rude, our biggest rule here is be nice. I will leave you a bit of slack on this first offense. I am very nice about that.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

So, you are accessing the Spring beans from EJBs, so your client is calling EJBs to get to your Spring Beans/services.

My comment is it seems that you are defeating the purpose of using Spring instead of EJBs. What benefits are you getting from the EJBs that you can't get from Spring alone in your environment?

Mark
Gigi Kent
Ranch Hand

Joined: Nov 30, 2004
Posts: 40
So, the idea is that I have a service layer, and through EJB I want to expose some methods, in a Weblogic 9.2 to external clients. So, other applications can connect to it , and do some stuff.
I'm using EJB because I have another app who can connect it self only to an EJB. So, EJB is not my choise.

So they (the apps) retrieve the EJB via a JNDI variable, and call one or more methods.
Actually, I think my problem is not the transaction but maybe Weblogic 9.2 cannot handle with the spring annotations (like @Autowire).

I will try to replace the @Autowire with the old fashion spring injection to see if this works. If it will, this is bad news, because I will have to change my code.

PS: Sorry for my previous message

If this will do the trick, I'll post it.

Corneliu.

[ October 23, 2008: Message edited by: Corneliu Croitoru ]
[ October 23, 2008: Message edited by: Corneliu Croitoru ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

So, EJB is not my choise.


Thanks, I actually should have asked that question up front. Sorry.

OK, so yes, you seem to have the configuration correct.

Something I noticed in some documentation. It stated,


In the above example, the ServicesConstants.PRIMARY_CONTEXT_ID constant would be defined as follows:
public static final String ServicesConstants.PRIMARY_CONTEXT_ID = "businessBeanFactory";


Because I don't see a ServicesConstants class in the Spring Javadocs, it looks to be a constant that you declare, if you haven't already. (But also looking at your config, it looks like you already have it declared in your EJB)

I am curious to see how you experiment goes with @Autowire, but I am doubting that to be the cause. Because it is in a BeanFactoryPostProcessor class that handles the @Autowire annotation, in which Weblogic has no control over.

Another obvious question, is can you remote debug your code in your App Server. Do you have it configured so that you can set a break point in your IDE, and check out the code. One in the EJB, and one in the service class

WAIT!, as I am typing my reply, I just saw this

@Autowired
@Qualifier("detailedPatientDaoService")
private DetailedPatientDao detailedPatientDao;

Are you sure the Qualifier name is the name of the bean that you want injected? Trying to inject a Service into a DAO reference. Check higher up in your server logs to see if you don't have an expection earlier that the ApplicationContext failed to be created.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

But then the odd question, is why is your JUnit test still passing? Are you stubbing or mocking out the DAO?

Mark
Gigi Kent
Ranch Hand

Joined: Nov 30, 2004
Posts: 40
The jUnit tests, are out of weblogic, in local.
I'm testing only the service layer, in my eclipse environement.
So....
I don't know.

I just replaced, the @Autowired with the old fashion injection way (via setter method) and now it's working.

So, the my conclusion is that there is a problem with weblogic 9.2 & @Autowire annotations.



Corneliu.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by Corneliu Croitoru:
The jUnit tests, are out of weblogic, in local.
I'm testing only the service layer, in my eclipse environement.
So....
I don't know.

I just replaced, the @Autowired with the old fashion injection way (via setter method) and now it's working.

So, the my conclusion is that there is a problem with weblogic 9.2 & @Autowire annotations.



Corneliu.


In the old fashion way, are you injecting the same "detailedPatientDaoService" or did you use a different bean name? I mean was the bean name supposed to be "detailedPatientDao" instead, and the @Qualifier just had a typo? So when you did it the old fashion way, you fixed that typo?

I am still thinking that Autowire should still work, I'll ask my co-workers via email.

Mark
Gigi Kent
Ranch Hand

Joined: Nov 30, 2004
Posts: 40
The bean id is detailedPatientDaoService.

Here

the detailedPatientDao is only the name of my attribute.
By any chance this could be the problem?


Now I have something like this



So, before using Qualifier I was specifying the bean id.


I'm missing something here?

Corneliu.
[ October 24, 2008: Message edited by: Corneliu Croitoru ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

No not at all. Now that I see that indeed the bean id for that Dao does end with "Service".

Now, I have one more curiosity question. OK, the tests 100% pass, right? I also thought that @Autowire could only be on setters or constructors and not instance variables? I could easily be wrong there.

So my curiosity question. Can you try using the @Resource JSR-250 annotation instead of @Autowire, and see if maybe that might work. @Resource("detailedPatientDaoService")

Oh, and one of my co-workers asked which Java version Weblogic is using? No one else answered my email question about whether @Autowire in Weblogic 9 isn't working. Maybe I need to send it to the whole dev team. I will get an answer for you on that.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yeah, it can go on Field, I just checked the javadocs.

Also being stupid here, but in your configuration file that gets deployed with your app, it does have

<context:annotation-config>, right?

Mark
[ October 24, 2008: Message edited by: Mark Spritzler ]
 
 
subject: ejb-spring-hibernate