Using Spring with EJB 3


Using Spring with EJB 3
By Robert.Filed in Java  |   Tags:ejbiocスプリング
 
Back when we we were planing the migration to Glassfish、I realised we would have two dependency-injection frame ew orks in use–EJB 3 and Spring.For ovious reasons、I wanted to know more about thers wotwotworkete.workete.wotwotwork.I couldn’t find anyone who had used EJB 3 and Spring together–even Ben Alex from Interface 21 hadn’t comme acros it.Six monts later,and I still haven’t heard of anyousing Spring剫fromEJBs.Except for us.
The’s lots of reasons to use both EJB 3 and Spring.EJBs have benefits Spring doesn’t give-integration with things Servlets and JSP tags、better integration with the container for monight and and and manager.inagon.inagon.inagon.inagon.inagon.inage.inagon.inage.ingment.inagon.inage.inagon.inage.ings.ings.inage.ings.inage.ings.inage.ings.ings.ingsbut also you get more control over the bean lifecycle.Together,they ara good complement.
But how do you get a Spring-managed bean from an EJB?The answer:dependency interceptor、of course.That is、with the aid of a EJB Interceptor:
[source=''java']public class SpringBeanIntercepter{
prvate BenFactory beanFactory;
@PostContstrustructputblic voidconfigreSpingBens(InvocationConttexcontexxt)throws Exception{for(Method methodod:context.getTargget().get Claasass()getMethods()Spif){Spiif(methodod.AndededededededededededededededededetitititititititiststststststststininininininininininininingggggststststststststststininininininininininininininggggggggggggName=determine SprigBenName(method、springClass)Object sprigBen=beanFactory().getBeaBen(springName);try{method. invoke(context.getTargget()、springBen);}cattttttttttttttttttcalArectException{){throw newnewnewnew Runtime RuntittttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttException e){throw new RuntimeException(e.getCause()}}context.proceed();
privte String determine SprigBenName.value()if(spring Nangth){string spring Name=method.get Annotation(Spring Ben.class).value();if(spring Nangme.length){springme=Naturing me=Caring me=>
privte String toCamelCase(String string string){return Charcter.toLowerCase(string.charmt(0))+string.substring(1,string.length();
prvate BenFactory BeanFactory(){if(beanFactory==null){BenFactoryLocator locator=ConttextSingletoBenFactoryLocator.getInstance("claspath*:beanRefConttext.xt.xmmml))= BeaBeacacacaceFafefefefefefefeferereretFactortFactortFactortFactortFactortFactortFactoreeeeeeeeFactortFactortFactortFactortFactortFactortFactortFastance e e e======ctory();return beanFactory;}
@Target(ElementType.METHOD)@Retension(Retension Policy.RUNTIME)public@interface SprigBen{String value()default'''''''''''''''''''//source'
That’s it.This Interceptor will look ove EJB,after its created,and provides the Spring beans.This version looks for'setter-methods'with an @SpringBean annotation(coyou SpouououSpuld alsochangeitititititititthe the the the the the thethethethethethethethethe fiinininininininininininininininininininininininininininininininininininininininindedededededededededededeststststststststststststststststststststststmakes this possible)
ヘレ’s how you would use it:
[source=''java'@Statiteless@Interceptors(Spring BenInterceptors.class)pblic class MyServiceBean implements MyService{prvate MySpringBean bean}

@SpringBean public void set MySpringBean{MySpringBean bean}
With this(and one other trick I'll discuss soon)、you can(nearly)seamless integrate EJB 3 and Spring.
The one big limitation of this:with Station Session Bens、you can only inject stateless dependencies、and you can only inject them when the EJB is initiased.