本来是测试网上的例子,看着看着,觉得可以修改的简单一点,说干就干。
1,在Eclispe里面建立一个项目,随便,这叫context吧,使用jdk5的环境。
2,在项目里面加入log4j-1.2.8.jar,commons-logging-1.0.4.jar,cglib-nodep-2.1_3.jar,spring.jar(2.5),aspectweaver.jar,aspecjrt.jar。
3,为了能够在系统中使用log4j,在项目目录下加入log4j.properties,内容
- log4j.rootLogger=INFO,stdout
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d %5p (%F:%L) - %m%n
-
4,加入spring的配置文件exception_config.xml
开始的内容:
<!---->
<beans xmlns="http://www.springframework.org/schema/beans"></beans>
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: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/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd">
- </beans>
5,做一个程序接口
java 代码
- public interface LogicInterface {
-
-
-
-
- void doInsert(String name);
-
-
-
-
- void doUpdate(String name);
-
-
-
-
- void doDelete(String name);
-
- }
-
6,做两个接口的实现
java 代码
- package phoenics.context.code;
-
-
-
-
-
- public class Logic1 implements LogicInterface{
- private int j=0;
-
-
-
-
- public void doInsert(String name) {
- System.out.println("Logic1...执行具体负责新增的业务逻辑…");
-
- for (int i = 0; i < 100000000; i++) {
- j++;
-
- }
- }
-
-
-
-
- public void doUpdate(String name) {
- System.out.println("Logic1...执行具体负责修改的业务逻辑…");
- for (int i = 0; i < 200000000; i++) {
- j++;
-
- }
- }
-
-
-
-
- public void doDelete(String name) {
- System.out.println("Logic1...执行具体负责删除的业务逻辑…");
- for (int i = 0; i < 300000000; i++) {
- j++;
- i = i / 0;
- }
- }
- }
-
-
- package phoenics.context.code;
-
-
-
-
-
- public class Logic2 {
- int j=0;
-
- public void doInsert(String name){
- System.out.println("Logic2...执行具体负责新增的业务逻辑…");
- for (int i = 0; i < 100000000; i++) {
- j++;
-
- }
- }
-
-
- public void doUpdate(String name){
- System.out.println("Logic2...执行具体负责修改的业务逻辑…");
- for (int i = 0; i < 200000000; i++) {
- j++;
-
- }
- }
-
-
- public void doDelete(String name){
- System.out.println("Logic2...执行具体负责删除的业务逻辑…");
- for (int i = 0; i < 300000000; i++) {
- j++;
- i = i / 0;
- }
- }
-
- }
-
-
7,log类,增加两个method,一个用于INFO,另一个用于error
java 代码
- package phoenics.context.code;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
-
-
-
-
-
- public class Logger {
- private static Log log = LogFactory.getLog(Logger.class);
- public void entry_info(String message) {
- log.info(message);
- }
- public void entry_error(String message) {
- log.error(message);
- }
- }
-
8,好了,现在才增加aop相关的类,先看代码
java 代码
- package phoenics.context.code;
-
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.AfterThrowing;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Pointcut;
-
-
-
-
-
- @Aspect
- public class LogAspect {
-
-
-
-
- private Logger logger = new Logger();
-
- @Pointcut("execution(public * *(..))")
- public void method1(){}
-
- @Pointcut("within(phoenics.context.code.Logic2)")
- public void method2(){}
-
-
-
-
-
-
- @AfterThrowing(
- pointcut="method1()",
- throwing="ex"
- )
- public void doExceptionActions(JoinPoint jp,Throwable ex ){
- String aa=jp.getSignature().getName();
- logger.entry_error(jp.getArgs()[0] + " run " + aa + " Throw...." + ex);
- }
-
-
-
-
-
-
-
- @Around("method2()")
- public Object doTimerActions(ProceedingJoinPoint p) throws Throwable{
- long procTime = System.currentTimeMillis();
- logger.entry_info(p.getArgs()[0] + " run Starting " + p.getSignature().getName() + " method");
- try {
- Object result = p.proceed();
- return result;
- } finally {
-
- procTime = System.currentTimeMillis() - procTime;
- logger.entry_info(p.getArgs()[0] + " run " + p.getSignature().getName() + " method end");
- logger.entry_info( "run "+p.getSignature().getName() +" and run method in " + procTime + "ms");
- }
- }
- }
-
这个里面有两个拦截的方法一个是doExceptionActions,拦截所有的发生异常的method,另一个doTimerActions,根据连接点看,它只会拦截phoenics.context.code.Logic2中的方法。
9,修改spring的配置文件exception_config.xml
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: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/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd">
-
- <aop:aspectj-autoproxy proxy-target-class="true"/>
- <bean id="logic1" class="phoenics.context.code.Logic1"/>
- <bean id="LogBean1" class="phoenics.context.code.LogAspect"/>
- <bean id="logic2" class="phoenics.context.code.Logic2"/>
-
-
-
- beans>
<bean class="phoenics.context.code.Logic2" id="logic2"></bean>
10,编写测试类
java 代码
- package phoenics.context.test;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- import phoenics.context.code.Logic2;
- import phoenics.context.code.LogicInterface;
-
-
-
-
- public class TestAop {
- public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
-
- ApplicationContext actx=new FileSystemXmlApplicationContext("exception_config.xml");
- LogicInterface logic1 = (LogicInterface)actx.getBean("logic1");
- Logic2 logic2 = (Logic2)actx.getBean("logic2");
-
- try {
- System.out.println("logic1 starting...");
- logic1.doInsert("张三");
- logic1.doUpdate("李四");
- logic1.doDelete("王五");
- } catch (Exception ex) {}
- try {
- System.out.println("logic2 starting...");
- logic2.doInsert("张三");
- logic2.doUpdate("李四");
- logic2.doDelete("王五");
- } catch (Exception ex) {}
- }
- }
-
11,看到的结果:
logic1 starting...
Logic1...执行具体负责新增的业务逻辑…
Logic1...执行具体负责修改的业务逻辑…
Logic1...执行具体负责删除的业务逻辑…
2007-10-26 20:31:53,390 ERROR (Logger.java:30) - 王五 run doDelete Throw....java.lang.ArithmeticException: / by zero
logic2 starting...
2007-10-26 20:31:53,421 INFO (Logger.java:27) - 张三 run Starting doInsert method
Logic2...执行具体负责新增的业务逻辑…
2007-10-26 20:31:53,625 INFO (Logger.java:27) - 张三 run doInsert method end
2007-10-26 20:31:53,625 INFO (Logger.java:27) - run doInsert and run method in 204ms
2007-10-26 20:31:53,625 INFO (Logger.java:27) - 李四 run Starting doUpdate method
Logic2...执行具体负责修改的业务逻辑…
2007-10-26 20:31:54,078 INFO (Logger.java:27) - 李四 run doUpdate method end
2007-10-26 20:31:54,078 INFO (Logger.java:27) - run doUpdate and run method in 453ms
2007-10-26 20:31:54,093 INFO (Logger.java:27) - 王五 run Starting doDelete method
Logic2...执行具体负责删除的业务逻辑…
2007-10-26 20:31:54,093 INFO (Logger.java:27) - 王五 run doDelete method end
2007-10-26 20:31:54,093 INFO (Logger.java:27) - run doDelete and run method in 0ms
2007-10-26 20:31:54,093 ERROR (Logger.java:30) - 王五 run doDelete Throw....java.lang.ArithmeticException: / by zero