Declaring aspect

The concept of declaring Aspect with the @AspectJ annotation style is somewhat similar to XML schema-based AOP declaration. Just to recall, a Java class can be an Aspect in the Spring AOP framework. In annotation-based Spring AOP, you can declare any bean as an Aspect with the @Aspect annotation as follows:

package com.packt.spring.aop.aspects;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class SessionCheck {
}

The SessionCheck class is defined as a regular Spring bean in the application context (XML) file as follows:

<aop:aspectj-autoproxy/>
<bean id="sessionCheckAspect" class="com.packt.spring.aop.aspects.SessionCheck">
</bean>

Aspect classes may have methods and fields like any other Java class. Spring doesn't impose any limits on defining aspects only with the bean defined in the application context (XML) file. If you have used bean autodetection through Java package scanning (with the <context:component-scan> element), Spring intelligently detects the Aspect class with the @Aspect annotation. Aspect classes may contain point-cuts and advice declaration.

The @Aspect annotation itself is not sufficient for Spring to autodetect the class. You still need to use @Component, or any other stereotype annotation. @Aspect will consider the class (which is autodetected by @Component or an equivalent annotation) as an aspect for Spring AOP.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.239.182