SpringAOP的实现

AOP的作用:提供声明式事务,允许用户自定义切面

1、横切关注点:跨越应用程序多个模块的方法或功能。即,与我们业务逻辑无关的,但我们需要关注的部分,就是横切关注点。如:日志、缓存、事务等等…。
2、切面(Aspect):横切关注点被实例化的特殊对象,即,他是一个类。
3、通知(Advice):切面一定要完成的工作。即,他是一个切面类中的方法。
4、目标(Target):被通知的目标对象。即,实际要被执行的对象。
5、代理(Proxy):向目标对象应用通知之后创建的对象。
6、切入点(PointCut):切面通知执行的“地点”的定义。
7、连接点(JointPoint):与切入点匹配的执行点。

AOP实现示例:现有类和方法在不修改原代码的情况下,增加函数执行前和执行后输出日志

导入切面织入器包

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.7</version>
</dependency>

定义基本业务接口和实现类类

/**
 * 用户Service接口
 */
public interface UserService {
    void add();
    void delete();
    void update();
    void select();
}
/**
 * 用户Service实现类
 */
public class UserServiceImpl implements UserService{

    @Override
    public void add() {
        System.out.println("添加用户");
    }

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

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

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

方式一:使用原生Spring API接口实现

用户类的函数被调用前执行日志输出

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;

/**
 * 输出日志
 * 方法执行前执行
 */
public class BeforeLog implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "类的" + method.getName() + "即将被执行了!");
    }
}

用户类的函数被调用后执行日志输出

import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;

/**
 * 方法被执行后执行,带返回值
 */
public class AfterLog implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "的" + method.getName() + "方法已经被执行!!");
    }
}

Spring 容器配置文件

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 注册bean -->
    <bean id="userService" class="com.zjy.demo01.service.UserServiceImpl"></bean>
    <bean id="beforeLog" class="com.zjy.demo01.log.BeforeLog"></bean>
    <bean id="afterLog" class="com.zjy.demo01.log.AfterLog"></bean>

    <!-- 方式一:使用原生Spring API接口-->
    <!-- 配置aop:需要导入AOP的约束-->
    <aop:config>
        <!-- 设置切入点:execution:表达式,execution(返回值 地址包下类下所有方法(任意参数))-->
        <aop:pointcut id="pointcut" expression="execution(* com.zjy.demo01.service.UserServiceImpl.*(..))"/>
        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"></aop:advisor>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
</beans>

方式二:自定义AOP切面类实现

/**
 * 自定义切面
 */
public class DiyPointCut {

    //方法调用前执行
    public void before(){
        System.out.println("自定义:方法调用前执行");
    }

    //方法调用后执行
    public void after(){
        System.out.println("自定义:方法调用后执行");
    }
}

Spring 容器配置文件

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 注册bean -->
    <bean id="userService" class="com.zjy.demo01.service.UserServiceImpl"></bean>
    <bean id="beforeLog" class="com.zjy.demo01.log.BeforeLog"></bean>
    <bean id="afterLog" class="com.zjy.demo01.log.AfterLog"></bean>

    <!-- 方式二:自定义切面-->
    <bean id="diyPointCut" class="com.zjy.demo01.diy.DiyPointCut"></bean>
    <aop:config>
        <!-- 切面实现-->
        <aop:aspect ref="diyPointCut">
            <!-- 定义切入点-->
            <aop:pointcut id="pointcut" expression="execution(* com.zjy.demo01.service.UserServiceImpl.*(..))"/>
            <!-- 自定义方法-->
            <aop:before method="before" pointcut-ref="pointcut"></aop:before>
            <!-- 自定义方法-->
            <aop:after method="after" pointcut-ref="pointcut"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

方式三:使用注解实现

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 使用注解方式实现AOP
 */
@Aspect
public class AnnotationPointCut {

    //目标方法执行前执行
    @Before("execution(* com.zjy.demo01.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("目标方法执行前执行");
    }

    //目标方法执行后执行
    @After("execution(* com.zjy.demo01.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("目标方法执行后执行");
    }

    //目标方法执行并有返回值后执行
    @AfterReturning("execution(* com.zjy.demo01.service.UserServiceImpl.*(..))")
    public void afterReturning(){
        System.out.println("目标方法执行并有返回值后执行");
    }

    //目标方法环绕执行
    @Around("execution(* com.zjy.demo01.service.UserServiceImpl.*(..))")
    public void aroud(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前");
        //执行目标方法
        Object proceed = joinPoint.proceed();
        System.out.println("返回值:"+proceed);
        System.out.println("环绕后");
    }
}
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 注册bean -->
    <bean id="userService" class="com.zjy.demo01.service.UserServiceImpl"></bean>
    <bean id="beforeLog" class="com.zjy.demo01.log.BeforeLog"></bean>
    <bean id="afterLog" class="com.zjy.demo01.log.AfterLog"></bean>

    <!-- 方式三:注解方式-->
    <!-- 注入注解切面类-->
    <bean id="annotationPointCut" class="com.zjy.demo01.diy.AnnotationPointCut"/>
    <!-- 开启注解支持!  属性proxy-target-class默认false(JDK),true(cglib)-->
    <aop:aspectj-autoproxy/>

</beans>

Spring集成 Mybatis实现

pom.xml配置项目用到的包和maven静态资源过滤

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-aop-mybatis</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.22</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.22</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.7</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.9</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.7</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>
package com.zjy.pojo;

import lombok.Data;

@Data
public class User {
    private int id;
    private String name;
    private String pwd;
}

package com.zjy.mapper;

import com.zjy.pojo.User;
import java.util.List;

/**
 * 用户Mapper数据库操作接口
 */
public interface UserMapper {

    public List<User> selectUser();
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--用户Mapper实现xml文件-->
<mapper namespace="com.zjy.mapper.UserMapper">

    <select id="selectUser" resultType="user">
        select * from user;
    </select>
</mapper>

Mybatis配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.zjy.pojo"/>
    </typeAliases>

    <!--<environments> 可以为MyBatis配置数据环境。-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" ></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/text?useUnicode=true&amp;characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull&amp;serverTimezone=GMT%2B8&amp;"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!--注册一个包下的所有持久层接口-->
        <package name="com.zjy.mapper"/>
    </mappers>
</configuration>

求关注~~~

点关注不迷路,喜欢的朋友们关注支持一下
给点继续写的动力,感谢!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乾坤鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值