Spring入门第二十三课

我们看基于XML配置的方式配置AOP

看代码:

package logan.study.aop.impl;

public interface ArithmeticCalculator {

    int add(int i, int j);
    int sub(int i, int j);
    int mul(int i, int j);
    int div(int i, int j);
}
package logan.study.aop.impl;

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    @Override
    public int add(int i, int j) {
        // TODO Auto-generated method stub
        int result = i + j;
        return result;
    }

    @Override
    public int sub(int i, int j) {
        // TODO Auto-generated method stub
        int result = i - j;
        return result;
    }

    @Override
    public int mul(int i, int j) {
        // TODO Auto-generated method stub
        int result = i * j;
        return result;
    }

    @Override
    public int div(int i, int j) {
        // TODO Auto-generated method stub
        int result = i / j;
        return result;
    }

}
package logan.study.aop.impl;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;

public class LoggingAspect {

    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method "+ methodName +" begins with "+args);
    }

    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method "+ methodName +" ends with "+args);
    }

    public void afterReturningMethod(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method "+ methodName +" ends with "+result);
    }

    public void afterThrowingMethod(JoinPoint joinPoint,Exception ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method "+ methodName +" occus with "+ex);
    }

}
package logan.study.aop.impl;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Before;
public class VlidationAspect {
    @Before("LoggingAspect.declareJoinPointException()")
    public void validateArgs(JoinPoint joinPoint){
        System.out.println("validate: " + Arrays.asList(joinPoint.getArgs()));
    }

}
<?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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <bean id="arithmeticCalculator"
    class="logan.study.aop.impl.ArithmeticCalculatorImpl"></bean>

    <bean id="loggingAspect"
    class="logan.study.aop.impl.LoggingAspect"></bean>

    <bean id="vlidationAspect"
    class="logan.study.aop.impl.VlidationAspect"></bean>

    <aop:config>
        <aop:pointcut expression="execution(* logan.study.aop.impl.ArithmeticCalculatorImpl.*(int, int))"
        id="pointcut"/>
        <aop:aspect ref="loggingAspect" order="2">
            <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
            <aop:after method="afterMethod" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterReturningMethod" pointcut-ref="pointcut" returning="result"/>
            <aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="ex"/>
        </aop:aspect>
        <aop:aspect ref="vlidationAspect" order="1">
            <aop:before method="validateArgs" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

</beans>
package logan.study.aop.impl;

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

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //1.创建Spring的IOC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
        //2.从IOC容器里面获取bean实例
        ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
        //使用Bean
        int result = arithmeticCalculator.add(3, 6);
        System.out.println(result);

        result = arithmeticCalculator.div(3, 1);
        System.out.println(result);

    }

}

返回结果:

五月 28, 2017 7:53:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]49c2faae: startup date [Sun May 28 07:53:29 CST 2017]; root of context hierarchy
五月 28, 2017 7:53:29 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext-xml.xml]
validate: [3, 6]
The method add begins with [3, 6]
The method add ends with [3, 6]
The method add ends with 9
9
validate: [3, 1]
The method div begins with [3, 1]
The method div ends with [3, 1]
The method div ends with 3
3

稍微修改代码:

package logan.study.aop.impl;

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

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //1.创建Spring的IOC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
        //2.从IOC容器里面获取bean实例
        ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
        //使用Bean
        int result = arithmeticCalculator.add(3, 6);
        System.out.println(result);

        result = arithmeticCalculator.div(3, 0);
        System.out.println(result);

    }

}

返回结果:

五月 28, 2017 7:56:58 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]49c2faae: startup date [Sun May 28 07:56:58 CST 2017]; root of context hierarchy
五月 28, 2017 7:56:58 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext-xml.xml]
validate: [3, 6]
The method add begins with [3, 6]
The method add ends with [3, 6]
The method add ends with 9
9
validate: [3, 0]
The method div begins with [3, 0]
The method div ends with [3, 0]
The method div occus with java.lang.ArithmeticException: / by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at logan.study.aop.impl.ArithmeticCalculatorImpl.div(ArithmeticCalculatorImpl.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy2.div(Unknown Source)
    at logan.study.aop.impl.Main.main(Main.java:18)
时间: 2024-11-07 14:57:24

Spring入门第二十三课的相关文章

Spring入门第二十一课

切面优先级 先看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); } package logan.study.aop.impl; import org.springframework.stereotype.Compon

Spring入门第二十课

返回通知,异常通知,环绕通知 看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); } package logan.study.aop.impl; import org.springframework.stereotyp

Spring入门第二十七课

声明式事务 直接上代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring jdbc.initPoolSize=5 jdbc.maxPoolSize=10 applicationContext.xml <?xml version="1.0" encodin

第二十三课

第二十三课第一单元语法部分 Vておく<提前>:预先…… 口语形式:-とく 说明:   A.表示为后面要做的事情事先做好某种准备. B.表示采取某种行为,并使其结果的状态持续下去.   C.有时表示一种临时的措施. 例句:     1 日本へ行く前に日本語を習っておくつもりだ.    2 電気は消さないで 練習: 1.事先打个电话问一.朝までつけておこう.    3 その場で一応の手当てをしておいて.病院へ連れていった.下比较好 2.预先磨好刀. Vてある<客体存续的状态>: 说明:

NeHe OpenGL教程 第二十三课:球面映射

转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线教程的编写,以及yarn的翻译整理表示感谢. NeHe OpenGL第二十三课:球面映射 球面映射: 这一个将教会你如何把环境纹理包裹在你的3D模型上,让它看起来象反射了周围的场景一样. 球体环境映射是一个创建快速金属反射效果的方法,但它并不像真实世界里那么精确!我们从18课的代码开始来创建这个教程

Spring入门第二课

看代码 package logan.spring.study; public class HelloWorld { private String name; public void setName2(String name){ System.out.println("setName: "+ name); this.name = name; } public void hello(){ System.out.println("hello: " + name); } p

Spring入门第二十二课

重用切面表达式 我们有的时候在切面里面有多个函数,大部分函数的切入点都是一样的,所以我们可以声明切入点表达式,来重用. package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); } package logan.study.aop.im

Spring入门第二十四课

Spring对JDBC的支持 直接看代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/selective-courses-system jdbc.initPoolSize=5 jdbc.maxPoolSize=10 applicationContext.xml <?xml ve

Spring入门第二十五课

使用具名参数 直接看代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/selective-courses-system jdbc.initPoolSize=5 jdbc.maxPoolSize=10 applicationContext.xml <?xml version=&quo