Spring之AOP日志管理配置示例(注解方式)

发表于:2015-8-13 10:27

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:FC WORLD!!!    来源:51Testing软件测试网采编

分享:
  applicationContext.xml配置
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
4     xsi:schemaLocation="
5             http://www.springframework.org/schema/beans
6             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7             http://www.springframework.org/schema/aop
8             http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
9             http://www.springframework.org/schema/context
10             http://www.springframework.org/schema/context/spring-context-3.0.xsd">
11     <aop:aspectj-autoproxy/>
12     <!-- 注入日志类 LogAspect-->
13     <bean id="logAspect" class="com.test.LogAspect"/>
14     <!-- 注入业务类 -->
15     <bean name="test" class="com.test.TestServiceImpl"></bean>
16     <!-- AOP配置 -->
17     <aop:config>
18         <aop:aspect id="simpleApect" ref="logAspect">
19             <!-- 配置切入点 -->
20             <aop:pointcut expression="execution(* com.test..*(..))" id="simplePointCut"/>
21             <!-- 配置通知 -->
22             <aop:before method="beforeShow" pointcut-ref="simplePointCut"/>
23             <aop:after method="afterShow" pointcut-ref="simplePointCut"/>
24             <!-- <aop:after-returning method="afterReturn" pointcut-ref="simplePointCut"/> -->
25             <aop:after-throwing method="doAfterThrowing" pointcut-ref="simplePointCut" throwing="ex"/>
26         </aop:aspect>
27     </aop:config>
28 </beans>
  测试主入口
1 package com.test;
2
3
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.springframework.context.support.ClassPathXmlApplicationContext;
8
9 public class TestMain{
10         public static void main(String[] args) {
11             //获取BeanFactory
12             ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
13             TestService test = (TestService)factory.getBean("test");
14             List l = new ArrayList();
15             l.add("param1");
16             l.add("param2");
17             l.add("param3");
18             //调用测试方法
19             test.test("ok",1,l);
20         }
21 }
  日志管理有两种方式:一种是注解方式,另外一种是xml配置方式(后续研究...)。主要用来与项目业务逻辑层进行分割,就是所谓的解耦和,便于日后管理及维护。
  测试时,需要导入Spring的包有:Spring基础包、Spring AOP相关包。
22/2<12
价值129的会员专享直播免费赠送,添加微信领取听课名额哦~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计

法律顾问:上海漕溪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2023
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号