先来 写一个接口
package com.cong.aop;
public interface Target {
    public void save();
}
 
对他进行实现
package com.cong.aop;
import org.springframework.stereotype.Controller;
@Controller
public class TargetImpl implements Target{
    @Override
    public void save() {
        System.out.println("target sunning,,,,,,,,,,");
    }
}
 
写一个增强方法 对目标对象进行增强
package com.cong.aop;
import org.springframework.stereotype.Controller;
@Controller
public class ZengQiang {
    public void before(){
        System.out.println("前置增强,,");
    }
    public void after(){
        System.out.println("后置增强,,");
    }
}
 
配置spring配置文件 进行配置 '
<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解-->
    <context:component-scan base-package="com.cong"/>
<!--//目标对象-->
<!--    <bean id="target" class="com.cong.aop.TargetImpl"/>-->
<!--    //切面对象-->
<!--    <bean id="zengQiang" class="com.cong.aop.ZengQiang"/>-->
<!--    配置织入 告诉spring框架 哪些方法(切点)需要进行那些增强(前置 后置 ..)-->
    <aop:config>
<!--        声明切面 -->
        <aop:aspect ref="zengQiang">
<!--            切面 切点+通知-->
            <aop:before method="before" pointcut="execution(public void com.cong.aop.TargetImpl.save())"></aop:before>
            <aop:after method="after" pointcut="execution(public void com.cong.aop.TargetImpl.save())"></aop:after>
        </aop:aspect>
    </aop:config>
</beans> 
'
测试
import com.cong.aop.Target;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
    @Test
    public void t1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Target target = context.getBean(Target.class);
        target.save();
    }
}
 
结果1

详细解释 :

<!--    配置织入 告诉spring框架 哪些方法(切点)需要进行那些增强(前置 后置 ..)-->
    <aop:config>
<!--        声明切面 -->
        <aop:aspect ref="zengQiang">
<!--            切面 切点+通知-->
            <aop:before method="before" pointcut="execution(* com.cong.aop.*.*(..))"></aop:before>
            <aop:after method="after" pointcut="execution(* com.cong.aop.*.*(..))"></aop:after>
        </aop:aspect>
    </aop:config> 

环绕增强 写一个方法
    public Object account(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前增强");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕后增强");
        return proceed;
    } 
配置
    <aop:config>
<!--        声明切面 -->
        <aop:aspect ref="zengQiang">
<!--            切面 切点+通知-->
<!--            <aop:before method="before" pointcut="execution(* com.cong.aop.*.*(..))"></aop:before>-->
<!--            <aop:after-returning method="after" pointcut="execution(* com.cong.aop.*.*(..))"></aop:after-returning>-->
            <aop:around method="account" pointcut="execution(* com.cong.aop.*.*(..))"/>
        </aop:aspect>
    </aop:config> 
测试

在spring配置文件中开启注解 识别
<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解-->
    <context:component-scan base-package="com.cong"/>
<!--    aop注解 自动代理-->
    <aop:aspectj-autoproxy/>
</beans> 
在增强方法上
@Aspect 证明这是个切面
在增强方法上添加相应的方法注解即可 比如环绕增强 @Around

package com.cong.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Controller;
@Controller
@Aspect
public class ZengQiang {
    public void before(){
        System.out.println("前置增强,,");
    }
    public void after(){
        System.out.println("后置增强,,");
    }
@Around("execution(* com.cong.aop.*.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前增强");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕后增强");
        return proceed;
    }
}
 
测试












