enterprise aop
play

Enterprise AOP With the Spring Framework Jrgen Hller VP & - PowerPoint PPT Presentation

Enterprise AOP With the Spring Framework Jrgen Hller VP & Distinguished Engineer, Interface21 Agenda Spring Core Container Spring AOP Framework AOP in Spring 2.0 Example: Transaction Advice What's Coming in Spring


  1. Enterprise AOP With the Spring Framework Jürgen Höller VP & Distinguished Engineer, Interface21

  2. Agenda � Spring Core Container � Spring AOP Framework � AOP in Spring 2.0 � Example: Transaction Advice � What's Coming in Spring 2.1?

  3. Spring Framework (1) � Java / J2EE Application Framework � originally based on Rod Johnson’s book “J2EE Design & Development” (Wiley, 2002) � popularized by "J2EE Development without EJB" (Rod Johnson, Jürgen Höller; Wiley, 2004) � Focus on "Plain Old Java Objects" (POJOs) � natural component model for applications � Open Source Project on SourceForge � since February 2003 � Apache license � >40000 downloads per month

  4. Spring Framework (2) � Business objects as decoupled POJOs � configuration and wiring through framework • or usage as normal Java objects � independent from the actual environment • no unnecessary ties to a framework � reusable in any kind of environment • in particular: testability in unit / integration tests � Generic middleware services � e.g. declarative transactions for POJOs • flexible alternative to EJB CMT � for all applications, including standalone • leverage container services when available

  5. Core Container (1) � "Inversion of Control" � configuration and lifecycle of application objects � objects do not configure themselves, but get configured from the outside � objects don't know the origin of their configuration � "Dependency Injection" � "setter-based" (JavaBean properties) � "constructor-based" (constructor arguments) � alternative: "Service Lookup" • for example: JNDI

  6. Core Container (2) public class ShopFacadeImpl implements ShopFacade { private OrderDao orderDao ; private ItemDao itemDao ; ... public void setOrderDao(OrderDao orderDao) { this.orderDao = orderDao; } public void setItemDao(ItemDao itemDao) { this.itemDao = itemDao; } ... public void insertOrder(Order order) { this.orderDao .insertOrder(order); this.itemDao .updateQuantity(order); } }

  7. Core Container (3) � Fine-grained externalized configuration � representing the internal structure of the application • references to other components • configuration parameters � enables flexible configuration management • at fine-grained component level • switching between different deployment scenarios � XML bean definitions � most common configuration format � often: separate admin properties file • linked into XML bean definitions through placeholders

  8. Core Container (4) <bean id=" dataSource " class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${ jdbc.username}"/> <property name="password" value="${ jdbc.password}"/> </bean> <bean id=" orderDao " class="com.myshop.dao.jdbc.JdbcOrderDao"> <property name="dataSource" ref=" dataSource "/> </bean> <bean id=" petStore " class="com.myshop.service.ShopFacadeImpl"> <property name="orderDao" ref=" orderDao "/> <property name="itemDao" ref="itemDao"/> </bean>

  9. AOP Framework (1) � Proxy-based Aspect Orientation � proxies for arbitrary POJOs • no fixed component model � flexible combination of interceptors � instance-based AOP � Interceptors for Cross-Cutting Concerns � actions at beginning/end of method call Caller Target method intercept

  10. AOP Framework (2) � Do not repeat code: factor out interceptor � e.g. logging: configurable trace log � e.g. security: authorization checks � e.g. common exception handling � e.g. transaction demarcation � Method interceptor � interceptor can be applied to any methods � interceptor can be enabled/disabled � AOP Alliance: MethodInterceptor interface � reuse of pre-built interceptors

  11. AOP Framework (3) � Custom interceptor implementations � rarely necessary, but straightforward � usually delegate to target ("proceed") � can also abort invocations • for example: authorization interceptor public class TestInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation inv) throws Throwable { System.out.println("before invocation"); try { return inv.proceed(); } finally { System.out.println("after invocation"); } } }

  12. AOP Framework (4) � AOP Framework = creation of proxies � build proxy for target object • JDK dynamic proxy or CGLIB proxy � build interceptor chain between proxy and target • according to AOP advice configuration � Hooks into Spring's core container � FactoryBean or auto-proxy creator � core container does not create proxies itself!

  13. ProxyFactory ShopFacadeImpl facade = new ShopFacadeImpl(); facade.setOrderDao(...); ProxyFactory pf = new ProxyFactory(); pf.setTarget(facade); pf.addAdvice(new SimpleTraceInterceptor()); pf.addInterface(ShopFacade.class); ShopFacade proxy = (ShopFacade) pf.getProxy(); proxy.insertOrder(...); // Every call on the proxy will delegate to the target // but first go through the trace interceptor. ProxyFactory pf = new ProxyFactory(); pf.setTarget(facade); pf.addAdvice(new SimpleTraceInterceptor()); pf.setProxyTargetClass(true); ShopFacadeImpl proxy = (ShopFacadeImpl) pf.getProxy(); // Enforce CGLIB proxy that can be cast to the target class!

  14. ProxyFactoryBean <bean id=" petStoreTarget " class="com.myshop.service.ShopFacadeImpl"> <property name="orderDao" ref="orderDao"/> <property name="itemDao" ref="itemDao"/> </bean> <bean id=" traceInterceptor " class="org.springframework.aop.interceptor.SimpleTraceIntercepto r"/> <bean id="petStore" class="org.springframework.aop.framework. ProxyFactoryBean "> <property name="target" ref=" petStoreTarget "/> <property name="interceptorNames" value=" traceInterceptor "> <property name="proxyInterfaces" value="com.myshop.service.ShopFacade"> </bean>

  15. AOP in Spring 2.0 � Simplified configuration � using <aop:*/> tags � Closer AspectJ integration � pointcut expression language � AspectJ-style aspects in Spring AOP � @AspectJ-style aspects in Spring AOP • fully interoperable with ajc compiled aspects � @Configurable � dependency injection on domain objects

  16. Simplified AOP Configuration < aop:config > < aop:advisor id="getAgeAdvisor" pointcut=" execution(* *..TestBean.getAge(..)) " advice-ref=" getAgeCounter "/> </aop:config> <bean id=" getAgeCounter " class="...CountingBeforeAdvice"/>

  17. AspectJ-style Aspects public class JavaBeanPropertyMonitor { private int getterCount = 0; private int setterCount = 0; public void beforeGetter() { this.getterCount++; } public void afterSetter() { this.setterCount++; } }

  18. AspectJ-style Aspects < aop:config > < aop:aspect ref="javaBeanMonitor"> < aop:before pointcut= "execution(public !void get*())" method=" beforeGetter "/> < aop:after pointcut= "execution(public void set*(*))" method=" afterSetter "/> </aop:aspect> </aop:config>

  19. @AspectJ-style Aspects @Aspect public class AjLoggingAspect { @Pointcut("execution(* *..AccountService.*(..))") public void callsToAccountService(){} @Before("callsToAccountService()") public void before(JoinPoint jp) { System.out.println("Before " + jp.toShortString()); } @AfterReturning("callsToAccountService()") public void after() { System.out.println("After."); } }

  20. @AspectJ-style Aspects <!-- detects all @AspectJ aspects among the bean definitions, and applies them to all other beans in the application context --> <aop:aspectj-autoproxy/> <bean id="aspect" class="demo.AjLoggingAspect"/> <bean id="account" class="demo.AccountService"/>

  21. Configuration Aspect for Domain Objects @Configurable("accountBean") public class Account { private AccountService service; public void setAccountService(AccountService service) { this.service = service; } … } // Automatically injected on 'new'! Account acc = new Account(…); <aop:spring-configured/>

  22. Spring 2.0: AOP Unification � Same programming model for proxy-based and weaving-based AOP � choice of implementation strategies � consistent programming model � based on AspectJ � Typically: start with proxy-based AOP � seamlessly works in any kind of ClassLoader environment � Use AspectJ compile-time / load-time weaving for more demanding needs � with same configuration model

Recommend


More recommend