后端-框架-Spring-AOP-注解注入
如果碰到代码无误,可能是JDK与aspectjweaver版本不匹配
pointcut位置
package cn.service;
import cn.service.UserService;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import cn.dao.UserDao;
import cn.entity.User;
@Service("UserService")
public class UserServiceImpl implements UserService {
private UserDao dao;
@Resource(name="UserDao")
public void setDao(UserDao dao) {
this.dao = dao;
}
public UserServiceImpl(UserDao dao) {
this.dao=dao;
}
public void addNewUser(User user) {
dao.save(user);
}
}
日志类
package cn.aop;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class UserServiceLogger {
public static Logger logger = Logger.getLogger(UserServiceLogger.class);
@Pointcut("execution(* cn.service.UserService.*(..))")
public void pointcut(){}
@Before("pointcut()")
public void before(JoinPoint jp){
logger.info("调用对象:"+jp.getTarget()+"方法:"+jp.getSignature()+"方法参数"+Arrays.toString(jp.getArgs()));
}
@AfterReturning(pointcut="execution(* cn.service.UserService.*(..))",returning="result")
public void afterReturn(JoinPoint jp,Object result){
logger.info("调用对象:"+jp.getTarget()+"方法:"+jp.getSignature()+"方法返回值"+result);
}
@AfterThrowing(pointcut="execution(* cn.service.UserServiceImpl.*(..))",throwing="e")
public void afterThrow(JoinPoint jp,Exception e){
logger.error(jp.getSignature().getName()+"捕捉到异常"+e);
}
@After("pointcut()")
public void after(JoinPoint jp){
logger.info(jp.getSignature().getName()+"最终增强");
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint jp) throws Throwable{
logger.info("调用对象:"+jp.getTarget()+"方法:"+jp.getSignature()+"方法参数"+Arrays.toString(jp.getArgs()));
try {
Object result = jp.proceed();
logger.info("调用对象:"+jp.getTarget()+"方法:"+jp.getSignature()+"方法返回值"+result);
return result;
} catch (Throwable e) {
logger.error(jp.getSignature().getName()+"捕捉到异常"+e);
throw e;
} finally{
logger.info(jp.getSignature().getName()+"方法结束");
}
}
}
准备的ApplicationContext.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-4.3.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-4.3.xsd"
>
<context:component-scan base-package="cn.dao,cn.service"/>
<bean id="UserServiceLogger" class="cn.aop.UserServiceLogger"></bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>