Spring学习日志三

一、回顾  

1.1 依赖注入的方式。

  1. set方法来注入 <property name=”属性名” />
  2. 构造方法来注入<construtor-args index=”” />

1.2 依赖注入的数据类型。

  1. 基本类型和字符串 value
  2. 对象类型  ref=       <bean></bean>
  3. 集合List set
  4. map类型 <entry key= value=>
  5. array类型

1.3 引入属性文件.<context:property-       location=””>

1.4 自动注入。 autowire=”byName | byType | default | no ”

1.5 bean的作用域  scope=”singleton | prototype”

1.6 注解. @Repository (持久化) @Service (业务层) @Controller (控制层) @Component (组件) @Autowired (自动注入 先按照类型注入,再按照名称注入)  @Resource (自动注入 按照名称先注入,再按照类型注入。如果没有起名,那么它的名称就是属性的名称。

二、AOP简介

AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心功能毫无关系对于其他类型的代码,如安全性、异常处理和透明的持续性也都是如此,这种散布在各处的无关的代码被称为横切(cross cutting),在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。

AOP技术恰恰相反,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。

使用"横切"技术,AOP把软件系统分为两个部分:核心关注点横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志、事物。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。

三、使用注解完成AOP

  1.加入依赖

   2.创建一个类

package com.zhiyou100.klb.aop;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticImp {

    public double add(double a, double b) {
        double result=a+b;
        int c = 10/0;
        System.out.println("result:"+result);
        return result;
    }

    public double sub(double a, double b) {
        double result=a-b;
        System.out.println("result:"+result);
        return result;
    }

    public double mul(double a, double b) {
        double result=a*b;
        System.out.println("result:"+result);
        return result;
    }

    public double div(double a, double b) {
        double result=a/b;
        System.out.println("result:"+result);
        return result;
    }
}

  3.创建一个切面类并添加注释

package com.zhiyou100.klb.aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect //表名本类为切面类
@Component
public class ArithmeticProxy {
    
    //第一个*:通配的是访问修饰符
    //前置通知
    @Before(value="execution(* com.zhiyou100.klb.aop.*.*(..))")
    public void aa(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        String name = joinPoint.getSignature().getName();
        System.out.println("the method "+ name +" begin with" + Arrays.asList(args));
    }
    
    //后置通知
    @After(value="execution(* com.zhiyou100.klb.aop.*.*(..))")
    public void bb(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        String name = joinPoint.getSignature().getName();
        
        System.out.print("the method "+ name +" end result:");
    }
    
    //返回通知
    @AfterReturning(value="execution(* com.zhiyou100.klb.aop.*.*(..))",returning="result")
    public void cc(Object result) {
        System.out.println(result);
    }
    
    //异常通知
    @AfterThrowing(value="execution(* com.zhiyou100.klb.aop.*.*(..))",throwing="e")
    public void dd(Exception e) {
        System.out.println("异常了");
    }
}

  4.在配置文件中开启包扫描及切面扫描

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

    <!-- 包扫描:扫描 -->
    <context:component-scan base-package="com.zhiyou100.klb.aop"/>
    
    <!-- 开启切面扫描 -->
    <aop:aspectj-autoproxy/>
    
</beans>

  5.测试

package com.zhiyou100.klb.aop;

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

public class Test {

    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("app.xml");
        
        Arithmetic a = (Arithmetic) app.getBean("arithmeticImp");
        a.div(10, 2);
    }
}    

四、通过xml文件来完成AOP

  1.加入依赖(jar包)

  2.创建一个类

  3.创建一个切面类

  4.配置xml文件

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

    <!-- 定义被通知的程序类 -->
    <bean id="ari" class="com.zhiyou100.klb.aop2.ArithmeticImp"/>
    
    <!-- 定义切面类 -->
    <bean id="logApect" class="com.zhiyou100.klb.aop2.ArithmeticProxy"/>
    
    <!-- 配置切面 -->
    <aop:config>
        <!-- 定义表达式 切点 -->
        <aop:pointcut expression="execution(* com.zhiyou100.klb.aop2.*.*(..))" id="pointcut"/>
        <!-- 定义切面 -->
        <aop:aspect ref="logApect">
            <!-- 定义前置通知 -->
            <aop:before method="aa" pointcut-ref="pointcut"/>
            <!-- 定义后置通知 -->
            <aop:after method="bb" pointcut-ref="pointcut"/>
            <!-- 定义返回通知 -->
            <aop:after-returning method="cc" pointcut-ref="pointcut" returning="result"/>
            <!-- 定义异常通知 -->
            <aop:after-throwing method="dd" pointcut-ref="pointcut" throwing="e"/>
        </aop:aspect>
    </aop:config>
        
</beans>

  5.测试

原文地址:https://www.cnblogs.com/kklb/p/11482104.html