下面是Spring5的配置文件,该文件配置了Spring5 AOP中的通知,请分析代码并回答后面的问题。(10分) applicationContext.xml: ① <?xml version="1.0" encoding="UTF-8"?> ② <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "https://www.springframework.org/dtd/spring-beans-2.0.dtd" [ <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">]> ③ <beans> ④ <bean id="BeforeAdvice" ⑤ class="adviceexample.GettingBeforeAdvice"></bean> ⑥ <bean id="AfterAdvice" class="adviceexample.GettingAfterAdvice"></bean> ⑦ <bean id="proxy" ⑧ class="org.springframework.aop.framework.ProxyFactoryBean"> ⑨ <property name="proxyInterfaces" ⑩ value="adviceexample.Reception"></property> ⑪ <property name="interceptorNames"> ⑫ <list> ⑬ <value>BeforeAdvice</value> ⑭ <value>AfterAdvice</value> ⑮ </list> ⑯ </property> ⑰ <property name="target" ref="target"></property> ⑱ </bean> ⑲ <bean id="target" class="adviceexample.ConcreteReception"></bean> ⑳ </beans> (1)上面文件配置了的两种通知是用来做什么的(4分) (2)上面文件配置了代理工厂对象proxy,该对象有三个属性,依次是代理接口,通知和目标对象,其中,代理接口是什么?(2分) (3)分析上面代码说明AOP主要包含哪些内容?(4分)
时间: 2023-06-15 13:07:34 浏览: 120
(1)上面文件配置了的两种通知分别是BeforeAdvice和AfterAdvice,分别用来在目标方法执行前和执行后进行一些操作,例如记录日志、权限控制等。(4分)
(2)代理接口是Reception,使用ProxyFactoryBean创建的代理对象会实现该接口。(2分)
(3)AOP主要包含以下内容:目标对象、切面、通知、切点、代理对象等。其中,切面是由通知和切点组成的,通知包括BeforeAdvice、AfterAdvice等不同类型,切点是用来匹配目标方法的规则。在Spring AOP中,代理对象是通过ProxyFactoryBean创建的,而目标对象则是被AOP增强的原始对象。AOP的核心是在目标方法执行前、后或异常抛出时插入相应的通知,从而实现特定的功能增强。 (4分)
相关问题
什么是spring aop,请详细的告诉我并给我一个demo(注解版和配置文件版),面试需要从哪方面下手,举个例子给我。
Spring AOP是Spring框架中的一个模块,它是基于AOP(面向切面编程)的思想,提供了在程序运行期间动态地将代码织入到应用程序中的能力,从而实现了对业务逻辑的解耦。
Spring AOP的核心概念是切面(Aspect)、连接点(Join Point)、切点(Pointcut)和通知(Advice)。其中,切面是横切关注点的模块化,连接点是程序执行过程中的一个特定点,切点则定义了连接点的集合,通知则是在连接点上执行的操作。
下面给出注解版和配置文件版的demo代码:
1. 注解版
切面类:
```java
@Aspect
@Component
public class LogAspect {
@Pointcut("execution(* com.example.demo.service.*.*(..))")
public void log() {}
@Before("log()")
public void before(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Before " + methodName);
}
@AfterReturning("log()")
public void afterReturning(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("AfterReturning " + methodName);
}
@AfterThrowing(pointcut = "log()", throwing = "ex")
public void afterThrowing(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName();
System.out.println("AfterThrowing " + methodName + " " + ex.getMessage());
}
@After("log()")
public void after(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("After " + methodName);
}
}
```
服务类:
```java
@Service
public class UserService {
public void addUser(String name) {
System.out.println("addUser " + name);
}
public void deleteUser(String name) throws Exception {
throw new Exception("deleteUser error");
}
}
```
2. 配置文件版
配置文件:
```xml
<aop:config>
<aop:aspect ref="logAspect">
<aop:pointcut id="log"
expression="execution(* com.example.demo.service.*.*(..))" />
<aop:before pointcut-ref="log" method="before" />
<aop:after-returning pointcut-ref="log" method="afterReturning" />
<aop:after-throwing pointcut-ref="log" method="afterThrowing"
throwing="ex" />
<aop:after pointcut-ref="log" method="after" />
</aop:aspect>
</aop:config>
<bean id="logAspect" class="com.example.demo.aspect.LogAspect" />
<bean id="userService" class="com.example.demo.service.UserService" />
```
服务类:
```java
public class UserService {
public void addUser(String name) {
System.out.println("addUser " + name);
}
public void deleteUser(String name) throws Exception {
throw new Exception("deleteUser error");
}
}
```
在面试中,你可以从以下几个方面入手:
1. AOP的基本概念和作用
2. Spring AOP的实现方式
3. 切面、连接点、切点和通知的含义和使用方法
4. AOP的应用场景和优缺点
举例来说,可以问面试者如何使用AOP实现日志记录、权限控制、事务管理等功能,同时也可以让面试者分析AOP的缺点和使用不当可能带来的问题。
简述Spring AOP 的概念及使用方法,并列举2种应用
Spring AOP(面向切面编程)是Spring框架中的一个重要特性,它通过将横切关注点与核心业务逻辑分离,实现了对系统的解耦和模块化。AOP通过在程序运行期间动态地将代码切入到对象的方法中,从而在不修改源代码的情况下,实现对方法的增强。
使用Spring AOP,我们可以通过以下步骤来实现:
1. 定义切面(Aspect):切面是一个类,其中包含了一些通知(Advice)和切点(Pointcut)。通知定义了在方法执行前、执行后、抛出异常等时机要执行的逻辑,而切点定义了哪些方法应该被增强。
2. 配置切面:在Spring配置文件中,我们需要声明和配置切面。
3. 启用AOP:在配置文件中启用AOP,以便Spring能够自动识别和处理切面。
4. 应用AOP:在需要应用AOP的地方,使用Spring提供的AOP支持,将切面织入到目标对象中。
两种常见的Spring AOP应用场景包括:
1. 日志记录:通过AOP可以在方法执行前后记录方法的调用信息、参数、返回值等,方便进行系统运行日志的记录和分析。
2. 事务管理:通过AOP可以在方法执行前开启事务,在方法执行后提交或回滚事务,实现对数据库操作的事务管理,保证数据的一致性。
以上是Spring AOP的概念及使用方法的简述,以及两种常见的应用场景。在实际开发中,我们可以根据具体需求结合AOP的特性,实现更多的功能和扩展。