The @aspect annotation will indicate that it is an Aspect. The @Component annotation will indicate that it is a Spring Component

Slicing aspects, since Spring supports AOP, will definitely work. Some people will ask how to take the original HTTP request and response information, with the following code should be taken ahhahaha RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest();

<aop:config> <! -- order: Specifies the priority of the slice bean, the smaller the value, < AOP :aspect id="fourAdviceAspect" ref="fourAdviceBean" order="2"> < AOP :around method="processTask" pointcut="execution(* com.wangjun.aop.xml.*.*(..) )"/> <aop:before method="authority" pointcut="execution(* com.wangjun.aop.xml.*.*(..) )"/> <aop:after-returning method="log" returning="rvt" pointcut="execution(* com.wangjun.aop.xml.*.*(..) )"/> </aop:aspect> </aop:config>Copy the code

Aop :aspect/ aop:around/ aop:before/ The pointCut attribute defines the pointcut, which is where the enhancement takes place. An expression like execution(* com.wangjun.aop.xml.. (..) ) The meanings are as follows:

Specify any class method in the com.wangjun.aop.xml package; The first one indicates that the return value is unlimited, and the second one indicates that the class name is unlimited. The third * indicates the method name is unlimited, and (..) in parentheses. Represents any number of parameters of unlimited type.

Usage scenarios Logging, auditing, declarative transactions, security, and caching.

The difference between AspectJ and Spring AOP just represents two ways to implement AOP: AspectJ implements AOP statically, that is, at compile time, the program is modified, requiring a special compiler, with better performance; Spring AOP implements AOP dynamically, that is, dynamically generates AN AOP proxy at run time and is implemented in pure Java, so no special compiler is required, but performance is generally poor.

Case: Log

Service log table

package chin.common; Public enum OpearteType {QUERY(" QUERY","QUERY"), CREATE(" QUERY","CREATE"), UPDATE(" maintain ","UPDATE"), DELETE(" DELETE","DELETE"), REVIEW(" REVIEW","REVIEW"),; private String name; private String code; private OpearteType(String name, String code) { this.name = name; this.code = code; } public static String getName(String code) { for (OpearteType c : OpearteType.values()) { if (c.getCode().equals(code)) { return c.name; } } return null; } public static String getCode(String name) { for (OpearteType c : OpearteType.values()) { if (c.getName().equals(name)) { return c.code; } } return null; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; }}Copy the code
package chin.annotation; import chin.common.BusinessModule; import java.lang.annotation.*; / / @documented @Retention(RetentionPolicy.runtime) @target (Value = elementType.type) public @interface BusinessModuleAttributes { BusinessModule businessModule(); }Copy the code
package chin.annotation; import chin.common.OpearteType; import java.lang.annotation.*; @documented @Retention(RetentionPolicy.runtime) @target (Value = elementType.method) public @interface BusinessLogAttributes { OpearteType opearteType(); String opearteDescription(); }Copy the code
package chin.aspect; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.beyondbit.ias.core.base.BaseController; import com.beyondbit.ias.core.util.IPUtil; import chin.annotation.BusinessLogAttributes; import chin.annotation.BusinessModuleAttributes; import chin.entity.BusinessLog; import chin.service.BusinessLogService; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.ui.Model; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Method; import java.util.Date; /** * @component public class BusinessLogAspect extends BaseController {@autoWired private BusinessLogService businessLogService; @Pointcut("@annotation(chin.annotation.BusinessLogAttributes)") public void logPointCut() { } @Around("logPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); BusinessModuleAttributes moduleAttributes = point.getTarget().getClass().getAnnotation(BusinessModuleAttributes.class); MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); BusinessLogAttributes logAttributes = method.getAnnotation(BusinessLogAttributes.class); MethodSignature ms = (MethodSignature) point.getsignature (); String[] parameterNames = ms.getparameterNames (); Object[] parameterValues = point.getargs (); StringBuilder strParams = new StringBuilder(); If (parameterNames! = null && parameterNames.length > 0) { for (int i = 0; i < parameterNames.length; i++) { if (parameterNames[i].equals("bindingResult")) { break; } if ((parameterValues[i] instanceof HttpServletRequest) || (parameterValues[i] instanceof HttpServletResponse)||(parameterValues[i] instanceof Model)||(parameterValues[i] instanceof MultipartFile)) { strParams.  append("["). append(parameterNames[i]).append("=").append(parameterValues[i]) .append("]"); } else { try { strParams. append("["). append(parameterNames[i]).append("=") .append(JSON.toJSONString(parameterValues[i], SerializerFeature.WriteDateUseDateFormat)) .append("]"); } catch(Throwable throwable){ strParams. append("["). append(parameterNames[i]).append("=").append(parameterValues[i]) .append("]"); } } } } Object result = null; BusinessLog log = new BusinessLog(); log.setUuid(java.util.UUID.randomUUID().toString()); log.setClientIP(IPUtil.getClientIp(request)); log.setBusinessModule(moduleAttributes.businessModule().getCode()); log.setOpearteType(logAttributes.opearteType().getCode()); log.setOpearteDescription(logAttributes.opearteDescription()); log.setOpeartor(super.getCurrentUser().getUserUid()); log.setOpeartorName(super.getCurrentUser().getName()); log.setOpearteDateTime(new Date()); log.setReqUrl(request.getRequestURL().toString()); log.setClazz(point.getTarget().getClass().getName()); log.setMethod(method.getName()); log.setParams(strParams.toString()); Proceed (); // proceed(); log.setStatus("1"); } catch (Exception e) { log.setStatus("0"); log.setException(e.getStackTrace().toString()); throw e; } finally { businessLogService.insertBusinessLog(log); } return result; }}Copy the code