google Analytics

Showing posts with label EJB3 Spring. Show all posts
Showing posts with label EJB3 Spring. Show all posts

Sunday, August 29, 2010

EJB 3.0 with Spring 1.2 SpringBeanAutowiringInterceptor

ejb3 Spring

The EJB
EJB3-compliant interceptor class that injects Spring beans into fields

and methods which are annotated with @Autowired. Performs injection after
construction as well as after activation of a passivated bean.
To be applied through an @Interceptors annotation in the

EJB Session Bean
or Message-Driven Bean class, or through an interceptor-binding XML
element in the EJB deployment descriptor.
Delegates to Spring's AutowiredAnnotationBeanPostProcessor underneath,
allowing for customization of its specific settings through overriding
the configureBeanPostProcessor(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,
java.lang.Object) template method.
The actual BeanFactory to obtain Spring beans from is determined by the getBeanFactory(java.lang.Object)
template method. The default implementation obtains the Spring ContextSingletonBeanFactoryLocator,
initialized from the default resource location classpath*:beanRefContext.xml,
and obtains the single ApplicationContext defined there


package com.test;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor;

import com.test.TestSpringBeanAutoWiringSessionBean;

@Interceptors(SpringBeanAutowiringInterceptor.class)
public @Stateless class TestSpringBeanAutoWiringSessionBeanBean implements TestSpringBeanAutoWiringSessionBean {
    @Override
    public void sayHello(String name) {
        System.out.println("Anish---->>"+name);
    }
   
    @Autowired
    private AnnotatedTarget target;
}

The beanRefContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="beanfactoryTDS"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="META-INF/SpringContext.xml" />
</bean>
</beans>

and here' smy SpringContext.xml
META-INF\SpringContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="foo" class="com.apress.prospring2.ch03.autowiring.Foo"/>
<bean id="bar" class="com.apress.prospring2.ch03.autowiring.Bar"/>
<bean id="annotatedTarget" class="com.apress.prospring2.ch03.autowiring.AnnotatedTarget"/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>

public class AnnotatedTarget {
@Autowired
private Foo foo;
@Autowired
private Foo foo2;
@Autowired
private Bar bar;

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("AnnotatedTarget");
sb.append("{foo=").append(foo);
sb.append(", foo2=").append(foo2);
sb.append(", bar=").append(bar);
sb.append('}');
return sb.toString();
}
}
==================================================================================================================================
public class Bar {

@Override
public String toString() {
return getClass().getName();
}
}
==================================================================================================================================
public class Foo {

@Override
public String toString() {
return getClass().getName();
}
}

Technology Solutions: MDB Using SpringBeanAutowiringInterceptor ...

MDB USING Spring Bean Autowiring Interceptor
http://opensourceframework.blogspot.com/2010/08/mdb-using-springbeanautowiringintercept.html



EJB 3.0 with Spring 1.2
SpringBeanAutowiringInterceptor Spring2.5.2 API



Thursday, August 26, 2010

Calling EJB3 from Spring

Calling EJB3 from Spring
=============================================================



=============================================================

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jee="http://www.springframework.org/schema/jee"      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

   
<bean id="helper" class="com.test.ejb.TestEjbImpl" scope="singleton">
<property name="testenvejb" ref="testenvejb" />
</bean>
<jee:jndi-lookup id="testenvejb" jndi-name="testenvejb">
<jee:environment>
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1300
</jee:environment>
</jee:jndi-lookup>   
</beans>



=============================================================
 

=============================================================
package com.test.ejb;

import org.springframework.beans.factory.annotation.Autowired;

import com.testenv.TestEnvEjbRemote;

public class TestEjbImpl {
    public TestEjbImpl() {
            System.out.println("called");
    }
    @Autowired
    public TestEnvEjbRemote testenvejb;
   
    public void callMe(){
        System.out.println(testenvejb);
        testenvejb.sayHello("ANish");
    }

    public void setTestenvejb(TestEnvEjbRemote testenvejb) {
        this.testenvejb = testenvejb;
    }
   
}
=============================================================




=============================================================
The Ejb
package com.testenv;

import javax.ejb.Stateless;

/**
 * Session Bean implementation class TestEnvEjb ANISH
 */
@Stateless(mappedName = "testenvejb")
public class TestEnvEjb implements TestEnvEjbRemote, TestEnvEjbLocal {

    /**
     * Default constructor.
     */
    public TestEnvEjb() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void sayHello(String name) {
        System.out.println("name------>>>>>>>"+name);
       
    }

}
package com.testenv;
import javax.ejb.Remote;

@Remote
public interface TestEnvEjbRemote {
   
    public void sayHello(String name);

}

package com.testenv;
import javax.ejb.Local;

@Local
public interface TestEnvEjbLocal {

}
===============================



=============================================================

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class TestEjb {
    public static void main(String[] args) {

        XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource(
                "/META-INF/spring/ODSSpring.xml"));
        System.out.println(bf.getBean("helper"));

        TestEjbImpl helper = (TestEjbImpl) bf.getBean("helper");
        helper.callMe();

    }
}





<jee:jndi-lookup/> (with multiple JNDI environment settings)

Using Jee Calling EJB3.0 with Spring
Understanding the jee environment Settings
jndi environment with jee schema