Announcement Announcement Module
Collapse
No announcement yet.
inject a session bean into pojo thru spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • inject a session bean into pojo thru spring

    Hi Guys
    I am Using EJB 3.0
    i am having a session bean which implements the Local interface,and i have a pure pojo also.
    How can i inject a session bean into the pojo rather than manual jndi look up in to pojo thru spring(using @Resource and SpringBeanAutowiringInterceptor)?.

    Please help me out...

  • #2
    after all round hard night i resolve it .
    Here we go ..
    My Pojo class is as follows

    public class TestPojo {

    @Resource(mappedName ="ejb/helloman")
    private Dominic local;


    public Dominic getLocal() {
    return local;
    }

    public void setLocal(Dominic local) {
    this.local = local;
    }

    public String sayHello(){
    return local.sayHello();
    }


    and i used a custom bean Factory to inject the session beans

    @Stateless
    @TransactionAttribute(TransactionAttributeType.REQ UIRED)
    @Interceptors(SpringBeanAutowiringInterceptor.clas s)
    @EJBs({ @EJB(name = "ejb/helloman", beanInterface =Dominic.class)})
    public class BeanHolder implements BeanFactory {

    @Autowired
    private ApplicationContext applicationContext;

    /**
    * Public default constructor.
    */
    public BeanHolder () {
    super();
    }

    /*
    * (non-Javadoc)
    *
    * @see
    * org.springframework.beans.factory.BeanFactory#cont ainsBean(java.lang.String
    * )
    */
    public boolean containsBean(String arg0) {
    return applicationContext.containsBean(arg0);
    }

    /*
    * (non-Javadoc)
    *
    * @see
    * org.springframework.beans.factory.BeanFactory#getA liases(java.lang.String)
    */
    public String[] getAliases(String arg0) {
    return applicationContext.getAliases(arg0);
    }

    /*
    * (non-Javadoc)
    *
    * @see
    * org.springframework.beans.factory.BeanFactory#getB ean(java.lang.String)
    */
    public Object getBean(String beanName) throws BeansException {
    return applicationContext.getBean(beanName);
    }

    /*
    * (non-Javadoc)
    *
    * @see
    * org.springframework.beans.factory.BeanFactory#getB ean(java.lang.String,
    * java.lang.Class)
    */
    @SuppressWarnings("unchecked")
    public Object getBean(String beanName, Class beanClass) throws BeansException {
    return applicationContext.getBean(beanName, beanClass);
    }

    /*
    * (non-Javadoc)
    *
    * @see
    * org.springframework.beans.factory.BeanFactory#getB ean(java.lang.String,
    * java.lang.Object[])
    */
    public Object getBean(String beanName, Object[] arguments) throws BeansException {
    return applicationContext.getBean(beanName, arguments);
    }

    /*
    * (non-Javadoc)
    *
    * @see
    * org.springframework.beans.factory.BeanFactory#getT ype(java.lang.String)
    */
    @SuppressWarnings("unchecked")
    public Class getType(String beanName) throws NoSuchBeanDefinitionException {
    return applicationContext.getType(beanName);
    }

    /*
    * (non-Javadoc)
    *
    * @see
    * org.springframework.beans.factory.BeanFactory#isPr ototype(java.lang.String)
    */
    public boolean isPrototype(String beanName) throws NoSuchBeanDefinitionException {
    return applicationContext.isPrototype(beanName);
    }

    /*
    * (non-Javadoc)
    *
    * @see
    * org.springframework.beans.factory.BeanFactory#isSi ngleton(java.lang.String)
    */
    public boolean isSingleton(String beanName) throws NoSuchBeanDefinitionException {
    return applicationContext.isSingleton(beanName);
    }

    /*
    * (non-Javadoc)
    *
    * @see
    * org.springframework.beans.factory.BeanFactory#isTy peMatch(java.lang.String,
    * java.lang.Class)
    */
    @SuppressWarnings("unchecked")
    public boolean isTypeMatch(String beanName, Class beanClass) throws NoSuchBeanDefinitionException {
    return applicationContext.isTypeMatch(beanName, beanClass);
    }

    and my Dominic Session bean as follows
    @Stateless
    public class Dominic implements DominicBase{

    @Override
    public String sayHello() {
    // TODO Auto-generated method stub
    return "i am a DataBase bean";
    }

    }


    and Here its my application Context....which will be load from interceptor that
    i specified in the BeanHolder.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation=
    "http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.5.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schem...g-util-2.5.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schem...ng-jee-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" >

    <context:annotation-config/>

    <!-- Global Setup -->
    <bean class="org.springframework.context.annotation.Comm onAnnotationBeanPostProcessor">
    <property name="alwaysUseJndiLookup" value="true"/>
    </bean>

    <!-- Bean Definitions -->
    <bean id="samplePojo" class="com.ejb.poc.TestPojo">
    </bean>

    THATS IT......

    Hats off Spring Dev Guys to make my life easy....hurahh.......

    Comment


    • #3
      WHat if you want to inject EJB 2.0 Bean Stateless SessionBean into Spring. How to do that?

      Comment


      • #4
        This is applicable to EJB 3.0

        if you use EJB 3.0
        <context:annotation-config/>

        will inject the session beans to your pojos

        Comment

        Working...
        X