spring中得AOP

      spring中得Ioc相对比较简单,他和iphone中得协议很类似。通过BeanFactory或者是ApplicationContext来动态获得相关的类,并且调用其方法,如:

   BeanFactory factory = new XmlBeanFactory(input); 

   EBean eb = (EBean)factory.getBean("eBean"); 

   思想和实现相对简单。

   AOP(Aspect-Oriented Programming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其名为“Aspect”,即方面。

   假设有很多个不同的类,他们不是仅从接口IHello派生的,而我们希望非常有条例的加入这些类(或更直接的接口)中一些通知,比如在某方法执行前,某方法执行后的一些通知。

package com.my.test;
public interface IHello {
public void hello(String name);
}

-------------------------------------------

package com.my.test;
import com.my.test.IHello;
public class HelloSpeaker implements IHello {
public void hello(String name) {
System.out.println("Hello,"+name);
}
}

-------------------以上是原始没有Spring介入时的定义,下边Spring介入-------------------------

那么在2.0以后,就有两种实现的方式:

(1)基于XML Schema的设置:

beans-config.xml加入XML控制

 <aop:config>  <!--映射对应com.my.test.IHello的所有方法-->
       <aop:pointcut id="logHello" expression="execution(* com.my.test.IHello.*(..))"/> 
       <aop:aspect id="logging" ref="logAspect">    <!--映射对应logAspect类-->
             <aop:before pointcut-ref="logHello" method="before"/>  <!--代表开始通知-->
             <aop:after pointcut-ref="logHello" method="after"/>  <!--代表之后通知-->
             <aop:around pointcut-ref="logHello" method="invoke"/> <!--代表调用时通知-->         
       </aop:aspect>
    </aop:config>

增加logAspect类:

public class LogAspect{
    
    private Logger logger=Logger.getLogger(this.getClass().getName());


    public void before(JoinPoint jointPoint) {
        logger.log(Level.INFO, "method starts..."
                + jointPoint.getSignature().getDeclaringTypeName() + "."
                + jointPoint.getSignature().getName());
    }
    
    public void after(JoinPoint jointPoint){
        logger.log(Level.INFO, "method ends..."
                + jointPoint.getSignature().getDeclaringTypeName() + "."
                + jointPoint.getSignature().getName());
    }


    public Object invoke(ProceedingJoinPoint jointPoint) throws Throwable{
        
        logger.log(Level.INFO, "method starts..."
                + jointPoint.getSignature().getDeclaringTypeName() + "."
                + jointPoint.getSignature().getName());
        
        Object retVal=jointPoint.proceed();
        
        logger.log(Level.INFO, "method ends..."
                + jointPoint.getSignature().getDeclaringTypeName() + "."
                + jointPoint.getSignature().getName());
     
        return retVal;
    }
 
}

(2)基于annotation的支持:

这个方法的好处是不需要配置XML,缺点是无法集中管理。

在XML中定义:

<bean id="logBeforeAdvice" class="com.my.test.LogBeforeAdvice"/>
<bean id="logAfterAdvice" class="com.my.test.AfterReturningAdvice" />

在类中定义标签:

@Aspect
public class LogBeforeAdvice {
@Before("execution(* com.my.test.IHello.*(..))")
public void before(JoinPoint joinPoint) {
System.out.println("Logging before "
+ joinPoint.getSignature().getName());
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值