Spring-整合AspectJ的AOP使用 2021-9-19

Spring的基础和应用系列文章目录

第一部分 Spring 基础
1.1 XML使用-IoC配置与依赖(DI)配置
1.2 基于注解和XML混合方式的使用
1.3 基于纯注解方式的使用
1.4 AOP 介绍
1.5 Spring 整合AspectJ的AOP使用

第二部分 组件支持
2.1 Spring 整合Junit
2.2 Spring 事务支持


是指的Spring + AspectJ整合,不过Spring已经将AspectJ收录到自身的框架中了,并且底层织入依然是采取的动态织入方式。

前置条件

1、 导入依赖

	<!-- 基于AspectJ的aop依赖 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aspects</artifactId>
		<version>5.0.7.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>aopalliance</groupId>
		<artifactId>aopalliance</artifactId>
		<version>1.0</version>
	</dependency>

2 编写目标类和目标方法

2.1 编写接口和实现类(目标对象)
UserService接口
UserServiceImpl实现类

2.2 配置目标类,将目标类交给spring IoC容器管理

<context:component-scan base-package="sourcecode.ioc" />

一、使用XML实现

1.1 实现步骤

1 编写通知(增强类,一个普通的类)

public class MyAdvice {
    public MyAdvice() {
    }

    public void after() {
        System.out.println("最终通知...");
    }
}

2 配置通知,将通知类交给spring IoC容器管理
将第一步编写的通知类交给Spring 管理。

	<!-- 配置通知类 -->
	<bean id="myAdvice" class="com.kkb.spring.aop.advice.MyAdvice"></bean>

3 配置AOP 切面
切入点和通知是切面。method 是 ref 对应类的增强方法。
使用:

	<aop:config>
		<aop:aspect ref="myAdvice">
			<!-- method属性:对应myAdvice类的after方法, 要增强的方法名 -->
			<!-- pointcut属性:指定切入点类AspectJExpressionPointcut, 需要配置切入点表达式 -->
			<aop:after method="after"
				pointcut="execution(* *..*.*ServiceImpl.*(..))" />
		</aop:aspect>
	</aop:config>

详细说明:

			<aop:before method="before"
				pointcut="execution(* *..*.*ServiceImpl.*(..))" />
			<!-- 对应 AspectJAfterAdvice 通知类 -->
			<!-- AspectJAfterAdvice构造参数: -->
			<!-- 第一个构造参数:method属性指定的方法的Method对象 -->
			<!-- 第二个构造参数:pointcut属性对应的切入点对象 -->
			<!-- 第三个构造参数:产生myAdvice类的工厂对象 -->

			<!-- 第二个参数的作用:确定哪些类运行时,调用AspectJAfterAdvice的after方法 -->
			<!-- 第一个参数和第三个参数的作用:一旦调用after方法的时候,就会调用method.invoke(obj,args) -->

Spring AOP实现的配置(了解)

		<!-- 这是使用的Spring AOP实现 -->
		 <aop:advisor advice-ref="" pointcut=""/> 

1.2 切入点表达式

1.2.1 表达式格式:

execution([修饰符] 返回值类型 包名.类名.方法名(参数))

1.2.2 表达式格式说明

1、 execution:必须要
2、修饰符:可省略
3、返回值类型:必须要,但是可以使用*通配符
4、包名 :

  • 多级包之间使用.分割
  • 包名可以使用代替,多级包名可以使用多个*代替 、
  • 如果想省略中间的包名可以使用 ..

5、 类名、方法名:
可以使用*

6、 参数:
如果有多个参数,可以使用 ..代替

示例:

"execution(void cn.spring.dao.UserDaoIml.insert())" />

1.3 通知类型

通知类型(五种):前置通知、后置通知、最终通知、环绕通知、异常抛出通知。

1.3.1 前置通知

  • 执行时机:目标对象方法之前执行通知
  • 配置文件:<aop:before method=“before” pointcut-ref=“myPointcut”/>
  • 应用场景:方法开始时可以进行校验

