Spring – 29 – Aspect Oriented Programming (AOP) – 03
Merhaba Arkadaslar
Onceki bolumde basit Spring AOP orneklerini inceledik. Benzer ornekleri XML konfigurasyonu ile yapalim.
Advice Implementation siniflarimiz , LoggerWriter ve Validator siniflarimiz ayni sekilde olacak.
Konfigurasyon dosyamizda ProxyFactoryBean bean tanimlari olusturuyoruz target ve interceptorNames icin setter injection gerceklestiriyoruz.
34.spring.aop.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="logger" class="_34.spring.aop.service.LoggerWriter" /> <bean id="validator" class="_34.spring.aop.service.Validator" /> <bean id="simpleBeforeAdviceImpl" class="_34.spring.aop.service.SimpleBeforeAdviceImpl" /> <bean id="simpleAfterReturningAdviceImpl" class="_34.spring.aop.service.SimpleAfterReturningAdviceImpl" /> <bean id="simpleAroundAdviceImpl" class="_34.spring.aop.service.SimpleAroundAdviceImpl" /> <bean id="simpleThrowsAdviceImpl" class="_34.spring.aop.service.SimpleThrowsAdviceImpl" /> <bean id="proxyBefore" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="logger"/> <property name="interceptorNames"> <list> <value>simpleBeforeAdviceImpl</value> </list> </property> </bean> <bean id="proxyAfterReturning" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="logger"/> <property name="interceptorNames"> <list> <value>simpleAfterReturningAdviceImpl</value> </list> </property> </bean> <bean id="proxyAround" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="logger"></property> <property name="interceptorNames"> <list> <value>simpleAroundAdviceImpl</value> </list> </property> </bean> <bean id="proxyThrows" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="validator"/> <property name="interceptorNames"> <list> <value>simpleThrowsAdviceImpl</value> </list> </property> </bean> </beans>
SimpleBeforeAdviceTest.java
package _34.spring.aop.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import _34.spring.aop.service.LoggerWriter; public class SimpleBeforeAdviceTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("34.spring.aop.xml"); LoggerWriter object = context.getBean("proxyBefore", LoggerWriter.class); object.log(); context.close(); } }
Ornegimizi calistirdigimizda ;
Before method: log This is log method !
SimpleAfterReturningAdviceTest.java
package _34.spring.aop.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import _34.spring.aop.service.LoggerWriter; public class SimpleAfterReturningAdviceTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("34.spring.aop.xml"); LoggerWriter object = context.getBean("proxyAfterReturning", LoggerWriter.class); object.log(); context.close(); } }
Ornegimizi calistirdigimizda;
This is log method ! After method: log
SimpleAroundAdviceTest.java
package _34.spring.aop.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import _34.spring.aop.service.LoggerWriter; public class SimpleAroundAdviceTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("34.spring.aop.xml"); LoggerWriter object = context.getBean("proxyAround", LoggerWriter.class); object.log(); context.close(); } }
Ornegimizi calistirdigimizda ;
Before ! This is log method ! After !
SimpleThrowsAdviceTest.java
package _34.spring.aop.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import _34.spring.aop.service.Validator; public class SimpleThrowsAdviceTest { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("34.spring.aop.xml"); Validator validator = context.getBean("proxyThrows", Validator.class); try { validator.validateAge(-10); } catch (ArithmeticException e) { System.out.println("ArithmeticException#catch\n"); } try { validator.parseAge("Exception"); } catch (NumberFormatException e) { System.out.println("NumberFormatException#catch\n"); } try { validator.throwRuntimeException(); } catch (RuntimeException e) { System.out.println("RuntimeException#catch\n"); } context.close(); } }
Ornegimizi calistirdigimizda ;
*** ArithmeticException Capture Caught: java.lang.ArithmeticException Method: validateAge *** ArithmeticException#catch *** NumberFormatException Capture Caught: java.lang.NumberFormatException Method: parseAge *** NumberFormatException#catch *** Generic Exception Capture Caught: java.lang.RuntimeException *** RuntimeException#catch
Github kaynak dosyalar/ source folder
leventerguder/injavawetrust-spring-tutorial
Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer
Leave a Reply