Spring------AOP面向切面编程,实现AOP

AOP

  • AOP(Aspect Oriented Programming)意为:面向切面编程
  • AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型

使用Spring实现AOP

方式一:配置增强类
  1. 导入依赖
<!--织入包-->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

2.编写要实现的接口

package com.yang.service;

public interface UserService {
    public void add();

    public void delete();

    public void update();

    public void serach();
}

  1. 编写实现类
import com.yang.service.UserService;

public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("增加用户");
    }

    public void delete() {
        System.out.println("删除用户");
    }

    public void update() {
        System.out.println("更新用户");
    }

    public void serach() {
        System.out.println("查询用户");
    }
}

  1. 编写增强类,前置增强,后置增强
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class LogAfter implements AfterReturningAdvice {
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"方法,返回了"+returnValue);
    }
}


import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()
        +"的"+method.getName()+"方法被执行了");
    }
}
  1. 注册到bean,相当于创建对象
<bean id="userService" class="com.yang.service.Impl.UserServiceImpl"/>
<bean id="log" class="com.yang.config.LogAfter"/>
<bean id="afterlog" class="com.yang.config.LogAfter"/>

  1. 添加AOP的配置文件
xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

  1. 配置AOP
   <!--aop的配置-->
<aop:config>
    <!--切入点,增强那些-->
    <aop:pointcut id="pc-userservice"   expression="execution(* com.yang.service.Impl.UserServiceImpl.*(..))"/>
    <!--环绕-->
    <aop:advisor advice-ref="log" pointcut-ref="pc-userservice"/>
    <aop:advisor advice-ref="afterlog"  pointcut-ref="pc-userservice"/>
</aop:config>
  1. 测试
@Test
public void test2(){
   //      bean就是创建对象
   ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   UserService userService = (UserService) context.getBean("userService");
   userService.add();
}
方式二:切面配置,有增强方式的类(bean就是在创建对象)

“execution(* com.yang.service.Impl.UserServiceImpl.*(…))”
这是创对象的关键,一定要写对

  1. 导入依赖
  2. 编写接口 ,编写实现类
package com.yang.service;
public interface UserService {
    public void add();

    public void delete();

    public void update();

    public void serach();
}
package com.yang.service.Impl;

import com.yang.service.UserService;

public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("增加用户");
    }

    public void delete() {
        System.out.println("删除用户");
    }

    public void update() {
        System.out.println("更新用户");
    }

    public void serach() {
        System.out.println("查询用户");
    }
}

  1. 编写增强类(含多个方法)
package com.yang.config;

public class zichuanglei {
    public void before(){
        System.out.println("=====qian=====");
    }
    public void after(){
        System.out.println("=====hou=====");
    }
}
  1. 配置到bean,完成 xml 的配置
<bean id="userService" class="com.yang.service.Impl.UserServiceImpl"/>
<bean id="ces" class="com.yang.config.zichuanglei"/>


<aop:config>
    <!--配置切面,需要配置标签来控制-->
    <aop:aspect ref="ces">
        <aop:pointcut id="pc-userservice" expression="execution(* com.yang.service.Impl.UserServiceImpl.*(..))"/>
        <!--方法来自于切面-->
        <aop:before method="before" pointcut-ref="pc-userservice"/>
        <aop:after method="after" pointcut-ref="pc-userservice"/>
    </aop:aspect>
</aop:config>

</beans>
  1. 测试
@Test
public void test2(){
   //      bean就是创建对象
   ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   UserService userService = (UserService) context.getBean("userService");
   userService.add();
}
注解实现
  1. 倒包
  2. 编写接口及实现类
  3. 编写增强类
package com.yang.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//切面原理
@Aspect
public class AnnotationPointcut {
    @Before("execution(* com.yang.service.Impl.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("=====qian=====");
    }
    @After("execution(* com.yang.service.Impl.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("=====hou=====");
    }
    @Around("execution(* com.yang.service.Impl.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("环绕前");
        System.out.println(joinPoint.getSignature());
        Object proceed = joinPoint.proceed();
        System.out.println("环绕后");
        System.out.println(proceed);
    }

}

  1. 注册bean
<bean id="userService" class="com.yang.service.Impl.UserServiceImpl"/>
<bean id="AnnotationPointcut" class="com.yang.config.AnnotationPointcut"/>
<!--让spring识别注解,开启识别-->
<aop:aspectj-autoproxy/>
   <!--aop的配置-->
  1. 测试

MyBatis-Spring

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值