Spring自学日志06(Aop)

什么是AOP

面向切面编程,通过预编译的方式和运行期动态代理实现程序功能的同一维护的技术。

AOP在Spring中的作用

提供声明式事务:允许用户自定义切面。
1:代理(proxy)向目标对象应用通知之后创建的对象。
2:切入点(PointCut)切面执行的“地点”的定义。
3:连接点(JointPoint)与切入点匹配的执行点。
4:目标(Target)被通知的对象
5:通知(Advice)切面必须完成的工作。即,他是类中的一个方法。
6:切面(Aspect)横切关注点被模块化的特殊对象。即,他是一个类。

用Spring接口实现AOP

首先导入依赖包

compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.5'

环境搭建
UserService

package com.Service;

public interface UserService {
    public void add();
    public void delete();
    public void updata();
    public void select();
}

UserServiceImpl

package com.Service;

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

    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void updata() {
        System.out.println("修改了一个用户");
    }

    @Override
    public void select() {
        System.out.println("查找了一个用户");
    }
}

log

package com.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class log implements MethodBeforeAdvice {
    //method:执行目标对象的方法
    //Object:参数
    //target:参数对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass()+"的"+method.getName()+"被执行了");
    }
}

Afterlog

package com.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class Afterlog implements AfterReturningAdvice {
    //returnValue返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回值为"+returnValue);
    }
}

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:p="http://www.springframework.org/schema/p"
        xmlns:c="http://www.springframework.org/schema/c"
        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-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:annotation-config/>
    <bean id="userService" class="com.Service.UserServiceImpl"/>
    <bean id="log" class="com.log.log"/>
    <bean id="Afterlog" class="com.log.Afterlog"/>
    <!--配置AOP:需要导入AOP的约束,使用原生Spring API接口-->
    <aop:config>
        <!--切入点:expression:表达式.execution(执行的位置 * * * * *)-->
        <aop:pointcut id="pointcut" expression="execution(* com.Service.UserServiceImpl.*(..))"/>
        <!--执行环绕增强-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="Afterlog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

使用自定义类实现AOP

定义一个类
DiyPoiunt

package com.csm;

public class DiyPoiunt {
    public void before(){
        System.out.println("===方法执行前===");
    }
    public void after(){
        System.out.println("===方法执行后===");
    }
}

重写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:p="http://www.springframework.org/schema/p"
        xmlns:c="http://www.springframework.org/schema/c"
        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-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:annotation-config/>
    <bean id="userService" class="com.Service.UserServiceImpl"/>
    <bean id="log" class="com.log.log"/>
    <bean id="Afterlog" class="com.log.Afterlog"/>

<--自定义类-->
    <bean id="Diy" class="com.csm.DiyPoiunt"/>
    <aop:config>
        <!--自定义切面。ref要引用类-->
        <aop:aspect ref="Diy">
            <!--切入点-->
            <aop:pointcut id="point" expression="execution(* com.Service.UserServiceImpl.*(..))"/>
            <!--通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point" />
        </aop:aspect>
    </aop:config>
</beans>

注解实现AOP

定义一个类AnnotationPointCut

package com.csm;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

//使用注解实现Aop

@Aspect//标记该类为一个切面
public class AnnotationPointCut {
    @Before("execution(* com.Service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("===方法执行前===");
    }
    @After("execution(* com.Service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("===方法执行后===");
    }
}

重写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:p="http://www.springframework.org/schema/p"
        xmlns:c="http://www.springframework.org/schema/c"
        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-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:annotation-config/>

    <bean id="userService" class="com.Service.UserServiceImpl"/>
    <bean id="log" class="com.log.log"/>
    <bean id="Afterlog" class="com.log.Afterlog"/>
    <bean id="annotationPointCut" class="com.csm.AnnotationPointCut"/>
    <!--开启注解支持 Jdk(默认false) cglib(true-->
    <aop:aspectj-autoproxy/>
</beans>
发布了20 篇原创文章 · 获赞 0 · 访问量 201

猜你喜欢

转载自blog.csdn.net/qq_43697752/article/details/103601421
今日推荐