Aop:再不改变原代码的基础上,无入侵的增加额外功能。
方法一、通过定义通知类添加额外功能
1、引入aop的相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
2、定义原始类
package com.zhigang.service;
public interface UserService {
void hello();
}
public class UserServiceImpl implements UserService {
@Override
public void hello() {
System.out.println("service实现类的hello方法执行");
}
}
3、定义通知类(添加额外的功能)
public class UserBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("前置通知执行");
}
}
3.1 、通知类可选
前置通知:MethodBeforeAdvice
后置通知:AfterAdvice
后置通知:AfterReturningAdvice //有异常不执行,方法会因异常而结束,无返回值
异常通知:ThrowsAdvice
环绕通知:MethodInterceptor
4、定义bean标签
<bean id="a1" class="com.zhigang.advice.UserBeforeAdvice"/>
<bean id="usi" class="com.zhigang.service.impl.UserServiceImpl"/>
5、定义切点和切面
<aop:config>
<!-- 切点-->
<aop:pointcut id="usi1" expression="execution(* com.zhigang.service.UserService.hello())"/>
<!-- 切面-->
<aop:advisor advice-ref="a1" pointcut-ref="usi1"/>
</aop:config>
6、测试
public class TestAdvice1 {
@Test
public void test1(){
ApplicationContext ca = new ClassPathXmlApplicationContext("spring-context.xml");
UserService userService =(UserService) ca.getBean("usi");
// UserService userService = ca.getBean(UserService.class);
userService.hello();
}
}
7、运行结果展示
方法二、通过写Aspect类
1、引入Aop依赖 同方法一
2、编写原始类 同方法一
3、定义Aspect类
public class Aspect {
public void before(){
System.out.println("前置通知");
}
public void after(){
System.out.println("后置通知");
}
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前");
pjp.proceed();//目标方法的调用
System.out.println("环绕后");
}
}
4、定义bean标签
<bean id="a1" class="com.zhigang.advice.UserBeforeAdvice"/>
<bean id="usi" class="com.zhigang.service.impl.UserServiceImpl"/>
<bean id="aspect1" class="com.zhigang.advice.Aspect"/>
5、定义<aop:config>标签
<aop:config>
<!-- 切点-->
<aop:pointcut id="usi1" expression="execution(* com.zhigang.service.UserService.hello())"/>
<aop:aspect ref="aspect1">
<aop:before method="before" pointcut-ref="usi1"/>
<aop:after method="after" pointcut-ref="usi1"/>
<aop:around method="around" pointcut-ref="usi1"/>
</aop:aspect>
</aop:config>
6、测试
public class TestAdvice1 {
@Test
public void test1(){
ApplicationContext ca = new ClassPathXmlApplicationContext("spring-context.xml");
UserService userService =(UserService) ca.getBean("usi");
// UserService userService = ca.getBean(UserService.class);
userService.hello();
}
}
结果展示
方法三:注解开发
1、引入aop依赖 同方法一
2、定义原始类(service实现类用注解注入IOC容器中)
package com.zhigang.service;
public interface BookService {
void add();
void delete();
void update();
void getAll();
void getOne();
}
@Service("book")
public class BookServiceImpl implements BookService {
@Override
public void add() {
System.out.println("add");
}
@Override
public void delete() {
System.out.println("delete");
}
@Override
public void update() {
System.out.println("update");
}
@Override
public void getAll() {
System.out.println("getALl");
}
@Override
public void getOne() {
System.out.println("getOne");
}
}
3、定义Aspect类(用注解 注入IOC容器中)
@Component
@org.aspectj.lang.annotation.Aspect
public class Aspect {
@Pointcut("execution(* com.zhigang.service.BookService.*())")
public void m1(){}//方法名自定义
@Before("m1()")
public void before(){
System.out.println("前置通知");
}
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前");
pjp.proceed();//目标方法的调用
System.out.println("环绕后");
}
}
4、编写 配置文件
<context:component-scan base-package="com.zhigang"/>
<!--开启aop注解-->
<aop:aspectj-autoproxy/>
5、测试
public class TestBook {
@Test
public void test1(){
ApplicationContext ca = new ClassPathXmlApplicationContext("spring-context.xml");
BookService bean = ca.getBean(BookService.class);
// BookService book =(BookService) ca.getBean("book");
bean.add();
bean.delete();
// UserService userService = ca.getBean(UserService.class);
}
}
结果展示