spring之AOP

来源:互联网 发布:画网络拓扑图的软件 编辑:程序博客网 时间:2024/04/30 05:05

一.AOP详解

1.AOP概念

  • AOP:面向切面(方面)编程,扩展功能不修改源代码实现
  • AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码

2.AOP原理

这里写图片描述

这里写图片描述

这里写图片描述

3.AOP操作术语

这里写图片描述

这里写图片描述

这里写图片描述

二.Spring的aop操作

  • 在spring里面进行aop操作,使用aspectj实现
    • aspectj不是spring一部分,和spring一起使用进行aop操作
    • spring2.0 以后新增了对aspectj切点表达式支持
  • 使用aspectj实现aop有两种方式
    • 基于aspectj的xml配置
    • 基于aspectj的注解方式

1.aop操作准备

  • 除了导入基本的jar包之外,还需要导入aop相关的jar包
    这里写图片描述

    • 创建spring核心配置文件,导入aop的约束

这里写图片描述

2.使用表达式配置切入点

  • 切入点:实际增强的方法
  • 常用的表达式
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(1) execution(* cn.st.aop.Book.add(..))(2) execution(* cn.st.aop.Book.*(..))(3) execution(* *.*(..))(4) 匹配所有save开头的方法 execution(* save*(..))

3.aspectj的aop操作

<?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.xsd"> <!-- bean definitions here -->           <!-- 1.配置对象 -->          <bean id="book" class="cn.st.web.Book"></bean>          <bean id="myBook" class="cn.st.web.MyBook"></bean>          <!-- 2 配置aop操作 -->          <aop:config>                <!--  2.1 配置切入点 -->                 <aop:pointcut expression="execution(* cn.st.web.Book.*(..))" id="pointcut1"/>                 <!-- 2.2配置切面把增强用到方法上面 -->                 <aop:aspect ref="myBook">                      <!-- 配置增强类型                      method:增强类里面使用哪个方法作为前置 -->                        <aop:before method="before1" pointcut-ref="pointcut1"/>                           <aop:after method="after1" pointcut-ref="pointcut1"/>                              <aop:around method="around1" pointcut-ref="pointcut1"/>                 </aop:aspect>          </aop:config></beans>      
public class Book {    public void add() {        System.out.println("add...");    }}
public class MyBook {     public void before1() {         System.out.println("before1...");     }     public void after1() {         System.out.println("after1...");     }    //环绕通知     public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{         //方法之前             System.out.println("方法之前...");             //执行被增强的方法             proceedingJoinPoint.proceed();             //方法之后             System.out.println("方法之后...");     }}
public class springTest {      @Test      public void fun1() {         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean1.xml");         Book book=(Book)applicationContext.getBean("book");         book.add();      }}

控制台打印

before1...around1...add......after1...

三.log4j介绍

  • 通过log4j可以看到程序运行过程中更详细的信息

    • 经常使用log4j查看日志
  • 使用

    • 导入log4j的jar包
    • 复制log4j的配置文件,复制到src下面
      这里写图片描述
  • 设置日志级别

    • Info:看到基本信息
    • debug:看到更详细信息
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c:\mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=info, stdout   //看到基本信息

四.Spring整合web项目演示

1.演示问题

  • Action调用service, service调用dao

这里写图片描述

  • 每次访问action时候,都会加载spring配置文件

2.解决方案

  • 在服务器启动时候,创建对象加载配置文件
  • 底层使用监听器,ServletContext对象

3.在spring里面不需要我们自己写代码实现,帮我们封装了的

  • 封装了一个监听器,只需要配置监听器 就可以了
  • 配置监听器之前做事情,导入spring整合web项目jar包

这里写图片描述

这里写图片描述

  • 指定加载spring配置文件位置

这里写图片描述

这里写图片描述

原创粉丝点击