@annotation() | Restrict matching of join points with specified annotations |
The final structure of each class is as follows:
1,Factory
package com.test.aspectj.expression; /** * Factory interface */ public interface Factory { // Make products void make(); // transport void delivery(String address); }
2,PhoneFactory
package com.test.aspectj.expression; import com.test.aspectj.expression.annotation.Log; import org.springframework.stereotype.Component; /** * Mobile phone factory */ @Component public class PhoneFactory implements Factory { // Note @ Log annotation for the method of making products @Override @Log public void make() { System.out.println("From target class PhoneFactory Message: making mobile phones"); } // How to transport mobile phones @Override public void delivery(String address) { System.out.println("From target class PhoneFactory Message for: Transport mobile phone to " + address); } }
3,FoodFactory
package com.test.aspectj.expression; import com.test.aspectj.expression.args.FreshFoodFactory; import com.test.aspectj.expression.args.FrozenFoodFactory; import org.springframework.stereotype.Component; /** * Food factory */ @Component public class FoodFactory implements Factory { // How to make products @Override public void make() { System.out.println("From target class FoodFactory News of: food production"); } // transport @Override public void delivery(String address) { System.out.println("From target class FoodFactory Sales of food to " + address); } }
4. Custom annotation Log
package com.test.aspectj.expression.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Custom log annotation */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Log { boolean value() default true; }
5. Annotation aspect, @annotation Code example
package com.test.aspectj.expression.annotation; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; /** * Use @ annotation() to weave enhancements for all methods annotated with @ Log */ @Aspect public class AnnotationAspect { @AfterReturning("@annotation(com.test.aspectj.expression.annotation.Log)") public void log() { System.out.println("Message from aspect: print log"); } }
6,xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.test.aspectj.expression"/> <bean id="annotationAspect" class="com.test.aspectj.expression.annotation.AnnotationAspect"/> <aop:aspectj-autoproxy/> </beans>
7. Test code
package com.test.aspectj.expression.annotation; import com.test.aspectj.expression.Factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Test execution aspect aspect */ public class AspectJExpressionDemo { public static void main(String[] args) { System.out.println("=============== test @annotation ==============="); ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-aspectjannotationexpression.xml"); Factory foodFactory = (Factory) context.getBean("foodFactory"); // There is no @ Log annotation on the foodfactory ා make method, so it will not be enhanced System.out.println("FoodFactory#There is no @ Log annotation on the make method, so it will not be enhanced '); foodFactory.make(); System.out.println("-----Dividing line-----"); Factory phoneFactory = (Factory) context.getBean("phoneFactory"); // The @ Log annotation on the phonefactory ා make method will be enhanced System.out.println("PhoneFactory#The @ Log annotation on the make method will be enhanced '); phoneFactory.make(); } }
8. Operation results
===============Test @ annotation=============== There is no @ Log annotation on the foodfactory ා make method, so it will not be enhanced Message from the target class FoodFactory: produce food -----Dividing line----- The @ Log annotation on the phonefactory ා make method will be enhanced Message from target class PhoneFactory: production phone Message from aspect: print log
11. Conclusion
- There is no @ Log annotation on the foodfactory ා make method, so it will not be enhanced
- The @ Log annotation on the phonefactory ා make method will be enhanced