Practical 8 :- Practical based on Spring Aspect Oriented Programming (AOP)
Write a program to demonstrate Spring AOP – before advice.
code
Logging.java package com.hiraymca; public class Logging { //Types of Advice //1 beforeAdvice public void beforeAdvice() { System.out.println("Setuping Student Profile "); } } Student.java package com.hiraymca; public class Student { private int age; private String name; public int getAge() { System.out.println("Age-"+age); return age; } public void setAge(int age) { this.age = age; } public String getName() { System.out.println("Name="+name); return name; } public void setName(String name) { this.name = name; } } MainApp.java package com.hiraymca; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml"); Student s=(Student)context.getBean("student"); s.getName(); s.getAge(); } } Beans.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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <aop:config> <aop:aspect id="log" ref="logging"> <aop:pointcut expression="execution(* com.hiraymca.*.*(..))" id="myid"/> <aop:before method="beforeAdvice" pointcut-ref="myid"/> </aop:aspect> </aop:config> <bean id="student" class="com.hiraymca.Student"> <property name="name" value="Sukhiram"></property> <property name="age" value="35"></property> </bean> <bean id="logging" class="com.hiraymca.Logging"></bean> </beans>
Write a program to demonstrate Spring AOP – after advice
code
Logging.java package com.hiraymca; public class Logging { //Types of Advice //1 beforeAdvice public void afterAdvice() { System.out.println("Student Profile done "); } } Student.java package com.hiraymca; public class Student { private int age; private String name; public int getAge() { System.out.println("Age-"+age); return age; } public void setAge(int age) { this.age = age; } public String getName() { System.out.println("Name="+name); return name; } public void setName(String name) { this.name = name; } } MainApp.java package com.hiraymca; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml"); Student s=(Student)context.getBean("student"); s.getName(); s.getAge(); } } Beans.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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <aop:config> <aop:aspect id="log" ref="logging"> <aop:pointcut expression="execution(* com.hiraymca.*.*(..))" id="myid"/> <aop:after method="afterAdvice" pointcut-ref="myid" /> </aop:aspect> </aop:config> <bean id="student" class="com.hiraymca.Student"> <property name="name" value="Sukhiram"></property> <property name="age" value="35"></property> </bean> <bean id="logging" class="com.hiraymca.Logging"></bean> </beans> Output Name=Sukhiram Student Profile done Age-35 Student Profile done
Write a program to demonstrate Spring AOP – around advice.
code
Write a program to demonstrate Spring AOP – after returning advice.
Write a program to demonstrate Spring AOP – after throwing advice.
Write a program to demonstrate Spring AOP – pointcuts.