iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >java框架中spring aop两种配置方式分别是什么
  • 877
分享到

java框架中spring aop两种配置方式分别是什么

2023-06-17 07:06:55 877人浏览 独家记忆
摘要

这篇文章给大家介绍java框架中spring aop两种配置方式分别是什么,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。注解配置AOP注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@

这篇文章给大家介绍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

package com.bjsxt.model;  public class User {     private String username;     private String passWord;     public String getUsername() {         return username;     }     public void setUsername(String username) {         this.username = username;     }     public String getPassword() {         return password;     }     public void setPassword(String password) {         this.password = password;     } }
 package com.bjsxt.dao; import com.bjsxt.model.User;   public interface UserDAO {     public void save(User user); }

实现接口:

package com.bjsxt.dao.impl;  import org.springframework.stereotype.Component;  import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User;  @Component("u") public class UserDAOImpl implements UserDAO {      public void save(User user) {                  System.out.println("user save11d!");          //抛异常     }  }

操作类:

package com.bjsxt.service; import javax.annotation.Resource;  import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component;  import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User;   @Component("userService") public class UserService {          private UserDAO userDAO;            public void init() {         System.out.println("init");     }          public void add(User user) {         userDAO.save(user);     }     public UserDAO getUserDAO() {         return userDAO;     }          @Resource(name="u")     public void setUserDAO( UserDAO userDAO) {         this.userDAO = userDAO;     }         public void destroy() {         System.out.println("destroy");     } }

加入aop

package com.bjsxt.aop;  import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component;  @Aspect @Component public class LogInterceptor {     @Pointcut("execution(public * com.bjsxt.service..*.add(..))")     public void myMethod(){};               @Before("myMethod()")     public void before() {         System.out.println("method staet");     }      @After("myMethod()")     public void after() {         System.out.println("method after");     }      @AfterReturning("execution(public * com.bjsxt.dao..*.*(..))")     public void AfterReturning() {         System.out.println("method AfterReturning");     }      @AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))")     public void AfterThrowing() {         System.out.println("method AfterThrowing");     }  }

配置文件

<?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:context="http://www.springframework.org/schema/context"        xmlns:aop="http://www.springframework.org/schema/aop"        xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-2.5.xsd            http://www.springframework.org/schema/aop                        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd              "><!-- 要添加***2行 -->                 <context:annotation-config />     <context:component-scan base-package="com.bjsxt"/>  <!-- 自动扫描 -->     <aop:aspectj-autoproxy/>  <!-- 要添加本行 --> </beans>

测试类:

package com.bjsxt.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;  import com.bjsxt.model.User;  //Dependency Injection //Inverse of Control public class UserServiceTest {      @Test     public void testAdd() throws Exception {         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");                           UserService service = (UserService)ctx.getBean("userService");         System.out.println(service.getClass());         service.add(new User());         System.out.println("###");                  ctx.destroy();              }  }

结果:

class com.bjsxt.service.UserService$$EnhancerByCGLIB$$7b201784
method staet
user save11d!
method AfterReturning
method after
###

注意:

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

第二种:xml配置aop

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

<?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:context="http://www.springframework.org/schema/context"        xmlns:aop="http://www.springframework.org/schema/aop"        xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-2.5.xsd            http://www.springframework.org/schema/aop                        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd              "><!-- 要添加***2行 -->                 <context:annotation-config />     <context:component-scan base-package="com.bjsxt"/>     <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>     <aop:config>         <aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))"          id="servicePointcut"/>         <aop:aspect id="logAspect" ref="logInterceptor">             <aop:before method="before"  pointcut-ref="servicePointcut" />         </aop:aspect>              </aop:config> </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这种标签了。

关于java框架中spring aop两种配置方式分别是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

--结束END--

本文标题: java框架中spring aop两种配置方式分别是什么

本文链接: https://www.lsjlt.com/news/287584.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • java框架中spring aop两种配置方式分别是什么
    这篇文章给大家介绍java框架中spring aop两种配置方式分别是什么,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。注解配置AOP注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@...
    99+
    2023-06-17
  • javascript中内置了两种json方法分别是什么
    这篇文章将为大家详细讲解有关javascript中内置了两种json方法分别是什么,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。javascript中内置了两种json方法:1、“JSON.parse()...
    99+
    2023-06-14
  • Spring中接口注入的三种方式分别是什么
    本篇文章为大家展示了Spring中接口注入的三种方式分别是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Spring中 接口注入的三种方式,上面的代码中,ClassA依赖于Interf...
    99+
    2023-06-17
  • Java实现生产者消费者的两种方式分别是什么
    本篇文章给大家分享的是有关Java实现生产者消费者的两种方式分别是什么,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。   我在8年前去面试程序员的时候,一个...
    99+
    2023-06-03
  • Spring Security中的四种权限控制方式分别是什么
    本篇文章为大家展示了Spring Security中的四种权限控制方式分别是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Spring Security 中对于...
    99+
    2022-10-19
  • Java编程中的索引技巧:Spring框架的实现方式是什么?
    在Java编程中,索引是一个重要的概念,它可以帮助我们快速地访问和查找数据。在Java编程中,常用的索引包括数组索引和Map索引。而在Spring框架中,索引也被广泛地应用,用于快速地访问Bean对象、注入依赖关系和处理请求等。 本文将介...
    99+
    2023-06-30
    索引 spring 编程算法
  • Java中接口和数组的运用:Spring框架的实现方式是什么?
    在Java编程中,接口和数组是两种非常重要的概念。其中,接口是一种定义了方法和常量的抽象类型,而数组则是一种可以存储多个相同数据类型的容器。在实际的编程中,这两个概念经常被用来设计和实现各种复杂的程序。在本文中,我们将重点讨论Spring...
    99+
    2023-08-09
    接口 spring 数组
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作