Results 1 to 7 of 7

Thread: Implemeting AOP/AspectJ in ejb 2.0

  1. #1

    Default Implemeting AOP/AspectJ in ejb 2.0

    It has been one week now. I have tried to implement aop (spring aop and aspectj both) but with no success. I am able to get ejb proxy with spring context but not able to apply advices on it.
    I have tried following xml config:

    LCSession is remote interface and LCSessionBean in ejb implementation

    1.


    Code:
    <?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">
    
    	<!-- target -->	
    	<bean id="lcBean" class="com.bnp.ivision.product.lc.sessionejb.LCSessionBean">
    	</bean>
    	
    	<!-- Advices -->
    	<bean id="testAdvice" class="com.bnp.ivision.spring.TestAdvice">
    	</bean>
    	
    	
    	
    
    	<!-- Mapping: ProxyFactoryBean -->
    	<!-- org.springframework.aop.framework.ProxyFactoryBean  
    	org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean -->
    	<bean id="beanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    		<property name="target" ref="slsbProxy"/>	
    		<property name="proxyInterfaces">
    			<list>
    				<value>com.bnp.ivision.product.lc.sessionejb.LCSession</value>
    				
    			</list>
    		</property>
    		 <property name="interceptorNames">
    			<list>
    				<value>testAdvisor</value>
    			</list>
    		</property>  
    	</bean>
    	
    	<bean id="slsbProxy" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
    		<property name="businessInterface" value="com.bnp.ivision.product.lc.sessionejb.LCSession"/> 
    		<property name="jndiName" value="LCSession"></property>	
    	</bean>
    	
    	<!-- NameMatchMethodPointcutAdvisor -->
    	<bean id="testAdvisor" 
    			class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
    			<property name="advice" ref="testAdvice"/>
       </bean>	
    	
    	
    </beans>
    
    
    
    package com.bnp.ivision.spring;
    
    import java.lang.reflect.Method;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    // MethodBeforeAdvice
    public class TestAdvice 
    	implements MethodBeforeAdvice{
    
    	public void before(Method method,
    			Object[] args, Object target)
    			throws Throwable {
    		System.out.println("In before of "
    				+method.getName());
    		if(args[0] instanceof Integer){
    			checkAccountNumber((Integer)args[0]);
    		}
    	}
    
    	// implementation of Advice
    	public boolean checkAccountNumber(int accountNumber) {
    		System.out.println("Checking Account Number :"
    				+accountNumber);
    		return true;
    	}
    }
    2.


    Code:
    <?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"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    	<aop:aspectj-autoproxy/>
    	
    <bean id="myService" class="com.bnp.ivision.product.lc.sessionejb.LCSessionBean" />
    
      <bean id="serviceLayerExceptionBarrier"
        class="com.bnp.ivision.spring.AspectJAdvice" />
    
      <aop:config>
        <aop:pointcut id="facadeOperation"
          expression="target(com.bnp.ivision.product.lc.sessionejb.LCSession)" />
        
      <aop:aspect id="myAspect" ref="serviceLayerExceptionBarrier">
      <aop:before method="testAdvice" pointcut-ref="facadeOperation" />
        <aop:after-throwing pointcut-ref="facadeOperation"
          throwing="t" method="testAdviceThrow" />
        </aop:aspect>
      </aop:config>
      
      <bean id="myServiceFacade"
      class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
        <property name="jndiName" value="LCSession" />
        
        <property name="businessInterface" value="com.bnp.ivision.product.lc.sessionejb.LCSession" />
    </bean>
      
      </beans>

    Is it possible to advise EJBs? If yes,how?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    1) How are you testing (Are you using an ApplicationContext and not a BeanFactory!)
    2) IN config 2 your pointcut doesn't do anything
    3) Why do you have a slsb and a local instance?

    Code:
    <aop:config>
    	<aop:pointcut id="facadeOperation" expression="execution(* com.bnp.ivision.product.lc.sessionejb.LCSession.*(..))" />    
    	<aop:aspect id="myAspect" ref="serviceLayerExceptionBarrier">
      		<aop:before method="testAdvice" pointcut-ref="facadeOperation" />
        		<aop:after-throwing pointcut-ref="facadeOperation" throwing="t" method="testAdviceThrow" />
        	</aop:aspect>
    </aop:config>
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Hi Marten,
    Here is my client code:
    Code:
    Resource res = new ClassPathResource("com/bnp/ivision/spring/aopj.xml");
    		
    		BeanFactory factory = new XmlBeanFactory(res);
    		
    		LCSession bn=(LCSession)factory.getBean("myServiceFacade");
    
                // calling ejb method
                 bn.ejbMethod();
    I tried the pointcut code provided by you, but still same !

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    As I already mentioned in my previous post.. Use an ApplicationContext not a BeanFactory. I suggest a read of the reference guide which explains the differences.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    Hi,
    Thanks a lot.
    It worked by using ApplicationContext for AspectJ. But for spring AOP(my first config), I needed to use BeanFactory.

    Can u elaborate about the diffrences in both approaches?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    For both you should have used an ApplicationContext, although it works in the first example with a BeanFactory.

    Why it works is explained in the reference guide (which I still suggest you read).

    Your second approach uses implicit proxy creation, where as the first it was explicit (due to the ProxyFactoryBean). Hence it worked. In general use an ApplicationContext...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    302

    Default

    Quote Originally Posted by chirag.jain18 View Post
    Hi,
    Thanks a lot.
    It worked by using ApplicationContext for AspectJ. But for spring AOP(my first config), I needed to use BeanFactory.

    Can u elaborate about the diffrences in both approaches?
    Read following part of the reference, http://static.springsource.org/sprin...ns-beanfactory

    it says to use application context and also says,

    The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purposes of backward compatibility with the large number of third-party frameworks that integrate with Spring.
    Amila Domingo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •