7.spring:SpringAOP(配置文件)

SpringAOP(xml文件配置)

配置文件的方式,主要是在xml文件中进行配置,不使用注解!

目录:

AtithmeticCalculator.java
public interface AtithmeticCalculator {
    int add(int i,int j);
    int sub(int i,int j);
    int mul(int i,int j);
    int div(int i,int j);
}
AtithmeticCalculatorImp1.java
@Component
public class AtithmeticCalculatorImp1 implements AtithmeticCalculator{
    public int add(int i, int j) {
        int res = i + j;
        return res;
    }
    public int sub(int i, int j) {
        int res = i - j;
        return res;
    }
    public int mul(int i, int j) {
        int res = i * j;
        return res;
    }
    public int div(int i, int j) {
        int res = i / j;
        return res;
    }
}
LoggionAspect.java
public class LoggionAspect {
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object [] args = joinPoint.getArgs();
        System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
    }
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends");
    }
    public void afterReturning(JoinPoint joinPoint, Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends with " + result);
    }
    public void afterThrowing(JoinPoint joinPoint, Exception e){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " occurs excetion:" + e);
    }}
LoggionAspect2.java
public class LoggionAspect2 {
    public void beforeMethod1(JoinPoint joinPoint){
      System.out.println("--->beforeMethod1");
      String methodName = joinPoint.getSignature().getName();
      Object [] args = joinPoint.getArgs();
      System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
      System.out.println("--->beforeMethod1");
    }

    public void afterMethod1(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends");
    }

    public void afterReturning1(JoinPoint joinPoint, Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends with " + result);
    }
    public void afterThrowing1(JoinPoint joinPoint, Exception e){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " occurs excetion:" + e);
    }
    

applicationContext.xml

<!-- 配置bean -->
<bean id="atithmeticCalculator" class="com.MrChengsc.AOP.xml.AtithmeticCalculatorImp1"></bean>

<!-- 配置切面的Bean -->
<bean id="loggionAspect" class="com.MrChengsc.AOP.xml.LoggionAspect"></bean>
<bean id="loggionAspect2" class="com.MrChengsc.AOP.xml.LoggionAspect2"></bean>

<!-- 配置AOP -->
<aop:config>
    <!-- 配置切点 表达式-->
    <aop:pointcut expression="execution(* com.MrChengsc.AOP.xml.AtithmeticCalculator.*(int, int))" id="pointcut"/>

    <!-- 配置切面及通知 -->
    <aop:aspect  ref="loggionAspect2" order="2">
        <aop:before method="beforeMethod1" pointcut-ref="pointcut"/>
    </aop:aspect>
    <aop:aspect  ref="loggionAspect" order="1">
        <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

main

    public static void main(String[] args) {
//        AtithmeticCalculator atithmeticCalculator = null;
//        atithmeticCalculator = new AtithmeticCalculatorImp();
//
//        atithmeticCalculator.add(1, 3);
//        System.out.println("---");
//        atithmeticCalculator.sub(3, 1);
//        System.out.println("---");
//        atithmeticCalculator.mul(1, 3);
//        System.out.println("---");
//        atithmeticCalculator.div(10, 2);
//        System.out.println("---");

        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //强制的类型使用接口的类型
        AtithmeticCalculator atithmeticCalculator = (AtithmeticCalculator) ctx.getBean(AtithmeticCalculator.class);

        int res = atithmeticCalculator.add(3, 6);
        System.out.println("res:" + res);
        System.out.println("-----");
//        int res1 = atithmeticCalculator.mul(2, 3);
//        System.out.println("res1:" + res1);

        //异常代码的测试
//        int res2 = atithmeticCalculator.div(10, 0);
//        System.out.println("res2:" + res2);
    }
The method add begins with [3, 6]
--->beforeMethod1
The method add begins with [3, 6]
--->beforeMethod1
res:9
-----

注:

1.配置bean,实现aop的类

2.配置切面的bean

3.配置aop需要使用<aop:config>标签

4.使用<aop:pointcut expression="execution(* com.MrChengsc.AOP.xml.AtithmeticCalculator.*(int, int))" id="pointcut"/>

  配置切点表达式

  *:代表任意的

  两个int:可以使用   ..  进行替换

5.配置切面以及通知使用<aop:aspect>

  ref:引用已配置的切面类bean

  order:切面的优先级(数值越小优先级越大)

6.标签:

<aop:before method="beforeMethod" pointcut-ref="pointcut"/> :前置通知
<aop:after method=""/>:后置通知
<aop:after-returning method="" returning="" pointcut="">:执行成功拿返回值
<aop:around method=""/>:环绕通知
<aop:after-throwing method="" >:异常通知

属性:

pointcut-ref:引用切点表达式

 method:切面类中的方法

returning:接受返回值

pointcut:切点表达式

原文地址:https://www.cnblogs.com/Mrchengs/p/10093698.html

时间: 2024-08-07 01:20:51

7.spring:SpringAOP(配置文件)的相关文章

Spring的配置文件 (SSM maven项目)

<?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:context="http://www.springframework.org/sch

Spring的配置文件

Web.xml将会配置Spring的配置文件位置: <servlet>        <servlet-name>x</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextC

spring mvc 配置文件:

标准方法是在web.xml中配置两个,然后在spring mvc配置文件中注解只扫controller注解,其余spring不扫controller注解 一.最开始当然是web.xml文件了,这是一个总的宏观配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee&q

SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean

分类: [java]2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路径下,Spring的配置文件也是src/applicationContext.xml路径下,那么我们可以借助Spring的property-placeholder读取配置文件,然后注入Bean中.我们在程序中,可以根据Bean的Id,获取注入的值.这样我们就可以借助Spring来读取配置文件. 2.

Spring Boot学习——Spring Boot配置文件application

Spring Boot配置文件有两种格式: application.properties 和 application.yml.两种配置文件只需要使用一个. 这两种配置文件的语法有些区别,如下 1. application.properties server.port = 8080         -- tomcat 端口 server.context-path = /webName    -- URL路径 2. application.yml server: port: 8080        

【SSH进阶之路】一步步重构容器实现Spring框架——配置文件+反射实现IoC容器(十)

目录 [SSH进阶之路]一步步重构容器实现Spring框架--从一个简单的容器开始(八) [SSH进阶之路]一步步重构容器实现Spring框架--解决容器对组件的"侵入式"管理的两种方案--主动查找和控制反转(九) [SSH进阶之路]一步步重构容器实现Spring框架--配置文件+反射实现IoC容器(十) [SSH进阶之路]一步步重构容器实现Spring框架--彻底封装,实现简单灵活的Spring框架(十一)(未更新) 上上篇博文[SSH进阶之路]一步步重构容器实现Spring框架--

在编辑Spring的配置文件时的自动提示

打 开MyEclipse—>Windows--->referenecs——>General,选择下面的Keys,这就是快捷键的设 置,可将Content Assist的快捷键改为 Alt+/ ,然后将command为word completion 的改为其他的快捷键,这样就OK了,在Spring的配置文件中敲代码时想要获得帮助时按住 Alt+/ 便会出现帮助 比如再输入<property name="maxIdle"></property>时,

java Spring使用配置文件读取jdbc.properties

Spring使用配置文件读取jdbc.properties 在beans.xml中加入两个必须的bean [html]<bean id="propertyConfigurer"     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">      <property name="location" value="

跟我一起学extjs5(27--服务端web,spring,hibernate配置文件的加入)

跟我一起学extjs5(27--服务端web,spring,hibernate配置文件的加入) 我们前面创建项目的时候是一个java web project,现在在项目中需要加入spring,hibernate,sqlserver连接的jar包以及加入配置文件.spring我现在使用的是3.1版本,hibernate使用的是3.0,具体的jar包以及一些附加包,请自行下载后加到工程中.(所有的jar包在我的前一个博客中提供的演示软件中有) 一.在java Resources的src中加入一些pa

Mina框架与Spring整合配置文件

Mina框架与Spring的整合其实很简单,主要是要弄清楚要注入的属性的名称,进而选择合适的注入方法. 关于Spring的四种注入方法请另一篇文章:spring依赖注入的四种方式 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w