java学习笔记整理JavaEE 学习专题程序员

AOP---面向切面

2017-09-16  本文已影响29人  常威爆打来福
一 概念

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

二 原理
原理 第一种情况 第二种情况
三 操作术语
四 AOP-AspectJ准备

1 在spring里面进行aop操作,使用AspectJ实现

2 使用AspectJ实现aop有两种方式
(1)基于AspectJ的xml配置
(2)基于AspectJ的注解方式
3 jar包

jar包

maven配置

<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

http://repo.spring.io/release/org/springframework/spring
4 创建spring核心配置文件,导入AOP操作
5 使用表达式配置切入点
(1)切入点,实际增强的方法
(2)常用的表达式
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

五 基于AspectJ的xml配置

1 Book.java

package AOP;

/**
 * Created by pc on 2017/9/16.
 */
public class Book {
    public void add(){
        System.out.println("add.......");
    }
}

2 MyBook.java

package AOP;

/**
 * Created by pc on 2017/9/16.
 */
public class MyBook {
    public void before(){
        System.out.println("前置增强.........");
    }
}

3 .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: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 -->

        <bean id="book" class="AOP.Book" />
        <bean id="mybook" class="AOP.MyBook" />
    <aop:config>
        <!--1.配置切入点-->
        <aop:pointcut id="pointcut1" expression="execution(* AOP.Book.*(..))" />
       <!-- 2.配置切面
       把增强用到方法上面-->
        <aop:aspect ref="mybook">
            <!--配置增强类型
            method:增强类里面使用哪个方法作为前置
            -->
            <aop:before method="before" pointcut-ref="pointcut1" />
        </aop:aspect>
    </aop:config>
</beans>

4 测试

        @Test
    public void test4(){
            ApplicationContext context = new ClassPathXmlApplicationContext("Spring/bean.xml");
           Book book = (Book) context.getBean("book");
           book.add();
        }
测试结果
六 基于AspectJ的注解方式

1 Books.java

package AOP;

import org.springframework.stereotype.Component;

/**
 * Created by yang on 17-10-22.
 */
//类似bean
@Component(value = "books")
public class Books {
    public void add(){
        System.out.println("add........");
    }
}

2 Mybooks.java

package AOP;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * Created by yang on 17-10-22.
 */
@Component(value = "myBooks")
//切点
@Aspect
public class MyBooks {
    @Before(value = "execution(* AOP.Books.*(..))")
    public void mybooks(){
        System.out.println("before.........");
    }
}

3 .xml配置

    <!--开启aop扫描-->
    <aop:aspectj-autoproxy/>
    <!--开启注解扫描-->
    <context:component-scan base-package="AOP"></context:component-scan>

4 测试

        @Test
    public void test5(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
         Books books = (Books) context.getBean("books");
         books.add();
        }
执行结果
上一篇 下一篇

猜你喜欢

热点阅读