Spring AOP面向切面编程

配置.xml文件
概念
1、什么是面向切面编程?

切面能帮助我们模块化关注点。简而言之,横切关注点可以被描述为影响应用多处的功能。

项目开发过程中有些功能需要在多个模块中使用,比如:安全,事务等,那么就可以将安全和事务作为一个模块贯穿所有用到他们的模块中,类似一个切面,那么不需要每个模块自己单独实现。

横切关注点可以被模块化为特殊的类,这些类被称为切面(aspect)。

好处:

首先现在每个关注点都集中于一个地方,而不是分散在多处代码中;

其次,服务模块更简洁,因为它们只包含主要关注点(或核心功能)的代码,而次要关注点的代码被转移到切面中了。
在这里插入图片描述

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

	<context:component-scan base-package="com.offcn.service,com.offcn.dao" />
	<bean class="com.offcn.aop.AopError"></bean>
	<!--开启spring注解-->
	<aop:aspectj-autoproxy/>
</beans>

在Aop实体类写注解

@Aspect
public class Aop {
}
分别

在各个在方法名上写注解

@AfterThrowing(pointcut = "execution(* com.offcn.service.UserService.*(..))",throwing = "e")
@After("execution(* com.offcn.service.UserService.*(..))")
@Before("execution(* com.offcn.service.UserService.*(..))")
@Around("execution(* com.offcn.service.UserService.*(..))")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值