题目要求
有VisitService接口和它的实现类,作为切入类
public interface VisitService {
void visit(String str) throws Exception;
}
public class VisitServiceImpl implements VisitService {
@Override
public void visit(String str) throws Exception {
System.out.println(str);
if (!str.equalsIgnoreCase("agree")) {
throw new Exception("非法访问");
}
}
}
现有如下测试代码
public class SpringAOPTest {
private VisitService visitService;
@Before
public void init() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
visitService = context.getBean(VisitService.class);
}
@Test
public void testVisit() {
try {
visitService.visit("agree");
} catch (Exception e) {
e.printStackTrace();
}
try {
visitService.visit("ok");
} catch (Exception e) {
e.printStackTrace();
}
}
}
要求:利用SpringAOP实现:
- 最终通知打印输出“输入成功”
- 后置通知打印输出“请求成功,欢迎”
- 异常通知打印输出“请求失败,拒绝”
实验解答一 xml配置方式
根据题目中已有的代码,不难知道,题目考察AOP的切点与切面的织入操作。现要完成的操作是:新建一个切面类,并完成切面类与切点类的织入。
下面我们按步骤来完成实验内容。
1. 在pom.xml中导入完成AOP的依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--spring官方推荐用的aop依赖包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
2. 创建一个切面类VisitAspect,在类中按题目定义after、afterRunning、afterThrowing方法
public class VisitAspect {
public void after() {
System.out.println("输入成功");
}
public void afterRunning() {
System.out.println("请求成功,欢迎");
}
public void afterThrowing() {
System.out.println("请求失败,拒绝");
}
}
3. 在Spring配置文件中完成切入点与切面的织入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<bean id="visitAspect" class="com.yjx.service.impl.VisitServiceImpl"></bean>
<bean id="aspect" class="com.yjx.aspect.VisitAspect"></bean>
<aop:config>
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* *.*.service.impl.VisitServiceImpl.visit(*))"/>
<!--切面-->
<aop:aspect ref="aspect">
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:after-returning method="afterRunning" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
4. 测试结果
实验解法二 注解方式
注解方式与xml方式原理是属于一样的,只是 切入点与切面的织入是在切面类中完成,而xml中主要是完成注解的扫包操作。
1. VisitAspect类有很大的不同
@Component
@Aspect
public class VisitAspect {
@Pointcut("execution(* *.*.service.impl.VisitServiceImpl.visit(*))")
public void pt() {}
@After("pt()")
public void after() {
System.out.println("输入成功");
}
@AfterReturning("pt()")
public void afterRunning() {
System.out.println("请求成功,欢迎");
}
@AfterThrowing("pt()")
public void afterThrowing() {
System.out.println("请求失败,拒绝");
}
}
2. 而配置类中只有简短的两行扫包配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.yjx"/>
<aop:aspectj-autoproxy/>
</beans>
3. 测试结果与xml中的一致
源码下载
需要参考源码的同学们可以按照需求点击下载,这里提供xml、注解两个版本的源码下载,资源是免积分的,用于学习交流使用,请勿用于其他途径。
实验五:SpringAOP注解开发方式完成的小实验
实验五:SpringAOPXML配置文件完成的小实验