aspectj xml

1.接口和类

1.1 ISomeService 接口

public interface ISomeService {

    public void doSome();

    public void dade();
}

1.2  SomeService类

public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
    }

    public void dade() {
        //异常
        //int sun=5/0;
        System.out.println("++++++++++++++de+++++++++++++");
    }

}

1.3  MyAspect 类

public class MyAspect {

    //前置增强
    public void before(){
        System.out.println("=====before========");
    }
    //后置增强
    public void afterReturing(){
        System.out.println("=====after========");
    }
    //后置增强   目标方法的返回值
    public void afterReturing(String result){
        System.out.println("=====后置通知方法 result========"+result);
    }
    //环绕通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("目标方法执行前执行");
        Object result = pjp.proceed();
        System.out.println("目标方法执行后执行");
        String temp=null;
        if (result!=null) {
            temp = (String) result;
            temp=temp.toUpperCase();
        }
        return temp;
    }
    //异常通知
    public void afterThrowing(){
        System.out.println("异常通知");
    }
    //异常通知
    public void afterThrowing(Exception ex){
        System.out.println("异常通知 ex="+ex.getMessage());
    }

    //最终通知
    public void after(){
        System.out.println("最终通知");
    }

}

2.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:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
">
    <!--01.目标对象-->
    <bean id="someService" class="cn.bdqn.spring18.SomeService"></bean>

    <!--02.增强类-->
    <bean id="aspect" class="cn.bdqn.spring18.MyAspect"></bean>

    <!--aop-->
    <aop:config>
        <!--设置一个切点-->
        <aop:pointcut id="mycut" expression="execution(* *..spring18.*.*(..))"></aop:pointcut>
        <aop:aspect ref="aspect">
            <aop:before method="before" pointcut-ref="mycut"></aop:before>
            <aop:after-returning method="afterReturing" pointcut-ref="mycut"></aop:after-returning>
            <aop:after-returning method="afterReturing(java.lang.String)" returning="result" pointcut-ref="mycut"></aop:after-returning>
            <aop:around method="around" pointcut-ref="mycut"></aop:around>
            <aop:after-throwing method="afterThrowing" pointcut-ref="mycut"></aop:after-throwing>
           <aop:after-throwing method="afterThrowing(java.lang.Exception)" throwing="ex" pointcut-ref="mycut"></aop:after-throwing>
            <aop:after method="after" pointcut-ref="mycut"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

3.测试类

 //aspectj xml
    @Test
    public void test20(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring16.xml");
        ISomeService service = (ISomeService) ctx.getBean("someService");
        service.doSome();
        service.dade();
    }
时间: 2024-08-19 11:27:00

aspectj xml的相关文章

AOP中的ASPECTJ

一.准备 1.架包 2.配置文件 二.注解的形式 UserDao.java 1 package cn.itcast.spring.aspectj.annocation; 2 3 public class UserDao { 4 5 public void add(){ 6 System.out.println("add......"); 7 } 8 9 public void delete(){ 10 System.out.println("delete......"

Spring AOP + AspectJ Annotation Example---reference

In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simple, Spring AOP + AspectJ allow you to intercept method easily. Common AspectJ annotations : @Before – Run before the method execution @After – Run aft

aspect xml

1.接口和类 1.1 ISomeService 接口 public interface ISomeService { public void doSome(); public void dade(); } 1.2  SomeService类 public class SomeService implements ISomeService { //核心业务 public void doSome(){ System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K&

javaEE之-------Spring中的aspectJ的应用

采用aspectJ可以且整个项目里面的需求. 如数据库的事物处理等 演示实例为主: 和之前一样,先采用java手动写的方式.. 切面技术 = 通知 + 切点 <span style="font-size:18px;">@Test public void test1(){ //1,获得代理工厂 ProxyFactory factory = new ProxyFactory(new Person()); //2,切点 AspectJExpressionPointcut cut

AspectJ AOP学习基础

一.切入点表达式 1.execution:匹配方法的执行 格式:execution(修饰符 返回值类型 包.类.方法(参数) throw 异常) 1.1修饰符,表示方法的修饰符,一般省略. 1.2返回类型 String表示返回String:void表示没有返回值:*表示返回任意类型,包括无返回值. 1.3包 hjp.spring.service 表示指定的包 hjp.spring.*.service 表示spring下子模块包含service的包 hjp.spring.service.. 表示s

Spring使用AspectJ开发AOP:基于Annotation

基于 Annotation 的声明式 在 Spring 中,尽管使用 XML 配置文件可以实现 AOP 开发,但是如果所有的相关的配置都集中在配置文件中,势必会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难. 为此,AspectJ 框架为 AOP 开发提供了另一种开发方式——基于 Annotation 的声明式.AspectJ 允许使用注解定义切面.切入点和增强处理,而 Spring 框架则可以识别并根据这些注解生成 AOP 代理. 关于 Annotation 注解的介绍如表 1

Spring day03笔记

spring day02回顾 AOP :切面编程 ????切面:切入点 和 通知 结合 spring aop 编程 <aop:config> 方法1: <aop:pointcut expression="切入点表达式" id=""> <aop:advisor advice-ref="通知引用" pointcut-ref="切入点的引用"> ? 方法2: <aop:advisor adv

Spring 框架(三)

1 spring l AOP :切面编程 切面:切入点 和 通知 结合 l spring aop 编程 <aop:config> 方法1: <aop:pointcut expression="切入点表达式" id=""> <aop:advisor  advice-ref="通知引用" pointcut-ref="切入点的引用"> 方法2: <aop:advisor  advice-re

spring_3

1 spring day02回顾 l AOP :切面编程 切面:切入点 和 通知 结合 l spring aop 编程 <aop:config> 方法1: <aop:pointcut expression="切入点表达式" id=""> <aop:advisor  advice-ref="通知引用" pointcut-ref="切入点的引用"> 方法2: <aop:advisor  a