13.07.2015 Views

Spring JavaConfig Reference Guide - Spring Web Services - Parent ...

Spring JavaConfig Reference Guide - Spring Web Services - Parent ...

Spring JavaConfig Reference Guide - Spring Web Services - Parent ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6. Using aspectsAOP support in <strong>JavaConfig</strong> is a work in progress. It is documented here in order to solicit feedback. Expectchanges in forthcoming milestone releases6.1. Embedded aspectsTipFor those unfamiliar with AOP and/or <strong>Spring</strong> AOP, you'll want to take a look Chapter 6 of the core<strong>Spring</strong> reference documentation, Aspect-oriented Programming with <strong>Spring</strong>.A configuration class can serve 'double duty' as an aspect. By applying @Aspect to a @Configuration class,you can then add pointcuts and advice that will be applied against all beans in the container.@Aspect@Configurationpublic class AppConfig {@Beanpublic Service service() {return new ServiceImpl(...);}}@Before("execution(* service..Service+.set*(*))")public void trackServicePropertyChange() {logger.info("property changed on service!");}This pointcut will match the all methods starting with 'set' on all implementations of the Service interface.Before any matching methods execute, the trackServicePropertyChange() method will be executed.6.2. Reusable aspectsTo create a reusable aspect, define a class annotated with @Configuration and Aspect containing advicemethods and pointcuts.@Aspect@Configurationpublic class PropertyChangeTracker {private Logger logger = Logger.getLogger(PropertyChangeTracker.class);}@Before("execution(* set*(*))")public void trackChange() {logger.info("property just changed...");}Then include that aspect in the application context, either using the @Import annotation or by adding at as aconstructor argument when instantiating <strong>JavaConfig</strong>ApplicationContext. Examples of both approaches arebelow.@Configuration@Import(PropertyChangeTracker.class)@Aspect // necessary in order to have transferServicepublic class MyConfig {@Beanpublic TransferService myService() {return new TransferServiceImpl(...);bean get aspects applied!<strong>Spring</strong> <strong>JavaConfig</strong> 22

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!