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.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.Student;
public class App {
public static void main(String[] args) {
ApplicationContext appContext=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
Student student=(Student)appContext.getBean("studentProxy");
student.printRollNo();
student.printName();
try{
student.throwException();
}
catch(Exception e){}
}
}
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;
}
}
}
Pointcut:- PointCut indicate that which method should intercept.
Advisor:- Advisor is the Group Advice and Pointcut into a single unit, and pass them to a proxy factory object.
spring-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="studentPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedName" value="printName" />
</bean>
<bean id="studentAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut" ref="studentPointcut" />
<property name="advice" ref="interceptAround" />
</bean>
<bean id="studentProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="studentService" />
<property name="interceptorNames">
<list>
<value>studentAdvisor</value>
</list>
</property>
</bean>
</beans>
0 Comment(s)