spring总结
使用spring实现AOP
【重点】使用AOP,必须导入一个依赖包
org.aspectj aspectjweaver 1.9.5 1 2 3 4 5 6 方式一:使用spring的API接口 创建一个UserService接口和UserServiceImplpublic 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
@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
@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();
}
- 结果
[外链图片转存中…(img-vTrYcgMm-1616230952977)]
方式三:使用注解实现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
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
结果