Spring aop 1.初步理解

如果有疑问和不太懂的,可以提问。我也是初学者,大家互相交流,互相进步
1.什么是aop,为什么要用aop
aop是一种切面编程。为什么要用,是因为实际开发中对方给你的一般是class文件。而class文件是机器编译好的文件不能更改,有时根据市场需求要添加一些功能时需要aop。
什么是aop,其原理是什么
列如银行在取款时发现需要一个打印取款凭证的功能。我们就需要在控制层调用业务层的取款方法后加入一个打印取款信息的方法。这时aop会一刀切下去,把打印取款信息的方法塞进去。如图下所示:
在这里插入图片描述
2.简单的aop配置
先配置aopjar包


junit
junit
4.12
test

	<!-- 引入spring依赖 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>4.3.3.RELEASE</version>
	</dependency>

	<!--spring aop + aspectj -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aop</artifactId>
		<version>4.3.3.RELEASE</version>
	</dependency>
	<!-- <dependency> -->
	<!-- <groupId>org.aspectj</groupId> -->
	<!-- <artifactId>aspectjrt</artifactId> -->
	<!-- <version>1.8.10</version> -->
	<!-- </dependency> -->
	<dependency>
		<groupId>org.aspectj</groupId>
		<artifactId>aspectjweaver</artifactId>
		<version>1.8.10</version>
	</dependency>
</dependencies>

再开启aop注解扫描
注意先取掉beans 的两个引号

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

<context:component-scan base-package=“com.vp.springtest.test”></context:component-scan>

<!-- 开启aop注解扫描 -->
<aop:aspectj-autoproxy />

‘<’/beans>

3.aop注解

  • 一个类 :用spring的切面类来实现
  • @author Administrator
    *@Aspect 定义此类为切面类(代理类)
    /
    @Component
    @Aspect
    public class StudentProxy {
    //记录日志是我们想加的功能,并且是在添加学生功能saveStudent之后@After自动调用
    // execution执行或者调用
    @Before("execution (
    com.vp.springtest.test.StudentService.saveStudent(…))")
    public void log() {
    System.out.println(“张三于2020-2-18 14:24分注册了帐号…”);
    }
    }
    @Component//先实列化
    @Aspect//再定义切面类
    @After//在调用StudentService的saveStudent方法调用。
    @Before//在调用StudentService的saveStudent方法调用。
    还有三个注解需要了解自行搜索。
    如果有疑问和不太懂的,可以提问。我也是初学者,大家互相交流,互相进步
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值