Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring AOP Advice

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 250
    Comment on it

    Spring AOP :- In Spring we can use AOP module. The AOP is Aspect Oriented Programming that works on cross-cutting concerns. This breaks the program logic into distinct part and these cross-cutting concerns are separated from the business logic of application. some examples of aspect like logging, declarative transactions and security etc. In Below Code you can see the Example of AOP Advice module.

    Student.java

    package com.service;
    
    public class Student 
    {
        private int rollno;
        private String name;
    
    
        public void setRollno(int rollno) {
            this.rollno = rollno;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        public void printName() {
            System.out.println("NAME:-"+this.name);
    
        }
        public void printRollNo() {
            System.out.println("ROLLNO:-"+this.rollno);
        }
    
        public void throwException()
        {
            System.out.println("Throwing NullPointer Exception");
            throw new NullPointerException();
    
        }
    
    
    }
    

    App.java

    package com.main;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.core.io.ClassPathResource;
    
    import com.service.Student;
    
    public class App {
    
        public static void main(String[] args) {
    
    
            ApplicationContext appContext=new ClassPathXmlApplicationContext("app-context.xml");
            Student student=(Student)appContext.getBean("studentProxy");
            student.printRollNo();
            student.printName();
    
            try{
                    student.throwException();
            }
            catch(Exception e){}
        }
    
    }
    

    1. Before Advice:- This will execute before the execution of method. The below code showMethodBefore class implements MethodBeforeAdvice interface.

    MethodBefore.java

    package com.aop;
    import java.lang.reflect.Method;
    import org.springframework.aop.MethodBeforeAdvice;
    public class MethodBefore implements MethodBeforeAdvice {
    
        public void before(Method method, Object[] args, Object target) throws Throwable 
        {
            System.out.println("THE REQUEST IS INTERCEPT BY THE BEFORE METHOD EXCUTION");
        }
    
    }
    

    app-context.xml

    <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-2.5.xsd">
    
        <bean id="studentService" class="com.service.Student">
            <property name="rollno" value="1100822" />
            <property name="name" value="Manish" />
        </bean>
    
         <bean id="interceptBeforeMethodExecution" class="com.aop.MethodBefore" />
    
        <bean id="studentProxy" 
                    class="org.springframework.aop.framework.ProxyFactoryBean">
    
            <property name="target" ref="studentService" />
    
            <property name="interceptorNames">
                <list>
                    <value>interceptBeforeMethodExecution</value>
    
                </list>
            </property>
        </bean> 
    
     </beans>
    

    2. After Return Advice:- This will executed after the method is return a result. The below code show AfterReturning class which implements AfterReturningAdvice interface.

    AfterReturning.java

    package com.aop;
    
    import java.lang.reflect.Method;
    
    import org.springframework.aop.AfterReturningAdvice;
    
    public class AfterReturning implements AfterReturningAdvice {
    
        public void afterReturning(Object returnVal, Method method, Object[] args,
                Object target) throws Throwable {
    
    
            System.out.println("EXECUTE AFTER RETURN FROM METHOD ");
    
        }
    
    }
    

    app-context.xml

    <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-2.5.xsd">
    
        <bean id="studentService" class="com.service.Student">
            <property name="rollno" value="1100822" />
            <property name="name" value="Manish" />
        </bean>
    
    
         <bean id="interceptAfterMethodReturns" class="com.aop.AfterReturning" />
    
        <bean id="studentProxy" 
                    class="org.springframework.aop.framework.ProxyFactoryBean">
    
            <property name="target" ref="studentService" />
    
            <property name="interceptorNames">
                <list>
                    <value>interceptAfterMethodReturns</value>
                </list>
            </property>
        </bean> 
    
     </beans>
    

    3. After throwing Advice:- This will execute after the method throws an exception. The Below code show AfterThrowing class which implements ThrowsAdvice interface.

    AfterThrowing.java

    package com.aop;
    
    import org.springframework.aop.ThrowsAdvice;
    
    public class AfterThrowing implements ThrowsAdvice {
    
        public void afterThrowing(Exception e) throws Throwable {
            System.out.println("After Throws The Exception");
        }
    }
    

    app-context.xml

    <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-2.5.xsd">
    
        <bean id="studentService" class="com.service.Student">
            <property name="rollno" value="1100822" />
            <property name="name" value="Manish" />
        </bean>
    
    
          <bean id="interceptAfterThrowing" class="com.aop.AfterThrowing" /> 
    
    
        <bean id="studentProxy" 
                    class="org.springframework.aop.framework.ProxyFactoryBean">
    
            <property name="target" ref="studentService" />
    
            <property name="interceptorNames">
                <list>
                    <value>interceptAfterThrowing</value>
                </list>
            </property>
        </bean> 
    
     </beans>
    

    4. Around Advice:- This will combines all three advices above, and execute during method execution. Around class which implements MethodInterceptor interface. We can call the methodInvocation.proceed() to proceed on the original method execution otherwise the original method will not execute.

    Around.java

    package com.aop;
    
    import java.util.Arrays;
    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    
    public class Around implements MethodInterceptor{
    
        public Object invoke(MethodInvocation invocation) throws Throwable {
    
            System.out.println("\nMethod Name:-"+invocation.getMethod().getName());
            System.out.println("Method Args:-"+Arrays.toString(invocation.getArguments()));
    
            System.out.println("Before Execution of Method");
    
            try{
    
                Object obj=invocation.proceed();
    
                System.out.println("After Execution of Method");
    
            return obj;
            }
            catch(Exception e)
            {
                System.out.println("After throwing Exception");
                throw e;
            }
        }
    
    }
    

    app-context.xml

    <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-2.5.xsd">
    
        <bean id="studentService" class="com.service.Student">
            <property name="rollno" value="1100822" />
            <property name="name" value="Manish" />
        </bean>
    
    
          <bean id="interceptAround" class="com.aop.Around" />   
    
        <bean id="studentProxy" 
                    class="org.springframework.aop.framework.ProxyFactoryBean">
    
            <property name="target" ref="studentService" />
    
            <property name="interceptorNames">
                <list>
                    <value>interceptAround</value>
                </list>
            </property>
        </bean> 
    
     </beans>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: