aop good idea can help us achieve decoupling of code, such as we mentioned before, the logging code and the code completely separate business layer, through the integration of the proxy class spring aop. In the aspect class, we can through the interface spring provided good access to relevant information of the original entry point.
First of all, we proceed from the code
Business layer code that StudentServiceImpl
@Service("studentService")
public class StudentServiceImpl implements StudentService {
@Override
public int addStudent(Student student) throws Exception {
System.out.println("addStudent...业务层代码执行...");
return 1;
}
@Override
public int deleteStudent(Integer id) throws Exception{
System.out.println("deleteStudent...业务层代码执行...");
int i = 1/0;
return 0;
}
}
Cut class StudentServiceLogger
@Aspect
@Component
public class StudentServiceLogger {
@Before("execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )")
public void doBefore(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法执行前...");
System.out.println("参数为:"+ Arrays.asList(args));
}
@After("execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )")
public void doAfter(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法执行后...");
}
@AfterReturning(value = "execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )" , returning = "returning")
public void doReturn(JoinPoint joinPoint,Object returning){
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法返回,返回值为:"+returning);
}
@AfterThrowing(value = "execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )",throwing = "ex")
public void doThrow(JoinPoint joinPoint,Exception ex){
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法异常,异常信息为:"+ex.getMessage());
}
}
Test class AopTest
public class AopTest {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
@Test
public void test1() throws Exception {
StudentService studentService = ac.getBean("studentService",StudentService.class);
studentService.addStudent(new Student());
System.out.println("==================割===============");
studentService.deleteStudent(1);
}
}
Finally, log output
addStudent方法执行前...
参数为:[com.wuwl.domain.Student@7e5c856f]
addStudent...业务层代码执行...
addStudent方法执行后...
addStudent方法返回,返回值为:1
==================割===============
deleteStudent方法执行前...
参数为:[1]
deleteStudent...业务层代码执行...
deleteStudent方法执行后...
deleteStudent方法异常,异常信息为:/ by zero
Relevant information about the entry point method, spring well encapsulated in the org.aspectj.lang.JoinPoint
interface, to note here is that in org.aopalliance.intercept
the next package, also has an interface so named, do not be mistaken.
joinPoint.getArgs()
The method of cutting the parameter information can return method, the return value is an array, to obtain the traversal; joinPoint.getSignature()
return value of the method signature method, the signature method information including the interface name and the like.
If you need to obtain in the cutting method of notifying method return value or exception information, it is necessary to use the corresponding property annotation to the corresponding job. @AfterReturning opening the annotation can be seen that a String returning() default "";
property, add return value parameter, then the parameter is declared annotation cut to the return value, refer to the above method of operation code notification process. Similarly you can use @AfterThrowing annotation String throwing() default "";
attributes, you can get to the anomalies cut method throws.