spring aop入门(序列三)

前两部分了解了aop的来源以及aop的基本术语,本节通过一个简单的例子来入门下aop。

这里通过一个简单的"性能评估"的例子来表述下:

/*性能监控子方法*/

public class MethodPerformance{


private long begin;
private long end;

private Object serviceMethod;

public MethodPerformance(Object serviceMethod){
this.serviceMethod=serviceMethod;
this.begin=System.currentTimeMillis();
}

public void printPerformance(){
end=System.currentTimeMillis();
long elapse=end-begin;
System.out.println("正在执行"+serviceMethod+",耗费时间为:"+elapse);
}
}

/*性能监控主方法*/

public class PerformanceMonitor {

private static ThreadLocal<MethodPerformance> performanceRecord = new ThreadLocal<MethodPerformance>();

public void begin(Object method){
System.out.println("begin...");
MethodPerformance mp = new MethodPerformance(method);
performanceRecord.set(mp);
}

public void end(){
System.out.println("end....");
MethodPerformance mp = performanceRecord.get();
mp.printPerformance();
}
}


/*逻辑处理方法,类似论坛删除文章..*/

/*接口*/

public interface ForumServiceI {

public void removeTopic(int topicId);

public void removeForum(int forumId);

}

/*实现类*/

public class ForumServiceImpl implements ForumServiceI {


@Override
public void removeTopic(int topicId) {
System.out.println("模拟删除Topic记录"+topicId);
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}


@Override
public void removeForum(int forumId){
System.out.println("模拟删除forum记录"+forumId);
try {
Thread.currentThread().sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
};
}


}

这里通过spring xml配置aop的方式来进行配置

/*xml配置文档*/

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="performanceMonitor" class="com.proxy.springproxy.monitor.PerformanceMonitor"></bean>

<bean id="forumService" class="com.proxy.springproxy.operation.ForumServiceImpl"></bean>

<aop:config>
<aop:aspect id="adviceOperation" ref="performanceMonitor">
<aop:before  method="begin"
pointcut="execution(* com.proxy.springproxy.operation.ForumServiceImpl.*(..)) and args(method)" 
/>
            <aop:after method="end"
              pointcut="execution(* com.proxy.springproxy.operation.ForumServiceImpl.*(..))" /> 
</aop:aspect>
</aop:config>
</beans>

/**测试类*/

public class TestForumService{

public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ForumServiceI fs=(ForumServiceI)ac.getBean("forumService");
fs.removeForum(10);
fs.removeTopic(1024);
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值