从头认识Spring-3.4 简单的AOP日志实现-扩展增加检查订单功能,以便记录并检测输入的参数
2016-02-22 22:27
976 查看
这一章节我们再上一个章节的基础上加上一个检查订单功能1.domain蛋糕类:
烤炉类:
厨师类:
日志类:
上面扩展了一个检测订单的功能,以便记录并检测输入的参数。
配置类:(我们这里使用基于java的配置)
我们上面引入了joinpoint这个接口,然后从接口里面得到想要的参数,再通过转型从而得到相应的输入参数。
2.测试类:
3.配置文件(重点)
配置文件这里,我们不需要特定的cake参数,直接就是把类放到expression里面即可。
测试输出:blueberryCheeseCake
jack make blueberryCheeseCake
总结:这一章节主要介绍一个简单的AOP日志实现,扩展增加检查订单功能,以便记录并检测输入的参数。
目录:/article/3641905.html 我的github:https://github.com/raylee2015/my_new_spring
package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } }
烤炉类:
package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4; public class Oven { private String name = ""; @Override public String toString() { return name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
厨师类:
package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4; public class Chief { private static int index = 0; public static int getIndex() { return index; } public static void setIndex(int index) { Chief.index = index; } private Cake cake = null; private final int id = index++; private String name = ""; private Oven oven = null; public Cake getCake() { return cake; } public int getId() { return id; } public String getName() { return name; } public Oven getOven() { return oven; } public void setCake(Cake cake) { this.cake = cake; } public void setName(String name) { this.name = name; } public void setOven(Oven oven) { this.oven = oven; } public void makeOneCake(Cake cake) { System.out.println(getName() + " make " + cake.getName()); } }
日志类:
package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class Log { public void washOven() { System.out.println("washOven,logging....."); } public void checkOrder(JoinPoint joinpoint) { for (Object item : joinpoint.getArgs()) { if (item instanceof Cake) { Cake cake = (Cake) item; System.out.println(cake.getName()); } } } public void prepare() { System.out.println("prepare,logging....."); } public void after() { System.out.println("after someting to do,logging....."); } public void around(ProceedingJoinPoint joinPoint) throws Throwable { washOven(); prepare(); long startTime = System.currentTimeMillis(); joinPoint.proceed(); long endTime = System.currentTimeMillis(); System.out.println("use time:" + (endTime - startTime)); after(); } }
上面扩展了一个检测订单的功能,以便记录并检测输入的参数。
配置类:(我们这里使用基于java的配置)
package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SpringBeans { @Bean public Chief jack() { Chief chief = new Chief(); chief.setName("jack"); chief.setOven(oven()); chief.setCake(cake()); return chief; } @Bean public Oven oven() { Oven oven = new Oven(); oven.setName("big oven"); return oven; } @Bean public Cake cake() { Cake cake = new Cake(); cake.setName("blueberryCheeseCake"); return cake; } @Bean public Log log() { return new Log(); } }在日志类这里我们需要注意的是:
我们上面引入了joinpoint这个接口,然后从接口里面得到想要的参数,再通过转型从而得到相应的输入参数。
2.测试类:
package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/com/raylee/my_new_spring/my_new_spring/ch03/topic_1_4/ApplicationContext-test.xml" }) public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief() { Chief jack = (Chief) applicationContext.getBean(Chief.class); Cake cake = applicationContext.getBean(Cake.class); cake.setName("blueberryCheeseCake"); jack.makeOneCake(cake); } }
3.配置文件(重点)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd 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"> <context:component-scan base-package="com.raylee.my_new_spring.my_new_spring.ch03.topic_1_3" /> <aop:config> <aop:aspect ref="log"> <aop:pointcut expression="execution(* com.raylee.my_new_spring.my_new_spring.ch03.topic_1_3.Chief.*(..))" id="chiefPointCut" /> <aop:before method="checkOrder" pointcut-ref="chiefPointCut"/> </aop:aspect> </aop:config> </beans>
配置文件这里,我们不需要特定的cake参数,直接就是把类放到expression里面即可。
测试输出:blueberryCheeseCake
jack make blueberryCheeseCake
总结:这一章节主要介绍一个简单的AOP日志实现,扩展增加检查订单功能,以便记录并检测输入的参数。
目录:/article/3641905.html 我的github:https://github.com/raylee2015/my_new_spring
相关文章推荐
- struts quick start
- java注解理论概述
- Java中抽象类和接口的区别
- spring-HelloWorld
- java.util.List;与java.awt.List;的区别
- javax.servlet包报错和javax.servlet.jsp.JspException
- java注解实例
- java自动拆装箱,接口和抽象类
- java 创建文件和文件夹
- 编译型和解释型的区别&&Java从编译到执行的过程
- JAVA 内部静态类-避免内存泄漏的原因
- java中的静态变量
- java36.Socket通信------ServerSocket的建立与使用示例
- java 以字符为单位读取文件(3)
- springMVC-interceptors
- eclipse导入java工程不如直接导入java文件方便
- JAVA中关于set()和get()方法的理解及使用
- java 以字节为单位读取文件(2)
- Eclipse配置外部Maven
- JAVA_面向对象_笔记_01