SpringAOP案例(一)XML方式

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingfeng812/article/details/20695589



第一步要导入spring架构包:



一、XML方式

1. TestAspect:切面类

[java]  view plain copy
  1. package com.spring.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4. import org.aspectj.lang.ProceedingJoinPoint;  
  5.   
  6. public class TestAspect {  
  7.   
  8.     public void doAfter(JoinPoint jp) {  
  9.         System.out.println("log Ending method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());  
  10.     }  
  11.   
  12.     public Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
  13.         long time = System.currentTimeMillis();  
  14.         Object retVal = pjp.proceed();  
  15.         time = System.currentTimeMillis() - time;  
  16.         System.out.println("process time: " + time + " ms");  
  17.         return retVal;  
  18.     }  
  19.   
  20.     public void doBefore(JoinPoint jp) {  
  21.         System.out.println("log Begining method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());  
  22.     }  
  23.   
  24.     public void doThrowing(JoinPoint jp, Throwable ex) {  
  25.         System.out.println("method " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + " throw exception");  
  26.         System.out.println(ex.getMessage());  
  27.     }  
  28. }  

2. AServiceImpl:目标对象

[java]  view plain copy
  1. package com.spring.service;  
  2.   
  3. // 使用jdk动态代理  
  4. public class AServiceImpl implements AService {  
  5.   
  6.     public void barA() {  
  7.         System.out.println("AServiceImpl.barA()");  
  8.     }  
  9.   
  10.     public void fooA(String _msg) {  
  11.         System.out.println("AServiceImpl.fooA(msg:" + _msg + ")");  
  12.     }  
  13. }  

3. BServiceImpl:目标对象

[java]  view plain copy
  1. package com.spring.service;  
  2.   
  3. // 使用cglib  
  4. public class BServiceImpl {  
  5.   
  6.     public void barB(String _msg, int _type) {  
  7.         System.out.println("BServiceImpl.barB(msg:" + _msg + " type:" + _type + ")");  
  8.         if (_type == 1)  
  9.             throw new IllegalArgumentException("测试异常");  
  10.     }  
  11.   
  12.     public void fooB() {  
  13.         System.out.println("BServiceImpl.fooB()");  
  14.     }  
  15.   
  16. 
    
  17. ApplicationContext:Spring配置文件   文件默认在src目录下

    [html]  view plain copy
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:aop="http://www.springframework.org/schema/aop"  
    5.     xmlns:context="http://www.springframework.org/schema/context"  
    6.     xmlns:tx="http://www.springframework.org/schema/tx"  
    7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
    8.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
    9.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
    10.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">  
    11.     <aop:config>  
    12.         <aop:aspect id="TestAspect" ref="aspectBean">  
    13.             <!--配置com.spring.service包下所有类或接口的所有方法-->  
    14.             <aop:pointcut id="businessService" expression="execution(* com.spring.service.*.*(..))" />  
    15.             <aop:before pointcut-ref="businessService" method="doBefore"/>  
    16.             <aop:after pointcut-ref="businessService" method="doAfter"/>  
    17.             <aop:around pointcut-ref="businessService" method="doAround"/>  
    18.             <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>  
    19.         </aop:aspect>  
    20.     </aop:config>  
    21.       
    22.     <bean id="aspectBean" class="com.spring.aop.TestAspect" />  
    23.     <bean id="aService" class="com.spring.service.AServiceImpl"></bean>  
    24.     <bean id="bService" class="com.spring.service.BServiceImpl"></bean>  
    25. </beans>  
    package com.spring.main;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.spring.service.AService;
    //测试类
    public class SpringAOPTest {
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
    				"ApplicationContext.xml");
    		//对于实现接口的类,要得到它的接口的bean而不是接口的实现类
    		AService aServiceImpl = (AService) ctx.getBean("aService");
    		aServiceImpl.barA();
    		aServiceImpl.fooA("这是springaop测试类!");
    		// AService b=(AService) ctx.getBean("bService");
    		// b.barA();
    		// b.fooA("这是没有实现接口的类!");
    
    	}
    }


相关文章
|
6月前
|
存储 Java 文件存储
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
1413 1
|
6月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
251 0
|
11月前
|
XML Java 数据格式
Spring从入门到入土(xml配置文件的基础使用方式)
本文详细介绍了Spring框架中XML配置文件的使用方法,包括读取配置文件、创建带参数的构造对象、使用工厂方法和静态方法创建对象、对象生命周期管理以及单例和多例模式的测试。
499 7
Spring从入门到入土(xml配置文件的基础使用方式)
|
9月前
|
XML Java 数据格式
【SpringFramework】Spring IoC-基于XML的实现
本文主要讲解SpringFramework中IoC和DI相关概念,及基于XML的实现方式。
186 69
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
XML Java 数据格式
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
95 1
|
9月前
|
XML Java 数据格式
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
本文介绍了在使用Spring框架时,如何通过创建`applicationContext.xml`配置文件来管理对象。首先,在resources目录下新建XML配置文件,并通过IDEA自动生成部分配置。为完善配置,特别是添加AOP支持,可以通过IDEA的Live Templates功能自定义XML模板。具体步骤包括:连续按两次Shift搜索Live Templates,配置模板内容,输入特定前缀(如spring)并按Tab键即可快速生成完整的Spring配置文件。这样可以大大提高开发效率,减少重复工作。
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
|
9月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
233 6
|
12月前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
607 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
12月前
|
XML Java 数据格式
spring复习02,xml配置管理bean
详细讲解了Spring框架中基于XML配置文件管理bean的各种方式,包括获取bean、依赖注入、特殊值处理、属性赋值、集合类型处理、p命名空间、bean作用域及生命周期和自动装配。
spring复习02,xml配置管理bean

热门文章

最新文章