Spring Ders20 - Aspect Oriented Programming - AspectJ Annotation Style
Spring frameworkünde AspectJ anotasyon stilinin kullanımı old stile göre kontrol edilmesi ve kullanılması kolaydır.
Spring anotasyon stilindeki anotasyonlar
- @Aspect : Aspect sınıfı tanımlar.
- @Pointcut : Kesme noktası tanımlar.
- @Before : Before advice tanımlar. Metot çağrılmadan önce çalışır.
- @After : After advice tanımlar. Metot çağrıldıktan sonra çalışır.
- @AfterReturning : AfterReturning advice tanımlar. Metot return ettikten sonra çalışır.
- @Around : Around advice tanımlar. Metot çağrılmadan önce ve sonra çalışır.
- @AfterThrowing : AfterThrowing advice tanımlar. Metot istisna (exception) attıktan sonra çalışır.
private void metot1(){..} |
private void metot1(){..} |
private void metot1(){..} |
private void metot1(){..} |
Test sınıfındaki tüm public setter metotlara pointcut ismi olarak metot1 metodu uygulanacağı anlamını taşır.
private void metot1(){..} |
Test sınıfındaki tüm int return typeli metotlara pointcut ismi olarak metot1 metodu uygulanacağı anlamını taşır.
Standart spring .jar dosyalarına ilave olarak sahip olmanız gereken .jar dosyaları
- spring-aop.jar
- spring-aspect.jar
- aspectj.jar
- aspectjweaver.jar
Before Advice
Main.java
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); A a = (A) context.getBean("a"); a.mesaj(); } }
A.java
package test; public class A { public void mesaj() { System.out.println("Merhaba"); } }
AspectJ.java
package test; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AspectJ { @Pointcut("execution(* A.*(..))") public void t() {} // pointcut ismi @Before("t()") // t() yerine direkt execution kısmı yazılabilirdi. public void aspectJ_once() { System.out.println("_____once_____"); } }
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="a" class="test.A"></bean> <bean id="aspectJ" class="test.AspectJ"></bean> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> </beans>
Main.java
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); A a = (A) context.getBean("a"); a.mesaj(); } }
A.java
package test; public class A { public void mesaj() { System.out.println("Merhaba"); } }
AspectJ.java
package test; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AspectJ { @Pointcut("execution(* A.*(..))") public void t() {} // pointcut ismi @AfterReturning(pointcut = "t()", returning= "result") // t() yerine direkt execution kısmı yazılabilirdi. public void aspectJ_dondurduktenSonra(JoinPoint jp,Object result){ System.out.println("Metot imzasi\t:\t"+jp.getSignature()); System.out.println("Advice sonucu\t:\t"+result); System.out.println("_____dondurdukten sonra_____"); } }
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="a" class="test.A"></bean> <bean id="aspectJ" class="test.AspectJ"></bean> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> </beans>
After Advice
Main.java
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); A a = (A) context.getBean("a"); a.mesaj(); } }
A.java
package test; public class A { public void mesaj() { System.out.println("Merhaba"); } }
AspectJ.java
package test; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AspectJ { @Pointcut("execution(* A.*(..))") public void t() {} // pointcut ismi @After("t()") // t() yerine direkt execution kısmı yazılabilirdi. public void aspectJ_sonra() { System.out.println("_____sonra_____"); } }
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="a" class="test.A"></bean> <bean id="aspectJ" class="test.AspectJ"></bean> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> </beans>
Around Advice
Main.java
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); A a = (A) context.getBean("a"); a.mesaj(); } }
A.java
package test; public class A { public void mesaj() { System.out.println("Merhaba"); } }
AspectJ.java
package test; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AspectJ { @Pointcut("execution(* A.*(..))") public void t() {} // pointcut ismi @Around("t()") // t() yerine direkt execution kısmı yazılabilirdi. public void aspectJ_etrafinda(ProceedingJoinPoint pjp) throws Throwable { System.out.println("_____etrafında_____"); pjp.proceed(); System.out.println("_____etrafında_____"); } }
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="a" class="test.A"></bean> <bean id="aspectJ" class="test.AspectJ"></bean> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> </beans>
AfterThrowing Advice
Main.java
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); A a = (A) context.getBean("a"); try { a.mesaj(9); } catch (Exception e) { System.out.println(e); } } }
A.java
package test; public class A { public void mesaj(int i) throws Exception { if (i < 10) { throw new Exception("Istisna : sayi 10dan kucuk"); } else { System.out.println("sayi : " + i); } } }
AspectJ.java
package test; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AspectJ { @Pointcut("execution(* A.*(..))") public void t() {} // pointcut ismi @AfterThrowing(pointcut = "t()",throwing= "error") // t() yerine direkt execution kısmı yazılabilirdi. public void aspectJ_throwing(JoinPoint jp,Throwable error){ System.out.println("Metot imzasi\t:\t"+jp.getSignature()); System.out.println("Exception\t:\t"+error); System.out.println("_____exception attıktan sonra_____"); } }
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="a" class="test.A"></bean> <bean id="aspectJ" class="test.AspectJ"></bean> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> </beans>
Yorumlar
Yorum Gönder