Spring 2.5 Aspect Oriented Programming (Massimiliano Dessì – $35.99 Amazon)
1. Understanding AOP Concepts
2. Spring AOP Components
3. Spring AOP Proxies
4. AspectJ Support
5. Design with AOP (Downloadable for free!! – http://www.packtpub.com/files/spring-2-5-aspect-oriented-programming-sample-chapter-5-design-with-aop.pdf)
6. Three-tier Spring Application, Domain-Driven Design
7. Three-tier Spring Application, Tests and AOP
8. Develop with AOP Tools
Tag Line: Create dynamic, feature-rich, and robust enterprise applications using the Spring framework.
The best part of this book is it gives you upfront a list of what you require for development and the authors use a stable version of Spring 2.5.6 unlike other books in the market which use the snapshot release. Also, the entire open source development environment is announced before you get into the chapters 🙂
Chapter 1: Understanding AOP Concepts
It starts of with introduction of AOP concepts within the Spring IOC container. There is a clear explanation on the limits of object-oriented programming (Code scattering, code tangling) which can cause the system to have duplicate code and functionality not being clear and how AOP overcomes them by modularizing, decoupling and using aspects. The explanation is very nicely represented with diagrams. I like the fact that it teaches you what you mean by an aspect before getting into the details of how and where they act and the Spring support in programmatic way using annotations and by XML configuration (before advice, after returning advice, around advice, after throwing advice). The simple “small but complete” code examples go with it very well. Most other books I have read just had snippets of code, which was annoying to test. I had to write everything in the chapter to get it to work. I would have liked a better formatting to the code examples though. It is difficult to tell between text if not for the font. It goes on to explain the aim of Spring AOP, which is to allow the realization of JEE functionality in the simplest manner and without being intrusive. There are code samples of Programmatic way to use Spring AOP(MethodBeforeAdvice, AfterReturningAdvice, MethodInterceptor, ThrowsAdvice). The same examples are used in the old Spring way ie. Spring beans declared in XML file and using ProxyFactoryBean.
The same examples are then realized using the syntax of AspectJ annotations to obtain the same advice that we saw in the programmatic examples (@Before, @AfterReturning, @Around, @After, @AfterThrowing). The XML schema based configuration of the examples is also explained.
Chapter 2: Spring AOP Components
The chapter 2 on AOP components does a great job at introducting concepts like Aspect, Pointcut and its components, four pointcuts provided by Spring ie. 1. NameMatchMethodPointcut, 2. RegexpMethodPointcut, 3. StaticMethodMatcherPointcut, 4. DynamicMethodMatcherPointcut. This follows operation on pointcut like union, intersection and Composable Pointcut which can compose two or more pointcuts together. It also touches JoinPoint, Advice (before advice, after returning advice, after throwning advice) and Advisor. It deals with appropriate situations to use these to obtain desired results. I don’t like Console window of Eclipse is used for results every time. They could have potentially used a simple text output. Introductions, which are an important concept in Spring AOP, permit us to dynamically introduce new one-object functionalities and obtain the effects of multiple inheritance in Java (mixin).
Chapter 3: Spring AOP Proxies
Chapter 3 helps understand the concept of Proxies which is the basic concept of Spring AOP (CGLIB-generate the bytecode for the new class on the fly for each proxy, reusing in those cases that were already created AND JDK- implement the interface that manages invocations). How to create proxy programmatically with Classic Proxy(Command Pattern) and AspectJ Proxy (@Aspect). ProxyFactoryBean, which allows us to focus our attention and development on crosscutting, concerns to apply through the proxy, rather than focus on the proxy. How to use Auto proxies with BeanNameAutoProxyCreator, DefaultAdvisorAutoProxyCreator and AbstractAdvisorAutoProxyCreator. In the later part it touches advanced concepts like advised objects and Target sources (Hot swappable target sources, pooling target sources, prototype target sources and threadlocal target sources).
Chapter 4: AspectJ Support
The next chapter deals with AOP with AspectJ way of defining an aspect, pointcut, joinpoint, binding and advice (@Before, @AfterReturning, @AfterThrowing, @After, @Around) and Introduction (@DeclareParents) which makes Spring even more powerful and simple. The focus is on the syntax and pointcuts are applied to complex context bindings or Spring beans. The authors go on to describe how to logically apply the pointcut designators depending on selection of methods’ names, types of argument, selection on types of return, selection on declared exceptions, selection on hierarchy, selection on annotations. How to add arguments to advice methods and bind any argument value, exception, return value or annotation binding. Introductions allow declaring that a target object implements interfaces, providing the implementation. Advisors are useful for joining together the aspect and advice in classic Spring AOP. The most common use is with the advice for transactions . There are beautiful examples to show how to use AOP with the domain object to solve the concurrency of aspects and the mixin of configurations:
Dependency injection in domain objects @Configurable() and AnnotationBeanConfigurerAspect
Advice ordering @Order(value)
Configuration mixin XML or Annotation
Aspect instantiation model AspectJ’s perthis and pertarget instantiation models
To enable the complete AspectJ framework in Spring AspectJ’s load time weaving with Spring is required. This is done through aop.xml in META-INF directory in the classpath root. Spring support for LTW can be as easy as in the config file. AspectJ’s LTW agent can be invoked by adding a VM argument to the command line.
AOP strategy considerations has a good argument about:
· Spring AOP proxy
· Spring with AspectJ Weaver
Chapter 5: Design with AOP
Chapter 5 gets into the design aspect touching hard to understand concepts like concurrency, caching and security (authentication and authorization) which are not well touched in other book available. How Spring can obtain a cleaner and concide source which is easy to maintain and debug is highlighted.
Concurreny is explained with ReadWriteLock and
Transparent caching which helps in terms of speed and resources is explained with an example of ehcache.
Spring sercurity covers AccessDecisionManager (AffirmativeBased, ConsensusBased, UnanimousBased) and AccessDecisionVoter
This is applied to code in the following instances:
· Securing methods with security interceptors
· Securing methods with pointcuts
· Securing methods with annotations
Chapter 6: Three-tier Spring Application, Domain-Driven Design
Three-tier Spring Application, Domain-Driven Design is a case study and explains capabilities and features of DDD. The topics covered are DDD, Domain layer and Infrastructure layer. The roles and responsibilities of objects with DDD are classified into following categories:
· Value Objects
A conceptual architecture is seen from the following standpoints:
· User Interface
· Application Layer
· Domain Layer
· Infrastructure Layer
A sample application (online wholesale fresh-fruit shop) is then used to describe concepts like
Design where interfaces are defined and implemented with @Configurable
Services intrinsically activities or actions, which explain relationship with other objects,
Factories used to create domain objects before the user interaction with application layer. Caching is used here. @Component
Repositories, which deal with persistence and used by domain classes. @Repository. iBATIS is used for persistence. Hibernate would be nicer.
Chapter 7: Three-tier Spring Application, Tests and AOP
Three-tier Spring Application, Tests and AOP is also case study which takes the last chapter further by defining the
Application layer and user interface
Controllers using the Domain Classes are shown which belong to the application layer. These interpret the GUI commands. Annotations like @Controller and @RequestMapping are used
Junit 4.5 with annotations is used testing this sample application. The main class is marked with @RunWith and @SuiteClasses, @BeforeClass (initializing), @AfterClass (tear down) are used in the code. JUnit executes any method annotated with @Test as a test method. Most of the testing code is geared towards testing in Eclipse IDE.
Spring AOP (@Configurable Annotation in conjunction with LTW jvm) and AspectJ aop.xml (LTW) are then added to the code to acheive the following:
· Concurrency (@Order, @Before, @After)
· To measure the method’s average execution time (@ManagedResource, @ManagedAttribute, org.springframework.util.StopWatch, @ManagedOperation)
· All the operations executed on the database must take place in a transactional way (commit or rollback series of transactions). I use this extensively
Chapter 8: Develop with AOP Tool
Extra points to the authors for including the setup of development environment for Spring AOP development on Canonical Ubuntu Linux 8.10, Apple MacOSX 10.5.6, or Windows XP, tools needed and where to download them.
· Java Development Kit
· Eclipse Plugins
· Spring IDE
· AspectJ Development Tool (AJDT)
· Apache Tomcat installation and configuration for
· Ubuntu Linux
· Microsoft Windows
· Ubuntu Linux
· Microsoft Windows
To summarize, I enjoyed reviewing this book. I loved the examples, diagrams and screen shots. Most of the code examples have been shown in both the annotations and XML based configuration for you to choose. Again, I reiterate the fact that the examples and simple and complete. The differences of 1.x and 2.5 version have been highlighted. Surprizingly I found a couple of links on the packtpub site pointing to US stores broken.
Who can use the book:
Any developer or architect who wants to learn and use the power of Spring 2.5.x and AOP (Annotations and XML). It assumes prior practical experience with Spring and configuration.
You can visit http://www.packtpub.com/files/code/4022_Code.zip to directly download the example codes in the book.
Paperback: 332 pages
Publisher: Packt Publishing (February 27, 2009)
Product Dimensions: 9.1 x 7.5 x 0.9 inches
About the Author (As on the site)
Massimiliano Dessì is an experienced Java developer, who started developing JEE applications in 2000. In 2004 he discovered the Spring Framework 1.0, and since then he has been one of its most enthusiastic users. He works as a Software Architect and Engineer for Sourcesense (http://www.sourcesense.com), one of the leading European Open-Source System Integrators. Before joining Sourcesense, he worked as software architect for CRS4 (http://www.crs4.it). He’s also an active technical writer, author of various articles, publications, and reviews available on http://www.jugsardegna.org/vqwiki/jsp/Wiki?MassimilianoDessi and on http://www.slideshare.net/desmax74/slideshows. Massimiliano is also a frequent speaker at Users Groups conferences, including, Java Users Groups, SpringFrameworkUser Group, Javaday, and Linux Users Groups. He is one of the founders of Java User Group Sardinia (https://jugsardegna.dev.java.net/ and http://www.jugsardegna.org), the founder of “Spring Framework Italian User Group”, “Jetspeed Italian User Group” and “Groovy Italian User Group”. He maintains a personal weblog at: http://jroller.com/page/desmax and lives in Cagliari, Sardinia with his family.