package com.howhy;
public interface IUserService {
void addUser();
int delUser();
void updateUser();
}
//UserServiceImpl
package com.howhy;
import org.springframework.stereotype.Component;
@Component("userService")
public class UserServiceImpl implements IUserService {
public void addUser() {
System.out.println(""+1/0);
System.out.println("add user");
}
public int delUser() {
System.out.println("del user");
return 0;
}
public void updateUser() {
System.out.println("update user");
}
}
//Logger class
package com.howhy.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
@Component("logger")
public class Logger {
public void printLog(){
System.out.println("before log");
}
public void afterLog(){
System.out.println("after log");
}
public void exceptLog(){
System.out.println("except log");
}
public void finallyLog(){
System.out.println("finally log");
}
public Object aroundLog(ProceedingJoinPoint pjp){
Object obj=null;
try {
Object[] args=pjp.getArgs();
System.out.println("around before");
obj=pjp.proceed(args);
} catch (Throwable throwable) {
throwable.printStackTrace();
}finally {
}
return obj;
}
}
//aop注解
@Component("logger")
@Aspect
public class Logger {
@Pointcut("execution(* com.howhy.UserServiceImpl.*(..))")
private void pt1(){}
@Before("pt1()")
public void printLog(){
System.out.println("before log");
}
@AfterReturning("pt1()")
public void afterLog(){
System.out.println("after log");
}
@AfterThrowing("pt1()")
public void exceptLog(){
System.out.println("except log");
}
@After("pt1()")
public void finallyLog(){
System.out.println("finally log");
}
public Object aroundLog(ProceedingJoinPoint pjp){
Object obj=null;
try {
Object[] args=pjp.getArgs();
System.out.println("around before");
obj=pjp.proceed(args);
} catch (Throwable throwable) {
throwable.printStackTrace();
}finally {
}
return obj;
}
}
//beans
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.howhy"></context:component-scan>
<aop:config>
<aop:aspect id="logAdiv" ref="logger">
<aop:before method="printLog" pointcut-ref="pt1"></aop:before>
<aop:after-returning method="afterLog" pointcut-ref="pt1"></aop:after-returning>
<aop:after-throwing method="exceptLog" pointcut-ref="pt1"></aop:after-throwing>
<aop:after method="finallyLog" pointcut-ref="pt1"></aop:after>
<aop:pointcut id="pt1" expression="execution(* com.howhy.UserServiceImpl.*(..))"/>
</aop:aspect>
</aop:config>
<!-->开启springmvc aop注解<-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
//run class
package com.howhy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
public static void main(String[] args) {
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("beans.xml");
IUserService accoutService=applicationContext.getBean("userService",IUserService.class);
accoutService.addUser();;
accoutService.delUser();
accoutService.updateUser();
}
}