Loading [MathJax]/jax/output/HTML-CSS/jax.js

java框架篇---spring aop两种配置方式

简介:




java框架篇---spring aop两种配置方式


第一种:注解配置AOP

注解配置AOP(使用 AspectJ 类库实现的),大致分为三步:
1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).
2. 开发需要被拦截的类。
3. 将切面配置到xml中,当然,我们也可以使用自动扫描Bean的方式。这样的话,那就交由Spring AoP容器管理。

另外需要引用 aspectJ 的 jar 包: aspectjweaver.jar aspectjrt.jar

实例:

User.java

 
 
  1. package com.bjsxt.model; 
  2.  
  3. public class User { 
  4.     private String username; 
  5.     private String password; 
  6.     public String getUsername() { 
  7.         return username; 
  8.     } 
  9.     public void setUsername(String username) { 
  10.         this.username = username; 
  11.     } 
  12.     public String getPassword() { 
  13.         return password; 
  14.     } 
  15.     public void setPassword(String password) { 
  16.         this.password = password; 
  17.     } 
  18. }
 
 
  1. /** 
  2. *接口类 
  3. */ 
  4. package com.bjsxt.dao; 
  5. import com.bjsxt.model.User; 
  6.  
  7.  
  8. public interface UserDAO { 
  9.     public void save(User user); 

实现接口:

 
 
  1. package com.bjsxt.dao.impl; 
  2.  
  3. import org.springframework.stereotype.Component; 
  4.  
  5. import com.bjsxt.dao.UserDAO; 
  6. import com.bjsxt.model.User; 
  7.  
  8. @Component("u") 
  9. public class UserDAOImpl implements UserDAO { 
  10.  
  11.     public void save(User user) { 
  12.          
  13.         System.out.println("user save11d!"); 
  14.         /*throw new RuntimeException("exception");*/ //抛异常 
  15.     } 
  16.  

操作类:

 
 
  1. package com.bjsxt.service; 
  2. import javax.annotation.Resource; 
  3.  
  4. import org.springframework.beans.factory.annotation.Autowired; 
  5. import org.springframework.beans.factory.annotation.Qualifier; 
  6. import org.springframework.stereotype.Component; 
  7.  
  8. import com.bjsxt.dao.UserDAO; 
  9. import com.bjsxt.model.User; 
  10.  
  11.  
  12. @Component("userService") 
  13. public class UserService { 
  14.      
  15.     private UserDAO userDAO;   
  16.      
  17.     public void init() { 
  18.         System.out.println("init"); 
  19.     } 
  20.      
  21.     public void add(User user) { 
  22.         userDAO.save(user); 
  23.     } 
  24.     public UserDAO getUserDAO() { 
  25.         return userDAO; 
  26.     } 
  27.      
  28.     @Resource(name="u") 
  29.     public void setUserDAO( UserDAO userDAO) { 
  30.         this.userDAO = userDAO; 
  31.     } 
  32.     
  33.     public void destroy() { 
  34.         System.out.println("destroy"); 
  35.     } 

加入aop

 
 
  1. package com.bjsxt.aop; 
  2.  
  3. import org.aspectj.lang.annotation.After; 
  4. import org.aspectj.lang.annotation.AfterReturning; 
  5. import org.aspectj.lang.annotation.AfterThrowing; 
  6. import org.aspectj.lang.annotation.Aspect; 
  7. import org.aspectj.lang.annotation.Before; 
  8. import org.aspectj.lang.annotation.Pointcut; 
  9. import org.springframework.stereotype.Component; 
  10.  
  11. @Aspect 
  12. @Component 
  13. public class LogInterceptor { 
  14.     @Pointcut("execution(public * com.bjsxt.service..*.add(..))") 
  15.     public void myMethod(){}; 
  16.      
  17.     /*@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))")*/ 
  18.     @Before("myMethod()") 
  19.     public void before() { 
  20.         System.out.println("method staet"); 
  21.     }  
  22.     @After("myMethod()") 
  23.     public void after() { 
  24.         System.out.println("method after"); 
  25.     }  
  26.     @AfterReturning("execution(public * com.bjsxt.dao..*.*(..))") 
  27.     public void AfterReturning() { 
  28.         System.out.println("method AfterReturning"); 
  29.     }  
  30.     @AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))") 
  31.     public void AfterThrowing() { 
  32.         System.out.println("method AfterThrowing"); 
  33.     }  

配置文件

 
 
  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:context="http://www.springframework.org/schema/context" 
  5.        xmlns:aop="http://www.springframework.org/schema/aop" 
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
  8.            http://www.springframework.org/schema/context 
  9.            http://www.springframework.org/schema/context/spring-context-2.5.xsd 
  10.            http://www.springframework.org/schema/aop             
  11.            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
  12.            "><!-- 要添加最后2行 --> 
  13.             
  14.     <context:annotation-config /> 
  15.     <context:component-scan base-package="com.bjsxt"/>  <!-- 自动扫描 --> 
  16.     <aop:aspectj-autoproxy/>  <!-- 要添加本行 --> 
  17. </beans> 

测试类:

 
 
  1. package com.bjsxt.service; 
  2. import org.junit.Test; 
  3. import org.springframework.context.ApplicationContext; 
  4. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  5.  
  6. import com.bjsxt.model.User; 
  7.  
  8. //Dependency Injection 
  9. //Inverse of Control 
  10. public class UserServiceTest { 
  11.  
  12.     @Test 
  13.     public void testAdd() throws Exception { 
  14.         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
  15.          
  16.          
  17.         UserService service = (UserService)ctx.getBean("userService"); 
  18.         System.out.println(service.getClass()); 
  19.         service.add(new User()); 
  20.         System.out.println("###"); 
  21.          
  22.         ctx.destroy(); 
  23.          
  24.     } 
  25.  

结果:

class com.bjsxt.service.UserServiceEnhancerByCGLIB7b201784
method staet
user save11d!
method AfterReturning
method after
###

注意:

@Aspect:意思是这个类为切面类
@Componet:因为作为切面类需要 Spring 管理起来,所以在初始化时就需要将这个类初始化加入 Spring 的管理;
@Befoe:切入点的逻辑(Advice)
execution…:切入点语法

第二种:xml配置aop

实例同上:只是配置文件不同

 
 
  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:context="http://www.springframework.org/schema/context" 
  5.        xmlns:aop="http://www.springframework.org/schema/aop" 
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
  8.            http://www.springframework.org/schema/context 
  9.            http://www.springframework.org/schema/context/spring-context-2.5.xsd 
  10.            http://www.springframework.org/schema/aop             
  11.            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
  12.            "><!-- 要添加最后2行 --> 
  13.             
  14.     <context:annotation-config /> 
  15.     <context:component-scan base-package="com.bjsxt"/> 
  16.     <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean> 
  17.     <aop:config> 
  18.         <aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))"  
  19.         id="servicePointcut"/> 
  20.         <aop:aspect id="logAspect" ref="logInterceptor"> 
  21.             <aop:before method="before"  pointcut-ref="servicePointcut" /> 
  22.         </aop:aspect> 
  23.          
  24.     </aop:config> 
  25. </beans> 

下面的<beans>是Spring的配置标签,beans里面几个重要的属性:

xmlns:

是默认的xml文档解析格式,即spring的beans。地址是http://www.springframework.org/schema/beans。

通过设置这个属性,所有在beans里面声明的属性,可以直接通过<>来使用,比如<bean>等等。

xmlns:xsi:

是xml需要遵守的规范,通过URL可以看到,是w3的统一规范,后面通过xsi:schemaLocation来定位所有的解析文件。

xmlns:aop:

这个是重点,是我们这里需要使用到的一些语义规范,与面向切面AOP相关。

xmlns:tx:

Spring中与事务相关的配置内容。

一个XML文件,只能声明一个默认的语义解析的规范。

例如上面的xml中就只有beans一个是默认的,其他的都需要通过特定的标签来使用,比如aop,它自己有很多的属性,如果要使用,前面就必须加上aop:xxx才可以。比如上面的aop:config。

类似的,如果默认的xmlns配置的是aop相关的语义解析规范,那么在xml中就可以直接写config这种标签了。


作者:返回主页 偶my耶

来源:51CTO

相关文章
|
16天前
|
人工智能 Java API
Java也能快速搭建AI应用?一文带你玩转Spring AI可落地性
Java语言凭借其成熟的生态与解决方案,特别是通过 Spring AI 框架,正迅速成为 AI 应用开发的新选择。本文将探讨如何利用 Spring AI Alibaba 构建在线聊天 AI 应用,并实现对其性能的全面可观测性。
|
1月前
|
存储 缓存 Java
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
109 3
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
|
11天前
|
人工智能 Java API
Java 也能快速搭建 AI 应用?一文带你玩转 Spring AI 可观测性
Java 也能快速搭建 AI 应用?一文带你玩转 Spring AI 可观测性
|
2月前
|
存储 NoSQL Java
使用Java和Spring Data构建数据访问层
本文介绍了如何使用 Java 和 Spring Data 构建数据访问层的完整过程。通过创建实体类、存储库接口、服务类和控制器类,实现了对数据库的基本操作。这种方法不仅简化了数据访问层的开发,还提高了代码的可维护性和可读性。通过合理使用 Spring Data 提供的功能,可以大幅提升开发效率。
79 21
|
1月前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
70 7
|
7天前
|
存储 监控 数据可视化
SaaS云计算技术的智慧工地源码,基于Java+Spring Cloud框架开发
智慧工地源码基于微服务+Java+Spring Cloud +UniApp +MySql架构,利用传感器、监控摄像头、AI、大数据等技术,实现施工现场的实时监测、数据分析与智能决策。平台涵盖人员、车辆、视频监控、施工质量、设备、环境和能耗管理七大维度,提供可视化管理、智能化报警、移动智能办公及分布计算存储等功能,全面提升工地的安全性、效率和质量。
|
13天前
|
人工智能 Java API
Java 也能快速搭建 AI 应用?一文带你玩转 Spring AI 可观测性
Java 也能快速搭建 AI 应用?一文带你玩转 Spring AI 可观测性
|
10月前
|
安全 Java Spring
Spring之Aop的底层原理
Spring之Aop的底层原理
|
10月前
|
设计模式 Java uml
Spring AOP 原理
Spring AOP 原理
50 0
|
7月前
|
Java
Spring5入门到实战------9、AOP基本概念、底层原理、JDK动态代理实现
这篇文章是Spring5框架的实战教程,深入讲解了AOP的基本概念、如何利用动态代理实现AOP,特别是通过JDK动态代理机制在不修改源代码的情况下为业务逻辑添加新功能,降低代码耦合度,并通过具体代码示例演示了JDK动态代理的实现过程。
Spring5入门到实战------9、AOP基本概念、底层原理、JDK动态代理实现