JavaConfig:替换 aop:advisor 和 tx:advice

2024-03-04

我想知道是否可以将这段 xml 配置映射到 Spring JavaConfig:

<?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" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop.xsd
                      http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx.xsd"
  default-autowire="byName">

  <aop:config>
     <aop:pointcut id="serviceAnnotatedClass" expression="@within(org.springframework.stereotype.Service)" />
     <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut-ref="serviceAnnotatedClass" order="20" />
  </aop:config>

  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="get*" read-only="true" />
      <tx:method name="find*" read-only="true" />
      <tx:method name="load*" read-only="true" />
      <tx:method name="is*" read-only="true" />
      <tx:method name="ownTransaction*" propagation="REQUIRES_NEW" rollback-for="Exception" />
      <tx:method name="*" rollback-for="Exception" />
    </tx:attributes>
  </tx:advice>

</beans>

到目前为止我想出了如何替换aop:切入点 with

<aop:advisor id="managerTx" advice-ref="txAdvice" 
pointcut="com.myapp.configuration.AspectConfig.serviceAnnotatedClass()" order="20"/>

and

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectConfig
{

  @Pointcut("@within(org.springframework.stereotype.Service)")
  public void serviceAnnotatedClass() {}
}

有什么提示如何替换其余的吗?


如果你根本不想使用任何xml,那么你可以为方面创建一个单独的Java配置类

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.myAspects")
public class AspectConfig {
    //Here you can define Aspect beans or just use @ComponentScan (as above)
    //to scan the @Aspect annotation in com.myAspects package
}

并在您的主 AppConfig 类中导入上述配置类

@Configuration
@EnableWebMvc
@Import({ AspectConfig.class })
@ComponentScan(basePackages = { "pkg1", "pkg2", "pkg3" })
public class AppConfiguration extends WebMvcConfigurationSupport {
    //Other configuration beans or methods
}

现在创建您的方面 bean

import com.myAspects;
@Component
@Aspect
public class LoggingAspect {

    @Before("execution(* com.service.*.*(..))")
    public void logBefore(){
        System.out.println("before advice called");
    } 

    @After("execution(* com.service.*.*(..))")
    public void logAfter(){
        System.out.println("after advice called");
    } 

}

您可以将切入点与建议注释一起使用,如上所示。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JavaConfig:替换 aop:advisor 和 tx:advice 的相关文章

随机推荐

  • 如何使用 Meteor 执行常见的 FB 操作?

    使用 Meteor 执行常见 Facebook 操作需要执行哪些步骤accounts facebook包裹 我正在尝试获取好友列表 在墙上发布并最终执行其他操作 但我不确定如何继续 更新 对meteor 0 6 0略有修改 您需要使用 AP
  • 来自 Pandas 混淆矩阵的散景热图

    熊猫怎么能DataFrame显示为散景热图 https docs bokeh org en latest docs user guide categorical html heat maps https docs bokeh org en
  • 具有自定义视图和图像视图的 MKAnnotationView

    在我的地图应用程序中 我想显示一个带有图像的彩色背景圆圈 而不是显示图钉 背景圆圈的颜色 下图中的绿色阴影 是动态的 它将如下图所示 我创建了 TCircleView 它在 drawRect 中绘制颜色为了显示类似的注释 我创建了 TCir
  • 如何调试 NodeJS 中的套接字挂起错误?

    我收到以下错误 events js 48 throw arguments 1 Unhandled error event Error socket hang up at createHangUpError http js 1091 15 a
  • nuget.org:无法加载服务索引

    这实际上不是一个重复这个问题 https stackoverflow com questions 41185443 nuget connection attempt failed unable to load the service ind
  • Python OrderedDict 按日期排序

    我正在尝试使用 OrderedDict Raymond Hettingersversion http code activestate com recipes 576693 对于 pre2 7 Python 其中我的键是日期 但是它没有正确
  • 默认字典(无)

    我希望有一本包含一组状态转换的字典 我认为我可以使用 states defaultdict None 来做到这一点 但它没有按我的预期工作 例如 states defaultdict None if new state 1 states S
  • jQuery Tablesorter 的日期排序问题

    我正在尝试对具有类似列的表进行排序2009 12 17 23 59 59 0 我正在使用下面的应用排序 document ready function dataTable tablesorter 但它不适用于 yyyy mm dd 格式的日
  • 比较 numpy.save 和 h5py 的速度时如何获得一致的结果?

    我正在尝试比较两种工具的速度效率 这两种工具可以节省 2 GB 的空间numpy array到磁盘到文件 numpy save and h5py create dataset 注意 这只是第一个测试 我必须处理的真实情况是数千个大小在 1
  • R data.table 使用 lapply 创建自定义函数来创建和重新分配多个变量

    我有以下几行代码 DT flag T temp haz 1 5 DT temp na locf temp na rm FALSE pid DT agedays 61 haz 1 5 1 temp 我需要将其转换为一个函数 以便它可以处理一系
  • 哪种报告技术? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 哪种报告技术最适合最佳情况 产品类型 我现在在想3技术 嵌入式报告 Crystal Reports M
  • codeigniter + 密码需要字母和数字

    我想使用表单验证来要求同时包含字母和数字字符的密码 到目前为止 这是我想到的 this gt form validation gt set rules password Password required matches passconf
  • Rust musl Docker 镜像找不到 Cargo

    我正在尝试让 Rust 在 Docker 中运行 以将其用于 32 位 musl 构建 自从我更新它以使用新的 URL 来拉 rustup 后 我在使用 bash 交互运行容器时遇到了这个问题 root 2c3549fe3169 sampl
  • Sqlite:自己行中的小计又名“汇总”

    我正在寻找一种方法来模拟其他基于 sql 的 dbms 中可用的 汇总 需要明确的是 我知道如何获得运行小计 这是not我在追求什么 我也知道我可以通过编程来做到这一点 但是 如果可以使用一个 或几个 sql 语句来完成 我宁愿这样做 例如
  • Eclipse - 期间发生内部错误:“计算启动按钮工具提示”

    我早些时候启动了 Eclipse 来记下一个片段 当我尝试运行一个新项目时 遇到了以下错误 期间发生内部错误 计算启动按钮工具提示 当我将鼠标悬停在顶部栏中的运行图标上时 或者右键单击资源管理器窗口中的类并选择任何运行 调试选项时 就会显示
  • scipy 中 line_search 的示例

    我正在寻找使用的例子scipy optimize line search 我不太明白这个函数如何与多变量函数一起使用 我写了一个简单的例子 import scipy as sp import scipy optimize def test
  • 如何在 Seaside 回调中访问 jQuery 事件对象

    基本上 我想将以下内容翻译成 Seaside Smalltalk myDiv bind click function e console log e 除此之外我不想console log事件 但在我的 ajax 回调中访问它 最有希望的方法
  • 返回上一页适用于模拟器,不适用于 iOS 设备 jquerymobile

    我正在用这种方法更改页面 mobile changePage Preview html transition slide role page changeHash true 这就是我的预览页面的样子 div class prew div c
  • 如何将字典写入文件?

    我有一个 FileHelper 类 其中实现了 3 个方法 其工作是将字典内容写入文件 这些方法是 func storeDictionary dictionary Dictionary
  • JavaConfig:替换 aop:advisor 和 tx:advice

    我想知道是否可以将这段 xml 配置映射到 Spring JavaConfig