Home > Back-end >  Solving!!!!! AOP custom annotations can be cut into the Controller method, but can't cut the Se
Solving!!!!! AOP custom annotations can be cut into the Controller method, but can't cut the Se

Time:09-27


Do system log management, need to save to the operation of the administrator.
Idea is to use a custom annotations + AOP, put custom annotations in the method of the Controller can be cut, but in the method of ServiceImpl annotations, and not cut.
Daniel said, how to solve?

ServiceImpl method were used in the @ Transactional transaction annotations. Some, and transaction conflict of aop,

Below is LogAspect class

@ Component
@ Aspect
Public class ConsoleSystemLogAspect {

Private static final Logger Logger=LoggerFactory. GetLogger (ConsoleSystemLogAspect. Class);

The @autowired
Private IConsoleSystemLogService consoleSystemLogService;

The @pointcut (" @ the annotation (com.inesv.root.com mon. The annotation. RecordLog) ")
Public void logAspect () {}


@ Before (logAspect "()")
Public void saveSystemLog (JoinPoint point) {
ConsoleSystemLog log=new ConsoleSystemLog ();
//get Request
It request=((ServletRequestAttributes) RequestContextHolder getRequestAttributes ()). GetRequest ();
//set the IP
String ipAddress=request. GetRemoteAddr ();
The setIpAddress (ipAddress);
//set the current logged in user's user name and id
ConsoleUser currentUser=(ConsoleUser) the SecurityUtils. GetSubject () getPrincipal ();
The setOpId (currentUser getId ());
The setOpName (currentUser getUsername ());
//set time
The setOpTime (new Date ());
//set the method
String simpleName=point. GetSignature (.) getDeclaringType () getSimpleName ();
String methodName=point. GetSignature (). The getName ();
The String function=simpleName + ". "+ methodName.
The setFunction (function);
//set the parameter
Object [] args=point. GetArgs ();
The setParams (JSON. ToJSONString (args));
LOGGER. The info (" operation logs: {} ", JSON toJSONString (log));
//save log
ConsoleSystemLogService. Save (log);

}


}


This is a UserServiceImpl

@ Service
Public class ConsoleUserServiceImpl implements IConsoleUserService {

The @autowired
Private ConsoleUserMapper ConsoleUserMapper;
The @autowired
Private ConsoleUserRoleMapper ConsoleUserRoleMapper;

@ Override
@ RecordLog
@ Transactional (rollbackFor={Exception. Class, RuntimeException class}, propagation=propagation. The NESTED)
Public void the delete (id) {
AssertUtils. IsIdNull (id, user id can't be empty, the Context, CODE_ARGS_ERROR);
//logic delete user
ConsoleUserMapper. UpdateDeleteStatus (id);
}


}

CodePudding user response:

The building Lord, my project also use the custom annotations, but is it in the controller, just tried to add in the service method under (also added a @ Transactional), is ok! Logging success is recorded, the original poster is not in the spring configuration file with
 
& lt; Aop: aspectj - autoproxy proxy - target -/& gt;

CodePudding user response:

My project is spring boot, but the general configuration is the same, the following is my main configuration
Annotation class
 
@ Documented
@ Target (ElementType METHOD)
@ Retention (RetentionPolicy RUNTIME)
Public @ interface LogsDescribe {

/* *
* interface name
* @ return
*/
A String value (the default) "";

}


The Aspect
 
@ Aspect
@ Component
Public class LogsDescribeAspect {

Private static final Logger Logger=LoggerFactory. GetLogger (LogsDescribeAspect. Class);

@ the Resource (name="operationLogService")
Private OperationLogService OperationLogService;

The @pointcut (" @ the annotation (com) huangbl) blog. Config. LogsDescribe) ")
Private void the cut () {

}

@ Before (the cut "()")
Public void before (JoinPoint JoinPoint) {
Logger. The info (" LogsDescribe... Before ");
}

@ After (" the cut () ")
Public void after (JoinPoint JoinPoint) {
Logger. The info (" LogsDescribe... After ");
}

}


Service method is the same as you, use annotations in the code I will not send

The last Aspect is automatic proxy, plus in the spring configuration file (in the spring in the boot need only in the service start class with @ EnableAspectJAutoProxy)
 
& lt; Aop: aspectj - autoproxy proxy - target -/& gt;

CodePudding user response:

Possible service layer has been cut a lot of aspects, problems lead to the failure plane order

CodePudding user response:

May be because of the spring and for springmvc is caused by two containers
  • Related