Implementing ObscenityFilter
For the SpringBlog application, we decided to create an implementation of ObscenityFilter that
allows the list of obscenities to filter to be specified as a List and that replaces the obscenities using
the ROT13 algorithm. With the implementation of the ObscenityFilter interface finished (the
ListBasedObscenityFilter class), an advice (the ObscenityFilterAdvice) was then created, which allows
obscenity filter capabilities to be applied declaratively to any method that accepts Entry or Comment
arguments. The basis of the ObscenityFilterAdvice is to modify the arguments passed to the method so
that any String properties of the Entry or Comment objects are replaced with their obfuscated alternatives.
Because we only need to look at the arguments passed to a method and perhaps modify them, a before
advice is ideal for this. In addition, we decided to use the @AspectJ-style annotations to implement
the advice.
Obscenity Filter Summary
As you can see from the example in this section, AOP has plenty of practical uses in a real application. We
found that by using AOP for the obscenity filter, we were able to keep the code for the service layer of the
SpringBlog application much cleaner and were also able to reduce the amount of code duplication within
the application. When you build your own applications with Spring, it is worth it to take the time to
identify crosscutting logic. Once you have done this, define the interfaces to interact with it, build the
implementations, and then instead of using a factory and embedding calls against your interfaces
throughout your code, use Spring AOP to weave the logic into your application transparently. For a
detailed explanation on the implementation of the obscenity filter and its source code, please refer to the
section "Obscenity Filter Using AOP" in Chapter 21.
Summary
In this chapter, we concluded our discussion on AOP. We looked at the advanced options for
pointcutting, as well as how to extend the set of interfaces implemented by an object using
introductions. A large part of this chapter focused on using Spring framework services to configure AOP
declaratively, thus avoiding the need to hard-code AOP proxy construction logic into your code. We
spent some time looking at how Spring and AspectJ are integrated to allow you to use the added power
of AspectJ without losing any of the flexibility of Spring. In the next chapter, we move on to a completely
different topic--how we can use Spring's JDBC support to radically simplify the creation of JDBC-based
data access code.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home