Search This Blog

Tuesday 23 July 2013

Simple before Advice Spring Demo





Simple before Advice Spring Demo



src\Test.java
===============================================================
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.target.Service;
public class Test {
                public static void main(String[] args) {
                                ClassPathXmlApplicationContext container=new ClassPathXmlApplicationContext("applicationContext.xml");
                                Service target =(Service)container.getBean("proxy");
                                System.out.println("The result is :"+target.addall(1, 2));
               
                }
}

src\applicationContext.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

                <bean id="target" class="com.target.Service"/>
                <bean id="beforeadvice" class="com.aspect.Beforeadvice"/>
                <bean id="aspect4"  class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
                                  <property name="advice">
                                                 <ref local="beforeadvice"/>
                                  </property>
                                  <property name="patterns">
                                                 <value>.*add.*</value>
                                  </property>
                </bean>
                <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
                                                <property name="target">
                                                                <ref local="target"/>
                                                </property>
                                                <property name="interceptorNames">
                                                                <list>
                                                                                <value>aspect4</value>
                                                               
                                                                </list>
                                                </property>
                </bean>
</beans>

src\com\aspect\Beforeadvice.java
====================================================================================
package com.aspect;

import java.lang.reflect.Method;


import org.springframework.aop.MethodBeforeAdvice;

public class Beforeadvice implements MethodBeforeAdvice {

                @Override
                public void before(Method arg0, Object[] arg1, Object arg2)
                                                throws Throwable {
                               
                                System.out.println("Invoking method:"+arg0);
                                System.out.println("The arguments to the method are:"+arg1[0]+"and"+arg1[1]);
                                System.out.println("The target is :"+arg2 );
                }
}

src\com\target\Service.java
====================================================================================
package com.target;

public class Service {
               
                public int addall(int a,int b){
                               
                                return a+b;
                }
}

No comments:

Post a Comment