schema-baed方式 (书写多个类,分别实现不同接口并重写方法)
前置类实现 MethodBeforeAdvice 并重写方法
后置类实现 AfterReturningAdvice 并重写方法
环绕实现 MethodInterceptor 参数MethodInvocation作为切点方法 在调用此方法运行前后可以设置环绕前置和环绕后置
异常实现 ThrowsAdvice 并重写方法,报异常就近原则,显示异常信息也是就近原则
public class UserServiceImpl implements UserService {
@Override
public void sayHello() {
System.out.println("sayHello无参方法");
}
@Override
public String showInfo() {
System.out.println("打印结果showInfo方法");
return "showInfo返回结果";
}
@Override
public String sayHello(String name) {
System.out.println("sayHello有参方法"+name);
return "返回结果sayHello有参方法";
}
@Override
public User login(String username, String password) throws Exception {
System.out.println("service自己的本身username"+username+"password的值"+password);
if(username==null){
throw new RuntimeException();
}
return new User();
}
}
<?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"
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"
default-autowire="default">
<bean name="userService" class="com.demo.service.impl.UserServiceImpl"/>
<bean id="beforeMethod" class="com.demo.advice.MyBeforeAdvice"/>
<bean id="afterMethod" class="com.demo.advice.MyAfterMethod"/>
<bean id="aroundMethod" class="com.demo.advice.MyAroundMethod"/>
<aop:config>
<aop:pointcut id="firstPointCut" expression="execution(void com.demo.service.impl.UserServiceImpl.sayHello())"/>
<aop:pointcut id="secondPointCut" expression="execution(java.lang.String com.demo.service.impl.UserServiceImpl.showInfo())"/>
<aop:pointcut id="thirdPointCut" expression="execution(void com.demo.service.impl.UserServiceImpl.sayHello(java.lang.String))"/>
<aop:pointcut id="allPointCut" expression="execution(* com.demo.service.*.*(..))"/>
<aop:advisor advice-ref="afterMethod" pointcut-ref="firstPointCut"/>
<aop:advisor advice-ref="beforeMethod" pointcut-ref="firstPointCut"/>
<aop:advisor advice-ref="aroundMethod" pointcut-ref="firstPointCut"/>
</aop:config>
</beans>
AspectJ方式(一个类书写多个方法,不用实现其他接口)
不传参方式
public class FirstAspectAdvice {
public void before() throws Throwable{
System.out.println("aspect前置");
}
public void afterRunning(Object returnValue) throws Throwable{
System.out.println("后置通知,返回值是"+returnValue);
}
public Object around(ProceedingJoinPoint point) throws Throwable{
Object returnValue = null;
try{
System.out.println("环绕前置");
Object proceed = point.proceed();
System.out.println("环绕后置");
}catch (Exception e){
e.printStackTrace();
System.out.println("环绕异常");
throw e;
}
return returnValue;
}
public void throwing(Exception ex) throws Throwable{
System.out.println("aspect异常");
}
}
<aop:aspect ref="firstAspect">
<aop:pointcut id="all" expression="execution(* com.demo.service.*.*(..))"/>
<aop:before method="before" pointcut-ref="all"/>
<aop:after-returning method="afterRunning" pointcut-ref="all"
returning="returnValue"/>
<aop:around method="around" pointcut-ref="all"/>
<aop:after-throwing method="throwing" pointcut-ref="all" throwing="ex"/>
</aop:aspect>
传参方式
public class SecondAspectAdvice {
public void before(String username,String password){
System.out.println("second前置 username"+username);
}
public void after(Object returnValue,String username,String password){
System.out.println("后置 password"+password);
}
public void around(ProceedingJoinPoint point,String username,String password) throws Throwable {
System.out.println("环绕前置 username"+username);
Object proceed = point.proceed();
System.out.println("环绕后置 password"+password);
}
public void throwing(Exception ex,String username,String password){
System.out.println("异常");
}
}
<bean id="secondAspect" class="com.demo.aspect.SecondAspectAdvice"/>
<aop:config>
<aop:aspect ref="secondAspect">
<aop:pointcut id="all"
expression="execution(com.demo.pojo.User com.demo.service.UserService.login(java.lang.String,java.lang.String)) and args(username,password)"/>
<aop:before method="before" pointcut-ref="all" arg-names="username,password"/>
<aop:after-returning method="after" pointcut-ref="all" arg-names="returnValue,username,password" returning="returnValue"/>
<aop:around method="around" pointcut-ref="all" arg-names="point,username,password"/>
<aop:after-throwing method="throwing" pointcut-ref="all" arg-names="ex,username,password" throwing="ex" />
</aop:aspect>
</aop:config>