Spring入门篇 学习笔记

advisor 就像一个小的自包含的方面,只有一个 advice

切面自身通过一个 bean 表示,并且必须实现某个 advice 接口,同时 advisor 也可以很好的利用 AspectJ 的切入点表达式

Spring 通过配置文件中 aop:advisor 元素支持 advisor,实际使用中,大多数情况下它会和 transactional advice 配合使用

为了定义一个 advisor 的优先级以便让 advice 可以有序,可以使用 order 属性来定义 advisor 的顺序

示例

添加切面:

public class ConcurrentOperationExecutor implements Ordered {

	private static final int DEFAULT_MAX_RETRIES = 2;

	private int maxRetries = DEFAULT_MAX_RETRIES;

	private int order = 1;

	public void setMaxRetries(int maxRetries) {
this.maxRetries = maxRetries;
} public int getOrder() {
return this.order;
} public void setOrder(int order) {
this.order = order;
} public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable {
int numAttempts = 0;
PessimisticLockingFailureException lockFailureException;
do {
numAttempts++;
System.out.println("Try times : " + numAttempts);
try {
return pjp.proceed();
} catch (PessimisticLockingFailureException ex) {
lockFailureException = ex;
}
} while (numAttempts <= this.maxRetries);
System.out.println("Try error : " + numAttempts);
throw lockFailureException;
}
}

添加类:

@Service
public class InvokeService { public void invoke() {
System.out.println("InvokeService ......");
} public void invokeException() {
throw new PessimisticLockingFailureException("");
} }

添加配置:

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.karonda.aop.schema"></context:component-scan> <aop:config>
<aop:aspect id="concurrentOperationRetry" ref="concurrentOperationExecutor">
<aop:pointcut id="idempotentOperation"
expression="execution(* com.karonda.aop.schema.advisors.service.*.*(..)) " />
<aop:around pointcut-ref="idempotentOperation" method="doConcurrentOperation" />
</aop:aspect>
</aop:config> <bean id="concurrentOperationExecutor" class="com.karonda.aop.schema.advisors.ConcurrentOperationExecutor">
<property name="maxRetries" value="3" />
<property name="order" value="100" />
</bean> </beans>

添加测试类:

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPSchemaAdvisors extends UnitTestBase { public TestAOPSchemaAdvisors() {
super("classpath:spring-aop-schema-advisors.xml");
} @Test
public void testSave() {
InvokeService service = super.getBean("invokeService");
service.invoke(); System.out.println();
service.invokeException();
} }

源码:learning-spring

最新文章

  1. shell导出mysql部分数据
  2. Shell重定向文件描述符
  3. 为VS集成IL环境
  4. centos下安装mycat
  5. MyEclipse新建Maven工程
  6. thinkphp 常见问题
  7. 黑魔法__attribute__((cleanup))
  8. git切换远程
  9. MouseOver/MouseOut vs MouseEnter/MouseLeave
  10. c#通过反射获取自定义属性
  11. elasticsearch地理空间操作简单操作
  12. don\'t have permission access on this server听语音
  13. python中类中的@property
  14. HDU 6432(不连续环排列 ~)
  15. JAVA方法调用中的解析与分派
  16. Building a Keras + deep learning REST API(三部曲之一)
  17. Number的Util
  18. emacs 图解
  19. mycat 不得不说的缘分
  20. 演示-JQuery关系选择器

热门文章

  1. 20175310 《Java程序设计》第7周学习总结
  2. pip install报错Can&#39;t roll back cryptography; was not uninstalled
  3. ZOJ - 2423-Fractal
  4. C# 实现实时网速
  5. .Net Core 在 Linux-Centos上的部署实战教程(三)
  6. 渗透测试_利用Burp爆破用户名与密码
  7. 微信小程序页面跳转方法总结
  8. PHP实用代码片段(四)
  9. UITableView加载数据,没有数据,没有网络界面处理
  10. 归并排序Python 实现