1.3.2 后置通知

  • 执行时机:目标对象方法之后执行通知,有异常则不执行了 *
  • 配置文件:<aop:after-returning method=“afterReturning” pointcut-ref=“myPointcut”/>
  • 应用场景:可以修改方法的返回值

1.3.3 最终通知

  • 执行时机:目标对象方法之后执行通知,有没有异常都会执行
  • 配置文件:<aop:after method=“after” pointcut-ref=“myPointcut”/>
  • 应用场景:例如像释放资源

1.3.4 环绕通知

  • 执行时机:目标对象方法之前和之后都会执行。
  • 配置文件:<aop:around method=“around” pointcut-ref=“myPointcut”/>
  • 应用场景:事务、统计代码执行时机

1.3.5 异常通知

  • 执行时机:在抛出异常后通知
  • 配置文件:<aop:after-throwing method=" afterThrowing " pointcut- ref=“myPointcut”/>
  • 应用场景:包装异常

二、使用注解和XML实现

2.1 编写切面类

注意不是通知类,因为该类中可以指定切入点. 使用@Aspect 注解, @Component
5种通知类型分别对应不同的注解,before, around, …

@Component("myAspect")
@Aspect
public class MyAspect {
    private static final String pcut = "execution(* *..*.*ServiceImpl.*(..))";

	// @Before(value="execution(* *..*.*ServiceImpl.*(..))")
	@Before(value="MyAspect.fn()")
	public void before() {
		System.out.println("这是注解方式的前置通知");
	}
	@AfterReturning(pcut)
	public void after() {
		System.out.println("这是注解方式的后置通知");
	}

	// 通过Pointcut 定义了一个通用的切入点
	@Pointcut("execution(* *..*.*ServiceImpl.*(..))")
	public void fn() {}
	
	// Around 通知 需要指定joinPoint, 调用目标方法
	@Around(value="execution(* *..*.*ServiceImpl.*(..))")
	public Object aroundAdvice(ProceedingJoinPoint joinPoint){
		object rtValue = null;
		try {
			// 前置
			object[] args = joinPoint.getArgs();
			joinPoint.proceed(args);
			// 后置
		} catch (java.lang.Exception e) {
			e.printStackTrace();
		} finally {
		}
	}
}

配置切面类,component-scan 用来扫描 component

<context:component-scan base-package="com.spring"/>

开启AOP自动代理

	<!-- 开启aspectj的自动代理,用于AOP的注解方式 -->
	<aop:aspectj-autoproxy/>

2.2 环绕通知 Around 注解需注意指定 joinPoint

2.3 定义通用切入点

使用@PointCut注解在切面类中定义一个通用的切入点,其他通知可以引用该切入点。

三、使用纯注解

对未使用注解的部分使用等价注解替换

@Configuration 
@ComponentScan(basePackages="com.kkb") 
@EnableAspectJAutoProxy 
public class SpringConfiguration {
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring-MyBatis整合是一种常见的开发方式,其中spring-mybatis.xml文件是用来配置Spring和MyBatis框架集成的配置文件。在这个文件中,我们会定义数据源、事务管理器、扫描Mapper接口等配置信息。同时,我们还需要将MyBatis的SqlSessionFactory注入到Spring容器中,以便其他组件可以使用它来执行数据库操作。以下是一个简单的spring-mybatis.xml文件示例: ```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" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClass}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.example.entity" /> <property name="mapperLocations" value="classpath*:com/example/mapper/*.xml" /> </bean> <!-- 配置Mapper扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启Spring的注解功能 --> <context:annotation-config /> <!-- 开启SpringAOP功能 --> <aop:aspectj-autoproxy /> <!-- 开启Spring的事务管理功能 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans> ``` 在这个配置文件中,我们使用Spring的注解功能、AOP功能和事务管理功能,以及MyBatis的Mapper扫描和SqlSessionFactory配置。其中,数据源、事务管理器和Mapper扫描的配置信息需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值