Spring 中基于 AOP 的 XML操作方式
2023-09-14 09:03:08 时间
Spring 框架的一个关键组件是面向方面的编程(AOP)框架。面向方面的编程需要把程序逻辑分解成不同的部分称为所谓的关注点。跨一个应用程序的多个点的功能被称为横切关注点,这些横切关注点在概念上独立于应用程序的业务逻辑。有各种各样的常见的很好的方面的例子,如日志记录、审计、声明式事务、安全性和缓存等。
一些AOP术语:
(1) aspect:一个模块具有一组提供横切需求的 APIs。例如,一个日志模块为了记录日志将被 AOP 方面调用。应用程序可以拥有任意数量的方面,这取决于需求。
(2) joint point:相当于ABAP类里可以被增强的method
(3) advice:这是实际行动之前或之后执行的方法。这是在程序执行期间通过 Spring AOP 框架实际被调用的代码。相当于ABAP的pre/post/overwrite exit.
(4) weaving: Weaving 把方面连接到其它的应用程序类型或者对象上,并创建一个被通知的对象。这些可以在编译时,类加载时和运行时完成。
为了使用 aop 命名空间标签,需要导入 spring-aop j架构,如下所述:
Beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!-- bean definition & AOP specific configuration -->
</beans>
看个具体的例子:
(1) 新建一个Logging.java:
public class Logging {
/**
* This is the method which I would like to execute
* before a selected method execution.
*/
public void beforeAdvice(){
System.out.println("Going to setup student profile.");
}
/**
* This is the method which I would like to execute
* after a selected method execution.
*/
public void afterAdvice(){
System.out.println("Student profile has been setup.");
}
/**
* This is the method which I would like to execute
* when any method returns.
*/
public void afterReturningAdvice(Object retVal){
System.out.println("Returning:" + retVal.toString() );
}
/**
* This is the method which I would like to execute
* if there is an exception raised.
*/
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("There has been an exception: " + ex.toString());
}
}
Beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:config>
<aop:aspect id="log" ref="logging">
<aop:pointcut id="selectAll"
expression="execution(* com.tutorialspoint.*.*(..))"/>
<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
<aop:after pointcut-ref="selectAll" method="afterAdvice"/>
<aop:after-returning pointcut-ref="selectAll"
returning="retVal"
method="afterReturningAdvice"/>
<aop:after-throwing pointcut-ref="selectAll"
throwing="ex"
method="AfterThrowingAdvice"/>
</aop:aspect>
</aop:config>
<!-- Definition for student bean -->
<bean id="student" class="com.sap.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean>
<!-- Definition for logging aspect -->
<bean id="logging" class="com.sap.Logging"/>
</beans>
MainApp.java:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
try {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) context.getBean("student");
student.getName();
student.getAge();
student.printThrowException();
}
catch (Exception e) {
System.out.println("Jerry ends here!");
}
}
}
输出:
Jul 26, 2020 11:13:45 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b80350b: startup date [Sun Jul 26 11:13:45 CST 2020]; root of context hierarchy
Jul 26, 2020 11:13:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Going to setup student profile.
Name : Zara
Student profile has been setup.
Returning:Zara
Going to setup student profile.
Age : 11
Student profile has been setup.
Returning:11
Going to setup student profile.
Exception raised
Student profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
Jerry ends here!
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关文章
- spring学习笔记(9)AOP基本概念
- Spring系列.容器扩展点
- Spring之AOP二
- 使用JDBCTemplate实现与Spring结合,方法公用 ——共用实现类(BaseImpl)
- 18-spring学习-AOP深入操作
- Spring AOP源码分析(一)AOP介绍和aspectj、SpringAOP入门
- spring配置文件头部配置解析
- spring boot:用swagger3生成接口文档,支持全局通用参数(swagger 3.0.0 / spring boot 2.3.2)
- Spring Cloud Alibaba与Spring Boot、Spring Cloud之间不得不说的版本关系
- Spring DI依赖注入讲解
- 不一样的视角来学习Spring源码之AOP---中
- Spring中基于注解@AspectJ的AOP实现
- Spring 基于设值函数的依赖注入
- SpringMVC访问WEB-INF下的jsp解决方案&Spring Boot集成使用jsp
- Spring事务管理—aop:pointcut expression 常见切入点表达式及事物说明
- Spring学习总结(三)——Spring实现AOP的多种方式
- spring+websocket综合(springMVC+spring+MyBatis这是SSM框架和websocket集成技术)
- Spring之AOP源码理解,Spring4.3.12.RELEASE版本
- Spring 常用注解(IOC/AOP/MVC)
- [spring学习]10、AOP思想提出
- [spring学习]11、AOP使用,注解&xml配置
- Spring Cloud 学习笔记(3 / 3)