Implement AOP in Spring in @ AspectJ Mode

Source: Internet
Author: User

1. spring AOP introduces Spring to provide many methods for Implementing AOP, including Spring interface, schema configuration, and annotation, starting from Spring 2.0, you can use schema-based and @ AspectJ to implement AOP. This article describes how to implement AOP in Spring using @ AspectJ as a simple example. Because @ Aspect is based on annotation, JDK 5.0 or later versions of annotation must be supported. 2. To use Spring AOP in a project, import the Spring jar package, aspectjweaver. jar, aspectjrt. jar, and cglib. jar in the project. In Spring MVC, you only need to add aspectjweaver. jar and cglib. jar can be [html] <groupId> org. aspectj </groupId> <artifactId> aspectjweaver </artifactId> <version> 1.7.1 </version> </dependency> <groupId> cglib </groupId> <artifactId> cglib </artifactId> <version> 2.2.2 </version> </dependency>, preparations are complete. 3. this instance uses AOP to enhance the execution method. That is, we execute a written method before and after the execution target method to filter before execution, log recording after execution. target method code [java] @ Controller public class AccountApiController {@ ResponseBody @ RequestMapping (value = "/openapi/v1/account/kp") public String kp (HttpServletRequest request) {System. out. println ("kp: 123"); return "kp: 123" ;}} B. enhanced @ AspectJ code [java] @ Aspect public class ApiAspect {private static final Logger logger = LoggerFactor Y. getLogger ("com. xxx. log "); // match the class @ Around (" within (com. xxx. api. * Controller) ") public String arountAction (ProceedingJoinPoint pjp) {// API request parameter check, HttpServletRequest request = (HttpServletRequest) pjp. getArgs () [0]; try {// TODO check} catch (Exception e) {// TODO log & return} String result = null; try {// execution target method result = (String) pjp. proceed (); // TODO log} catch (Throwable e) {// TODO log & return} return result;} c. spring xml configuration [html] <aop: aspectj-autoproxy/> <beans: bean class = "com. xxx. api. apiAspect "/> <context: component-scan base-package =" com. xxx. api. * "/> in this way, the required functions are implemented. The advantage of AOP is that the pre-and post-processing can be implemented without making any changes to the target method code, and @ Before can also be used in B, @ After and other annotations to implement different functions. Next we will introduce the detailed usage of @ AspectJ 4. @ AspectJ detailed usage currently, only the execution method is the connection point in Spring AOP. The AspectJ Entry Point Indicators supported by Spring AOP are as follows: execution (public **( ..) When any public method is executed, the start point function is executed. Execution (* set * (..) when any method starting with "set" is executed, the start point function is executed. Execution (* com. demo. service. AccountService. * (..) when any method in the AccountService interface is executed, the start point function is executed. Execution (* com. demo. service. *. * (..) when any method in the service package is executed, the start point function is executed. Any connection point of within (com. demo. service. *) in the service package. Any connection points of within (com. demo. service... *) in the service package or sub-package. This (com. demo. service. AccountService) implements any connection point for the proxy object of the AccountService interface. Target (com. demo. service. AccountService) achieves any connection point of the target object of the AccountService interface. Args (java. io. serializable) any one accepts only one parameter, and the input parameter at run time enhances the connection point of the Serializable interface: @ Before: Run @ AfterReturning Before the method: run @ AfterThrowing: Throw and then run @ After: No matter how the method ends, it will be executed (similar to finally) @ Around: surround execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.