爱代码

做一个最单纯的人 || 走一段最幸福的路

    已关注
    关注我

粉丝: 2 关注: 1

微信公众号搜索 未来技术网 关注我们 支付宝首页搜索:“ 7151684 ” 即可领取最高99元红包

sringAOP详解

  2018-08-12 11:22     10

AOP 面向切面编程。

降低模块之间的耦合度,提高开发效率,提高可维护性,可扩展性,可复用性


Aop(aspect oriented programming面向切面编程),是spring框架的另一个特征。AOP包括切面、连接点、通知(advice)、切入点(pointCut) 。


1.aop几个概念:


  • 横切关注点: 对哪些方面进行拦截,拦截后怎么处理。

  • 切面(aspect):切面是横切关注点的抽象。

  • 连接点(joinpoint):被拦截的方法

  • 切入点(pointcut):对连接点进行拦截的定义。

  • 通知(advice):拦截到连接点之后要执行的代码

  • 目标对象:代理的目标对象

  • 织入

  • 引入



2.主要功能:


  • 日志记录

  • 性能统计

  • 安全控制

  • 事物处理

  • 异常处理



3.advice类型:


  • 前置通知(before advice)

  • 返回后通知(after  returning advice)

  • 抛出异常后通知(after throwing advice)

  • 后通知(after advice)

  • 环绕通知(around advice)



范例

例子我是基于maven和Spring注解的方式,用POJO实现

工程的主要文件的配置

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>imooc</groupId>
    <artifactId>springmaven</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springmaven Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <finalName>SSHMJ-FRANK</finalName>
    </properties>
    <repositories>
        <repository>
            <id>mav</id>
            <name>sss</name>
            <url>http://mvnrepository.com</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <layout>default</layout>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-asm</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument-tomcat</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.9</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.5.3</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc-portlet</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-struts</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>2.6.9</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2</version>
        </dependency>
    </dependencies>


    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.4</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <finalName>springmaven</finalName>
    </build>
</project>

`web.xml`

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<!--   工程名 -->
  <display-name>springmaven</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- spring配置文件的配置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
<!--   springmvc的配置环境 -->
  <servlet>
      <servlet-name>spring</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-servlet.xml</param-value>
      </init-param>
      <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>spring</servlet-name>
      <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

applicationContext.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" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          http://www.springframework.org/schema/task  
        http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <context:component-scan base-package="com.service" />
    <aop:aspectj-autoproxy />
    <aop:config proxy-target-class="true">
        <aop:aspect ref="preDo">
            <aop:pointcut expression="execution(* com.service.ToDo.toEat(..))"
                id="register" />
            <aop:before method="toPre" pointcut-ref="register" />
        </aop:aspect>
    </aop:config>

</beans>

spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    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-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">                   
 
        <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />
 
    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            id="internalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<!--         前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
<!--         后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

IToDo.java

切入点(Pointcut)接口

package com.service.imp;

public interface IToDo {

    public String toEat();
}

ToDo.java

package com.service;

import org.springframework.stereotype.Service;

import com.service.imp.IToDo;
@Service
public class ToDo implements IToDo {

    @Override
    public String toEat() {
        System.out.println("吃苹果");
        return "吃苹果";
    }

}

IPreDo.java

package com.service.imp;

public interface IPreDo {

    public String toPre();
}

application.java

用于测试

package springmaven;

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

import com.service.imp.IToDo;

public class application {
    public static void main(String[] args) {
    ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
     IToDo tdo = (IToDo)appCtx.getBean("toDo");
     tdo.toEat();
    }
}

工程图片

工程

返回的结果

洗手
吃苹果

主要的配置讲解

    <aop:aspectj-autoproxy />
    <aop:config proxy-target-class="true">
        <aop:aspect ref="preDo">
            <aop:pointcut expression="execution(* com.service.ToDo.toEat(..))"
                          id="register" />
            <aop:before method="toPre" pointcut-ref="register" />
        </aop:aspect>
    </aop:config>

<aop:aspectj-autoproxy />:会自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面,我这里没有使用注解的方式,使用了xml配置的方式。

<aop:config>:就是用来配置aspectJ切面

proxy-target-class:设置代理模式。当poxy-target-class="true"时,表示使用CGLib动态代理技术织入增强。设置为false时,表示使用jdk动态代理织入增强,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。

<aop:aspect ref="preDo">:设置切面,ref是切面Bean的id名

<aop:pointcut expression="execution(* com.service.ToDo.toEat(..))" id="register" />:这里设置切入点,expression设置切面植入的切入点的方法地址

<aop:before method="toPre" pointcut-ref="register" >:在执行切入点方法之前执行切面方法,method为切面中的执行方法,pointcut-ref与切点的id一致就可以了

<aop:advisor> 定义一个AOP通知者
<aop:after> 后通知
<aop:after-returning> 返回后通知
<aop:after-throwing> 抛出后通知
<aop:around> 周围通知
<aop:aspect>定义一个切面
<aop:before>前通知
<aop:config>顶级配置元素,类似于<beans>这种东西
<aop:pointcut>定义一个切点

<aop:after-returning>讲解

    <aop:aspectj-autoproxy />
    <aop:config proxy-target-class="true">
        <aop:aspect ref="preDo">
            <aop:pointcut expression="execution(* com.service.ToDo.toEat(..))"
                id="register" />
            <aop:after-returning method="toPre" pointcut-ref="register" returning="returnValue"/>
        </aop:aspect>
    </aop:config>

returning:是切点的返回值,在切面中通过returnValue接受,若要获取传给切点的参数在切面的java方法参数中加上JoinPoint,获取的是一个数组,按顺序排列。


package com.service;

import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Service;

import com.service.imp.IPreDo;
@Service
public class PreDo implements IPreDo {

    @Override
    public String toPre(JoinPoint joinPoint,Object returnValue) {
        System.out.println("洗手");
        System.out.println(joinPoint.getArgs()[0]);
        return "洗手";
    }

}



查看评论
发表评论
官方公众号