aop代码大全

本文详细介绍如何使用Spring AOP实现方法拦截,包括定义接口与实现类、创建切面类及配置XML文件等内容,并演示了如何通过注解方式进一步增强功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

aop实现讲解
1  先定义一个接口 (必须定义)
public interface Performer {
void perform();
}
2 定义一个实现类(类名字不要求)
public class PerformerImp implements Performer {
private int beanBags = 3;
    public void perform()  {
        System.out.println("JUGGLING " + beanBags + " BEANBAGS");
    }
}
3 定义切面类
@Aspect
public class Stage {
  
  //@Pointcut("execution(* *insertPoint())")
  @Pointcut("execution(* *perform())")
  public void insertPoint()//pointcut
  {
 System.out.println("in insert point");
  }
  @Before("insertPoint()")
  public void before1() 
  {
 System.out.println("in insert before1");
  }
  @After("insertPoint()")
  public void after1()
  {
 System.out.println("in insert after1");
  }
}
4 在xml中注册
<?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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">




    <aop:aspectj-autoproxy/>
    <bean id="stage" class="Stage"/>
    <bean id="performer" class="PerformerImp"/>
    </beans>
5 下面就可以在测试
public static void main(String[] args)
  {
 ApplicationContext context = new ClassPathXmlApplicationContext(
              "spring-idol.xml");
 Performer s = (Performer) context.getBean("performer");
      s.perform();
  }


-----------------------------------------------------------------------------------
execution(* com.xyz.service.AccountService.*(..))
execution(* com.xyz.service.AccountService.*(..)and within(com.xyz.service.service2.*) and bean(id))


<bean id="audience" class="com.springinaction..."/>
<aop:config>
  <aop:aspect ref="audience">   <!-- 切面-->
     <aop:before pointcut="execution(* com.xyz.service.AccountService.*(..))" method="takeSeak" />
  </aop:aspect>
</aop:config>
<!-- 环绕式切面 方法定义-->
public void method(ProceedingJoinPoint jointPoint)
{
    try{
             ......
             jointPoint.proceed();
             ........
    }cache(Throwable e)
    {    }
}
<aop:config>
  <aop:aspect ref="audience">   <!-- 切面-->
     <aop:pointcut id="id1"  expression="execution(* com.xyz.service.AccountService.*())" />
     <aop:around pointcut-ref="id1" method="m1()"/>
      <aop:pointcut id="id2"  expression="execution(* com.xyz.service.AccountService.*(String)) and args(s1)" />
      <aop:before pointcut-ref="id2" method="m1" arg-names="s1"/>
  </aop:aspect>
</aop:config>
-----------------引入新功能----------------------------------
1  定义新的接口 
public interface Poem {
void recite();
}
2  定义新的接口实现
public class Sonnet29 implements Poem {
private String [] LINES={
"HELLO1",
"HELLO2"
};
public Sonnet29(){}
public void recite(){
for(int i=0;i<LINES.length;i++){
System.out.println(LINES[i]);
}
}
}
3 xml注册
<aop:aspect>
  <aop:decare-parents
        types-matching="...baseclass"
        implement-interface="...interface"
        default-impl="...impl" />
</aop:aspect>
4 测试
public static void main(String[] args)
  {
 ApplicationContext context = new ClassPathXmlApplicationContext(
              "spring-idol.xml");
 Poem s = (Poem) context.getBean("performer");
      s.recite();
  }
  
<aop:aspect>
  <aop:decare-parents
        types-matching="...baseclass"
        implement-interface="...interface"
        delegate-ref="id1" />
</aop:aspect>
-------------注解------------------
@Aspect
public class Audience {
  
  @Pointcut("execution(* com.xyz.service.AccountService.perform())")
  public void insertPoint()//pointcut
  {
 
  }
  @Before("insertPoint()")
  public void before1()
  {
 ...
  }
@Around("insertPoint()")
  public void before1(ProceedingJoinPoint point)
  {
 ...
 point.proceed();
 ...
  }
}
<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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <aop:aspectj-autoproxy/>
    </beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值