Spring AOP

Spring Framework (24 Part Series)

1 Spring Configuration
2 External Properties in Spring
20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types

Aspect-Oriented Programming (AOP) enables the modularization of generic functionalities that are needed in many places (join points) like logging, security, caching, error handling, etc.

Spring AOP uses dynamic proxies (enhanced classes that stand in place of the originals) for aspect weaving (combining aspects with the code). An aspect is a java class that contains pointcuts (expressions that select one or more join points) and advice (code to be executed at each selected join point).

For example, let’s implement an aspect for logging. First, we need to enable the use of the @Aspect annotation by adding the @EnableAspectJAutoProxy annotation in the @Configuration (see Spring Configuration) class (with its respective @ComponentScan to the packages with the aspects). Now we can create the @Aspect:

@Aspect
@Component
public class LoggingAspect {

    private Logger logger = Logger.getLogger(getClass());

    @Before("execution(public * package_name.*(..))")
    public void log() {
          logger.info("Logging from aspect");
    }
}

Enter fullscreen mode Exit fullscreen mode

We can receive the JoinPoint (or its subinterface ProceedingJoinPoint) as a parameter in the methods and to get some properties (see JoinPoint interface). And not only to use the @Before annotation over the methods, but to use other annotations from the org.aspectj.lang.annotation package as @Around, @After, @AfterReturning, and @AfterThrowing, followed by the context about the intercepted point using AspectJ’s pointcut expression language (wildcard asterisk matches once, two points match zero or more) for selecting where to apply the advice (see AspectJ Documentation).

Take into account:

  • If a @Before advice throws an exception, the target will not be called.
  • You can only advise non-private methods.
  • You can only apply aspects to Spring Beans.

Spring Framework (24 Part Series)

1 Spring Configuration
2 External Properties in Spring
20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types

原文链接:Spring AOP

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容