Module 4 Assignments
Module 4 Assignments
Programming
Join point : This represents a point in your application where you can plug-in the AOP
aspect. You can also say, it is the actual place in the application where an action will be taken
using Spring AOP framework.
● An application has thousands of opportunities or points to apply Advice. These points are
known as join points. For example – Advice can be applied at every invocation of a
method or exception be thrown or at various other points. But Spring AOP currently
supports only method execution join points (advising the execution of methods on Spring
beans).
Join point is any point in your program such as method execution, exception handling,
field access etc. Spring supports only method execution join points
Advice :
Advice represents an action taken by an aspect at a particular join point.
There are different types of advices:
● After (finally) Advice: it executes after a join point regardless of join point exit
whether normally or exceptional return.
Spring AOP can be used in 3 ways given below. But the widely used approach is
Spring AspectJ Annotation Style.
2. By AspectJ annotation-style
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"
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 "
>
<bean id="stud" class="com.aop.Student">
</bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<aop:config>
<aop:aspect id="log" ref="logbean">
<aop:before method="beforeadvice" pointcut="execution(public void
display())"/>
<aop:after method="afteradvice" pointcut="execution(public void
display())"/>
</aop:aspect>
</aop:config>
</beans>
Around Advice
Around is an advice type which ensures that an advice runs before and after the method execution.
Syntax
<aop:config>
</aop:aspect>
</aop:config>
Where,
package com.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggingAspect {
public void aroundadvice(ProceedingJoinPoint jp) throws Throwable {
System.out.println("Student data(Around Advice)");
jp.proceed();
System.out.println("Above data is of required student(Around
Advice)");
}
}
<?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"
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 "
>
<bean id="stud" class="com.aop.Student">
<property name="ID" value="56"></property>
<property name="NAME" value="Shweta Waghmare"></property>
</bean>
<bean id="l" class="com.aop.LoggingAspect"></bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<aop:config>
<aop:aspect id="LoggingAspect" ref="l">
<aop:around method="aroundadvice" pointcut="execution(public void display())"/>
</aop:aspect>
</aop:config>
</beans>
package com.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentMainApp {
public static void main(String[] args) {
ApplicationContext context =new
ClassPathXmlApplicationContext("studentconfig.xml");
Student s= (Student) context.getBean("stud");
s.display();
}
}
//Main File
4. Write a program to demonstrate Spring AOP – after returning advice.
package com.aop;
public class LoggingAspect {
public void afterreturnadvice(Object retval) {
System.out.println("Method got successfully executed (After Returning
(value) Advice )");
}
}
<?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"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd config.xml
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "
>
<bean id="stud" class="com.aop.Student">
<property name="ID" value="56"></property>
<property name="NAME" value="Shweta Waghmare"></property>
</bean>
<bean id="l" class="com.aop.LoggingAspect"></bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy><aop:config>
<aop:aspect id="LoggingAspect" ref="l">
<aop:after-returning method="afterreturnadvice" returning="retval"
pointcut="execution(public void display())"/>
</aop:aspect></aop:config>
</beans>
Main method
package com.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
}
5. Write a program to demonstrate Spring AOP – after throwing advice.
package com.aop;
public class Student {
private int ID;
private String NAME;
private int age; Student.java
//getter and setter
public void display() {
System.out.println("ID:" + ID);
System.out.println("Name:" + NAME);
}
public void validate(int age) {
if (age < 18) {
throw new ArithmeticException();
} else {
System.out.println("Valid age");
}
}
}
LoggingAspect .java
package com.aop;
</aop:aspect></aop:config>
Main App
public class StudentMainApp {
public static void main(String[] args) {
ApplicationContext context =new
ClassPathXmlApplicationContext("studentconfig.xml");
Student s= (Student) context.getBean("stud");
s.display();
System.out.println("After throwing advice:");
s.validate(24);
}
}