Spring Ders19 - Aspect Oriented Programming - Old Style
- Before Advice : Metot çağrısından önce çalışır.
- After Advice : Metot çağrısından sonra çalışır.
- Around Advice : Metot çağrısından önce ve sonra çalışır.
- Throw Advice : Metot exception (istisna) attığında çalışır.
Bunlarla ilgili şimdi 4 ayrı program yapalım.
Before Advice
Metot çağrısından ö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) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); A a=(A)context.getBean("proxy"); a.mesaj(); } }
A.java
package test; public class A { public void mesaj() { System.out.println("Merhaba"); } }
Once.java
package test; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class Once implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { 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="once" class="test.Once"></bean> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="a" /> <property name="interceptorNames"> <list> <value>once</value> </list> </property> </bean> </beans>
Metot çağrısından 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) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); A a=(A)context.getBean("proxy"); a.mesaj(); } }
A.java
package test; public class A { public void mesaj() { System.out.println("Merhaba"); } }
Sonra.java
package test; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class Sonra implements AfterReturningAdvice { @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { 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="sonra" class="test.Sonra"></bean> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="a" /> <property name="interceptorNames"> <list> <value>sonra</value> </list> </property> </bean> </beans>
Around Advice
Metot çağrımından ö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) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); A a=(A)context.getBean("proxy"); a.mesaj(); } }
A.java
package test; public class A { public void mesaj() { System.out.println("Merhaba"); } }
Etranfinda.java
package test; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class Etrafinda implements MethodInterceptor { @Override public Object invoke(MethodInvocation i) throws Throwable { System.out.println("_____Etrafında_____"); Object o = i.proceed(); System.out.println("_____Etrafında_____"); return o; } }
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="etrafinda" class="test.Etrafinda"></bean> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="a" /> <property name="interceptorNames"> <list> <value>etrafinda</value> </list> </property> </bean> </beans>
Throws Advice
Metot exception (istisna) attığında ç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("proxy"); try { a.mesaj(9); } catch (Exception e) { e.printStackTrace(); } } }
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); } } }
Istisna.java
package test; import org.springframework.aop.ThrowsAdvice; public class Istisna implements ThrowsAdvice { public void afterThrowing(Exception e){ 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" 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="istisna" class="test.Istisna"></bean> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="a" /> <property name="interceptorNames"> <list> <value>istisna</value> </list> </property> </bean> </beans>
Açıklamalar
ProxyFactoryBean sınıfı spring frameworkü tarafından sağlanan bir sınıftır. Bu sınıf iki tane property (özellik) içerir. Bunlar target ve interceptorNames özelllikleridir.
- target özelliğine aop uygulanacak bean referans olarak verilmelidir.
- interceptorNames özelliğine ise, aop sınıfının beani verilmelidir.
<?xml version="1.0" encoding="UTF-8"?> <beans ..
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="a" />
<property name="interceptorNames"> <list> <value>......</value> </list> </property> </bean>
</beans>
Yorumlar
Yorum Gönder