Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a dependency to an EJB

I want to add a dependency to an EJB. How do I do this using Spring? The dependent object is a general service object. Based on code below I want to wire myDependency without having to use 'new'. The EJB runs in weblogic.

@Stateless(mappedName = "MyBean")
public class MyBean implements MyBeanRemote, MyBeanLocal {

    @EJB(name = "MyOtherBean")
    private MyOtherBean myOtherBean;


    private MyDependency myDependency;
    ...

}
like image 878
striker77 Avatar asked Jun 14 '26 05:06

striker77


1 Answers

This is well described in the Spring documentation:

For EJB 3 Session Beans and Message-Driven Beans, Spring provides a convenient interceptor that resolves Spring 2.5's @Autowired annotation in the EJB component class: org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor. This interceptor can be applied through an @Interceptors annotation in the EJB component class, or through an interceptor-binding XML element in the EJB deployment descriptor.

@Stateless
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class MyFacadeEJB implements MyFacadeLocal {

    // automatically injected with a matching Spring bean
    @Autowired
    private MyComponent myComp;

    // for business method, delegate to POJO service impl.
    public String myFacadeMethod(...) {
        return myComp.myMethod(...);
    }
    ...
}

Stateless EJBs and Spring beans, however, offer more or less the same possibilities. Mixing them together seems like unnecessary complexity.

like image 57
JB Nizet Avatar answered Jun 16 '26 18:06

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!