8. AOP support
Prev |  | Next

## 8. AOP support

JavaConfig focuses its AOP support on AspectJ 5 `@Aspect`-style aspects.

## 8.1. @AspectJAutoProxy

Fashioned after Spring XML's `<aop:aspectj-autoproxy>`, `@AspectJAutoProxy` detects any `@Aspect` beans and generates proxies as appropriate to weave the advice methods in those aspects against other beans in the container.

    /**
     * An aspect that logs a message before any property (javabeans setter method) is invoked
     */
    @Aspect
    public class PropertyChangeTracker {
        private Logger logger = Logger.getLogger(PropertyChangeTracker.class);

        @Before("execution(void set*(*))")
        public void trackChange() {
            logger.info("property about to change");
        }
    }

    /**
     * A class with setter methods
     */
    public class SimpleCache implements Cache {
        private int size = 100; // default value;
        private DataSource dataSource;

        public void setCacheSize(int size) {
            this.size = size;
        }

        public void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
        }
    }

    /**
     * A @Configuration class that wires up TransferService and applies
     * the PropertyChangeTracker aspect.
     */
    @Configuration
    @AspectJAutoProxy
    public class Config {

        // declare the aspect itself as a bean
        @Bean
        public PropertyChangeTracker propertyChangeTracker() {
            return new PropertyChangeTracker();
        }

        @Bean
        public Cache cache() {
            return new SimpleCache();
        }
    }

    /**
     * A main method to bootstrap the application
     */
    public class Main {
        public static void main(String[] args) {
            JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(Config.class);
            Cache cache = ctx.getBean(Cache.class);
            // should see "property about to change" message in the log when calling this line..
            cache.setCacheSize(1000);
        }
    }

| Note
The attributes to the `@AspectJAutoProxy` annotation are very similar to the attributes to the `<aop:aspectj-autoproxy>` element. See the related documentation and the JavaDoc for `@AspectJAutoProxy` for details.

### 8.1.1. `@Aspect`-annotated `@Configuration` classes

As a convenience, `@Configuration` classes may themselves be annotated with `@Aspect`, allowing for inline advice methods. Modifying the example above, the `Config` class could be rewritten as follows, and `PropertyChangeTracker` could be eliminated entirely:

    @Aspect
    @Configuration
    @AspectJAutoProxy
    public class Config {
        private Logger logger = Logger.getLogger(Config.class);

        // declare the advice method locally, eliminating the need for PropertyChangeTracker
        @Before("execution(void set*(*))")
        public void trackChange() {
            logger.info("property about to change");
        }

        @Bean
        public Cache cache() {
            return new SimpleCache();
        }
    }

Prev |  | Next
7. Transaction-management support | Home | 9. JMX support