下面是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>
时间: 2024-04-26 08:22:02 浏览: 14
该配置文件配置了一个代理对象和两个通知(BeforeAdvice和AfterAdvice)。具体分析如下:
(1) 在第4行到第6行配置了BeforeAdvice通知的Bean,id为“BeforeAdvice”,class为“adviceexample.GettingBeforeAdvice”。
(2) 在第7行到第9行配置了AfterAdvice通知的Bean,id为“AfterAdvice”,class为“adviceexample.GettingAfterAdvice”。
(3) 在第10行到第19行配置了一个代理对象的Bean,id为“proxy”,class为“org.springframework.aop.framework.ProxyFactoryBean”。该代理对象实现了“adviceexample.Reception”接口。
(4) 在第13行配置了代理对象需要实现的接口“adviceexample.Reception”。
(5) 在第15行到第18行配置了代理对象需要织入的通知列表,包括BeforeAdvice和AfterAdvice。
(6) 在第20行到第22行配置了代理对象要代理的目标对象,id为“target”,class为“adviceexample.ConcreteReception”。
因此,该配置文件实现了一个代理对象,在代理对象的方法执行前后分别执行BeforeAdvice和AfterAdvice通知。该代理对象实现了Reception接口,且代理的目标对象是ConcreteReception。
相关问题
什么是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的特性,实现更多的功能和扩展。