spring中Aop的理解和例子

配置文件bean
<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    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-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <bean id="log" class="aop.Log">
    </bean>
    <!-- 在aop:config中配置各种通知 -->
    <aop:config>
        <aop:aspect ref="log">
            <!-- 声明切入点 ,方法名,第二个*表示方法名,小括号里面的俩个小..代表方法的参数-->
            <aop:pointcut  expression="execution( * aop.*.*(..))"
                id="operate"  />
            <!-- 各个通知类型 -->
            <aop:before method="before" pointcut-ref="operate" />
            <aop:around method="around" pointcut-ref="operate"/>
            <aop:after method="after" pointcut-ref="operate" />
            <!-- 异常通知没有效果哦,不知道什么情况....待解决 -->
             <!-- <aop:after-throwing method="error" pointcut="execution( * aop.Operate.*(..))"
                 /> -->

            <aop:after-returning method="success" pointcut-ref="operate" />

        </aop:aspect>
    </aop:config>

    <bean id="ope" class="aop.Operate"></bean>
    <bean id="aop" class="aop.Aop"></bean>


</beans>
这个配置文件没有使用注解,标签<aop:config></aop:config>是面向切面的开始,
一下是方法中的切面和切点
====切面
package aop;
/***
 *
 * @author wudi
 * 定义切面
 */
public class Operate {
    public void parse(){
    String string="12345";
    try {
        int s=Integer.parseInt(string);
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        System.out.println("出错的原因"+e);
    }
    }
}
====切面
package aop;
/***
 * 切面
 * @author wudi
 *
 */
public class Aop {
    
    public void show (){
         String string="12345";
            try {
                int s=Integer.parseInt(string);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                System.out.println("出错的原因"+e);
            }
    }

}
=====切点
package aop;
/***
 *
 * @author wudi
 * 定义切点
 */
public class Log {
    //前置通知
    public void before(){
        System.out.println("前置通知");
    }
    //环绕通知
    public void around(){
        System.out.println("环绕通知");
    }
    //后置通知
    public void after(){
         System.out.println("后置通知");
    }
    //成功通知
    public void success(){
        System.out.println("登陆成功");
        
    }
    //失败后通知
    public void error(Exception e){
        System.out.println("登陆失败 "+e.getMessage());
    }
}

=====测试的切点
package aop;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {
    @Test
public  void show(){
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
    Operate operate=(Operate)applicationContext.getBean("ope");
    operate.parse();
}
    @Test
public void shows(){
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
      Aop aop =(Aop)applicationContext.getBean("aop");
      aop.show();
    
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值