2021-04-05

spring总结

使用spring实现AOP

【重点】使用AOP,必须导入一个依赖包

org.aspectj aspectjweaver 1.9.5 1 2 3 4 5 6 方式一:使用spring的API接口 创建一个UserService接口和UserServiceImpl

public interface UserService {
void add();
void delete();
void update();
void query();
}
public class UserServiceImpl implements UserService {
public void add() {
System.out.println(“增加了一个用户”);
}
public void delete() {
System.out.println(“删除了一个用户”);
}
public void update() {
System.out.println(“修改了一个用户”);
}
public void query() {
System.out.println(“查询了一个用户”);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
编写通知类

//前置通知
public class BeforeLog implements MethodBeforeAdvice {
//method:需要执行的目标对象的方法
//objects:参数
//o:目标对象
public void before(Method method, Object[] objects, Object o) throws
Throwable {
System.out.println(o.getClass().getName() + “的” + method.getName()

  • “被执行了”);
    }
    }
    //后置通知
    public class AfterLog implements AfterReturningAdvice {
    //returnValue:返回值
    public void afterReturning(Object returnValue, Method method, Object[]
    args, Object target) throws Throwable {
    System.out.println(“执行力” + method.getName() + “返回结果为:” +
    returnValue);
    }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
编写spring配置文件,注册Bean和配置aop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 测试

@Test
public void test() {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext(“applicationContext.xml”);
UserService userService = (UserService) context.getBean(“userService”);
userService.add();
}
1
2
3
4
5
6
7
结果

方式二:自定义类实现AOP【主要是切面实现】
编写自定义切面类

//自定义切点类
public class DiyPointCut {
public void before() {
System.out.println(“方法执行前”);
}
public void after() {
System.out.println(“方法执行后”);
}
}
1
2
3
4
5
6
7
8
9
在spring配置文件里配置AOP

1 2 3 4 5 6 7 8 9 10 11 测试

@Test
public void test() {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext(“applicationContext.xml”);
UserService userService = (UserService) context.getBean(“userService”);
userService.add1();
}
1
2
3
4
5
6
7
结果

方式三:使用注解实现AOP
自定义切面类

@Aspect //标注这个类是一个切面
@Component
public class AnnotationPointCut {
@Pointcut(“execution(* com.caians.service.UserServiceImpl.*(…))”)
private void pointcut() {
}
@Before(“pointcut()”)
public void before() {
System.out.println(“方法执行前”);
}
@After(“pointcut()”)
public void after() {
System.out.println(“方法执行后”);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
编写spring配置文件,开启注解扫描,开启AOP注解方式

<context:component-scan base-package=“com.caians.*”/>

aop:aspectj-autoproxy/
1
2
3
4
5
测试

public class MyTest {
@Test
public void test() {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext(“applicationContext.xml”);
UserService userService = (UserService)
context.getBean(“userService”);
userService.add();
}
}
1
2
3
4
5
6
7
8
9
10
结果

plicationContext context = new
ClassPathXmlApplicationContext(“applicationContext.xml”);
UserService userService = (UserService) context.getBean(“userService”);
userService.add1();
}

  1. 结果

[外链图片转存中…(img-vTrYcgMm-1616230952977)]

方式三:使用注解实现AOP

  1. 自定义切面类
@Aspect //标注这个类是一个切面
@Component
public class AnnotationPointCut {
@Pointcut("execution(* com.caians.service.UserServiceImpl.*(..))")
private void pointcut() {
}
@Before("pointcut()")
public void before() {
System.out.println("方法执行前");
}
@After("pointcut()")
public void after() {
System.out.println("方法执行后");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
编写spring配置文件,开启注解扫描,开启AOP注解方式

<!-- 方式三 -->
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.caians.*"/>
<!-- 开启AOP注解方式 -->
<aop:aspectj-autoproxy/>
1
2
3
4
5
测试

public class MyTest {
@Test
public void test() {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService)
context.getBean("userService");
userService.add();
}
}
1
2
3
4
5
6
7
8
9
10
结果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值