一、.默认advisor自动代理生成器(实现前置增强)
1.编写接口类
public interface ISomeService {
    public void doSome();
    public void add();
}
2.实现接口类中的方法
public class SomeService implements ISomeService {
    public void doSome() {
        System.out.println("code  ok");
    }
    public void add() {
        System.out.println("=========log===============");
    }
}
3.编写增强类
public class MyBeforeAdvice implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("执行前置增强");
    }
}
4.写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
    <!--目标对象-->
    <bean id="SomeService" class="cn.happy.service.SomeService"></bean>
    <!--增强  通知-->
    <bean  id="beforeAdvice" class="cn.happy.service.MyBeforeAdvice"></bean><!--前置增强-->
    
      <!-- 使用名称匹配方法切入点顾问NameMatchMethodPointcutAdvisor-->
 <bean  id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
       <!-- <!–顾问 Advisor 包装通知, 那个方法需要增强–>-->
        <property name="advice" ref="beforeAdvice"></property>
        <property name="mappedNames" value="doSome"></property>
    </bean>
    <!--默认advisor自动代理生成器  有几个目标方法都会增强,但是只能对顾问自动代理,不能自动代理通知-->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>
5.测试方法
public class test01 {
    // AspectJ 
    @Test
    public void testAspect(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ISomeService someService= (ISomeService)context.getBean("SomeService");
        someService.add();
        someService.doSome();
    }
6.测试结果

二、Aspectj实现前置增强
AspectJ:是一个面向切面的框架,它扩展了Java语言。
AspectJ表达式:
    语法:execution(表达式) 通过execution函数,可以定义切点的方法切入
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
例如
execution(public * *(..)) 匹配所有类public方法
execution(* cn.study.dao..*(..)) 匹配指定包下所有类方法,不包含子包
execution(* cn.study.dao..*(..)) ..*表示包、子孙包下所有类
execution(* cn.study.service.UserService.*(..)) 匹配指定类所有方法
execution(* cn.study.dao.GenericDAO+.*(..)) 匹配实现特定接口所有类方法
execution(* save*(..)) 匹配所有save开头的方法
  AspectJ增强
    @Before 前置通知,相当于BeforeAdvice
    @AfterReturning 后置通知,相当于AfterReturningAdvice
    @Around 环绕通知,相当于MethodInterceptor
    @AfterThrowing抛出通知,相当于ThrowAdvice
    @After 最终final通知,不管是否异常,该通知都会执行
1.基本的方法不变,只改一下xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
    <!--目标对象-->
    <bean id="SomeService" class="cn.happy.serviceXML.SomeService"></bean>
  <!--  <!–增强  通知–>  注解
    <bean id="beforeAdvice" class="cn.happy.service.MyAspect"></bean>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>-->
    <!--增强类-->
    <bean id="aspect" class="cn.happy.serviceXML.MyAspect"></bean>
    <!--aop-->
    <aop:config>
        <!--设置一个切点-->
        <aop:pointcut id="mycut" expression="execution(* *..serviceXML.*.*(..))"></aop:pointcut>
       <aop:aspect ref="aspect">
           <aop:before method="myBefore" pointcut-ref="mycut"></aop:before>
           <!--<aop:after-returning method="after" pointcut-ref="mycut"></aop:after-returning>-->
           <!--<aop:around method="myArround" pointcut-ref="mycut"></aop:around>-->
       </aop:aspect>
    </aop:config>
</beans>
2.运行后结果
 
原文:http://www.cnblogs.com/cn-930621/p/7657715.html