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");
    }

    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-03 17:01:37

aspect xml的相关文章

轻量级的.NET对象查找服务和AOP开发框架Netop.Core源码解说(2)--配置

先把Netop.Core的最核心部分"对象查找服务"放一放,先说说应用系统的配置. 一个应用系统的配置是少不了的,除非你是一个纯硬代码族顽固者. 也见过有的应用系统通过系统提供的健值(key-value)方法在appSettings节点下设了几十个甚至上百个,不堪入目,更别说条理性了. 开发一个应用框架,配置一般是少不了,如log4net就有自己的配置,不会让你在appSettings设几十个条目. 开发配置是很简单的,下面慢慢说来. 在NET应用中,配置信息以XML文档储存.WEB应

轻量级的.NET对象查找服务和AOP开发框架Netop.Core源码解说(4)--AOP

上节谈了谈类工厂/对象查找服务,本节谈谈AOP的实现. AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. Netop.Core的AOP采用代理的实现方式.采用代理方式,您的类就必须继承一个基类(当然不是那个默认的Object,如您的类已经有一个父类,那可以让那个类的父类去继承)-- ContextBoundObject或Netop.Core.Aspect.AspectObject.当然Net

轻量级的.NET对象查找服务和AOP开发框架Netop.Core源码解说(3)--类工厂/对象查找服务

上节谈了谈Netop.Core的对于应用系统的配置信息的处理,对于Netop.Core最核心的服务--类工厂/对象查找服务当然要用到配置服务,等下会说到. 对于NET类工厂/对象查找服务,名气大的有Spring.Net(对应于Java的Spring--号称轻量级中间件),为什么还要再造一个轮子呢?如果说Spring是轻量级的,那Netop.Core就只 能是微量级的,够用就好,学习曲线会大幅下降,学习研究代码的时间也会大幅下降. 够用就好,何乐而不为?况且Netop.Core的类工厂/对象查找服

一个使用Spring的AspectJ LTW的简单例子

参考:https://docs.spring.io/spring-framework/docs/4.3.9.RELEASE/spring-framework-reference/htmlsingle/#aop-aj-ltw-first-example https://www.ibm.com/developerworks/cn/java/j-lo-springaopcglib/(Spring AOP 实现原理与 CGLIB 应用) http://blog.csdn.net/a128953ad/ar

xml的方式配置AOP:Aspect Oriented Programming

在某些类中, 什么时机, 做什么事情 切入点(point-cut): 在某些类中(Class<?>[] itfc = new Class<?>[] { IStudentService.class }) 通知: 什么时机, 做什么事情(InvocationHandler的invoke方法) 切面: 切入点 + 通知 织入(weaver): Proxy.newProxyInstance: 把切入点和通知施加到具体的业务逻辑上的过程 XML配置AOP步骤: 1,准备了一个实现接口的bea

基于aspect实现AOP——xml配置的其他操作

将上方配置中的前置通知,可换成环绕通知 原文地址:https://www.cnblogs.com/cn-chy-com/p/9260031.html

Spring之AOP在XML中的配置方法

AOP 即 Aspect Oriental Program 面向切面编程 先来一个栗子: <aop:config> <aop:pointcut id="loggerCutpoint" expression= "execution(* com.how2java.service.ProductService.*(..)) "/> <aop:aspect id="logAspect" ref="loggerAsp

[刘阳Java]_Spring AOP基于XML配置介绍_第9讲

基于注解配置的Spring AOP固然简单,但是这节我们会给大家介绍基于XML配置的AOP是如何应用的.为什么这么说了,因为后面我们还会介绍到Spring对Dao操作的事务管理(基于AOP的XML文件方式来配置事务) 1. 基于XML文件方式来配置Spring的AOP,则我们需要的一些基本元素如下 <aop:config.../>,此标签很重要.它是在XML里配置AOP功能的核心标签 all aspect and advisor elements must be placed within a

xml实现AO

1.使用<aop:config></aop:config> 2.首先我们需要配置<aop:aspect></aop:aspect>,就是配置切面 2.1首先配置切面的id,也就是切面叫什么名称,我们起名字为id="myLogAspect" 2.2我们的切面是由哪一个类来做的,ref="logAspect",ref属性值是spring所管理的类(bean) 3.配置pointcut,下面需要配置,我们要在哪一些类里面加入