Spring Ders21 - Aspect Oriented Programming - AspectJ Xml Configuration Style
Spring'de Aspect Oriented Programmingi .xml configuration (yapılandırma) dosyasında yapabilirsiniz. Bu AspectJ Xml Configuration Style olarak adlandırılır.
AspectJ anotasyon stilindeki tüm advicelar burada da desteklenir.
- aop:before : Before advice tanımlar. Metot çağrılmadan önce çalışır.
- aop:after : After advice tanımlar. Metot çağrıldıktan sonra çalışır.
- aop:after-returning : AfterReturning advice tanımlar. Metot return ettikten sonra çalışır.
- aop:around : Around advice tanımlar. Metot çağrılmadan önce ve sonra çalışır.
- aop:after-throwing : AfterThrowing advice tanımlar. Metot istisna (exception) attıktan sonra çalışır.
aop:before
Before advice tanımlar. Metot çağrılmadan önce çalışır.
Before advice tanımlar. Metot çağrılmadan önce çalışır.
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; public class AspectJ { public void aspectJ_once(JoinPoint jp){ System.out.println("_____once_____"); //System.out.println("Metot imzası : " + jp.getSignature()); } }
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" 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="a" class="test.A"></bean> <bean id="aspect" class="test.AspectJ"></bean> <aop:config> <aop:aspect id="aspectim" ref="aspect"> <!-- @Before --> <aop:pointcut id="once" expression="execution(* test.A.*(..))" /> <aop:before method="aspectJ_once" pointcut-ref="once" /> </aop:aspect> </aop:config> <aop:aspectj-autoproxy /> </beans>
aop:after
After advice tanımlar. Metot çağrıldıktan sonra çalışır.
Main.java package test; import org.aspectj.lang.JoinPoint; public class AspectJ { public void aspectJ_sonra(JoinPoint jp) { System.out.println("_____sonra_____"); // System.out.println("Metot imzası : " + jp.getSignature()); } }
A.java
package test; public class A { public void mesaj() { System.out.println("Merhaba"); } }
AspectJ.java
package test; import org.aspectj.lang.JoinPoint; public class AspectJ { public void aspectJ_sonra(JoinPoint jp) { System.out.println("_____sonra_____"); // System.out.println("Metot imzası : " + jp.getSignature()); } }
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" 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="a" class="test.A"></bean> <bean id="aspect" class="test.AspectJ"></bean> <aop:config> <aop:aspect id="aspectim" ref="aspect"> <!-- @After --> <aop:pointcut id="sonra" expression="execution(* test.A.*(..))" /> <aop:after method="aspectJ_sonra" pointcut-ref="sonra" /> </aop:aspect> </aop:config> <aop:aspectj-autoproxy /> </beans>
AfterReturning advice tanımlar. Metot return ettikten sonra çalışır.
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; public class AspectJ { public void aspectJ_dondurduktenSonra(JoinPoint jp,Object result){ System.out.println("_____dondurdukten sonra_____"); //System.out.println("Metot imzası : " + jp.getSignature()); //System.out.println("Advice sonucu : "+result); } }
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" 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="a" class="test.A"></bean> <bean id="aspect" class="test.AspectJ"></bean> <aop:config> <aop:aspect id="aspectim" ref="aspect"> <!-- @AfterReturning --> <aop:pointcut id="dondurduktenSonra" expression="execution(* test.A.*(..))" /> <aop:after-returning method="aspectJ_dondurduktenSonra" returning="result" pointcut-ref="dondurduktenSonra" /> </aop:aspect> </aop:config> <aop:aspectj-autoproxy /> </beans>
aop:around
Around advice tanımlar. Metot çağrılmadan önce ve sonra çalışır.
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; public class AspectJ { public Object aspectJ_etrafinda(ProceedingJoinPoint pjp) throws Throwable { System.out.println("_____etrafinda_____"); Object obj = pjp.proceed(); System.out.println("_____etrafinda_____"); return obj; } }
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" 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="a" class="test.A"></bean> <bean id="aspect" class="test.AspectJ"></bean> <aop:config> <aop:aspect id="aspectim" ref="aspect"> <!-- @Around --> <aop:pointcut id="etrafinda" expression="execution(* test.A.*(..))" /> <aop:around method="aspectJ_etrafinda" pointcut-ref="etrafinda" /> </aop:aspect> </aop:config> <aop:aspectj-autoproxy /> </beans>
aop:after-throwing
AfterThrowing advice tanımlar. Metot istisna (exception) attıktan sonra çalışır.
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; public class AspectJ { public void aspectJ_throwing(JoinPoint jp, Throwable error) { System.out.println("Metot imzası : " + jp.getSignature()); System.out.println("Exception : " + error); System.out.println("Bir istisna meydana geldi."); } }
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" 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="a" class="test.A"></bean> <bean id="aspect" class="test.AspectJ"></bean> <aop:config> <aop:aspect id="aspectim" ref="aspect"> <!-- @AfterThrowing --> <aop:pointcut id="throwing" expression="execution(* test.A.*(..))" /> <aop:after-throwing method="aspectJ_throwing" throwing="error" pointcut-ref="throwing" /> </aop:aspect> </aop:config> <aop:aspectj-autoproxy /> </beans>
Yorumlar
Yorum Gönder