ITKeyword,专注技术干货聚合推荐

注册 | 登录

从头认识Spring-3.7 简单的AOP日志实现(注解版)-增加检查订单功能

raylee2007 分享于 2016-02-26

推荐:从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展增加检查订单功能,以便记录并检测输入的参数

这一章节我们讨论一下扩展增加检查订单功能,以便记录并检测输入的参数。 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_8;

2019阿里云全部产品优惠券(新购或升级都可以使用,强烈推荐)
领取地址https://promotion.aliyun.com/ntms/yunparter/invite.html

这一章节我们使用注解来实现检查订单功能 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_7;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_7;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_7;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_7;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;@Aspectpublic class Log {

@Pointcut(value = "execution(* com.raylee.my_new_spring.my_new_spring.ch03.topic_1_7.Chief.*(..))

&& args(cake)")

public void chiefPointCut(Cake cake) {

}

public void washOven() {

System.out.println("washOven,logging.....");

}

@Before(value = "chiefPointCut(cake)")

public void checkOrder(Cake cake) {

System.out.println("is blueberryCheeseCake?" + cake.getName().equals("blueberryCheeseCake"));

}

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();

}} 这里我们需要注意的是,我们再配置切点的时候,表达式的表示方式有所不同,原来的“and” 变成了“&&” 配置类:(我们这里使用基于java的配置) package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_7;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class SpringBeans {

@Bean

public Chief jack() {

Chief chief = new Chief();

chief.setName("jack");

chief.setOven(oven());

chief.setCake(cake());

return ch

推荐:从头认识Spring-3.5 简单的AOP日志实现(注解版)-某方法之前的前后记录日志

这一章节我们使用注解来简单实现AOP日志。 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_5;public class Cake {

priva

ief;

}

@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();

}} 2.测试类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_7;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_7/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_7" />

<aop:aspectj-autoproxy /></beans> 测试输出: is blueberryCheeseCake?true jack make blueberryCheeseCake 总结:这一章节主要介绍一个简单的AOP日志实现。 目录:http://blog.csdn.net/raylee2007/article/details/50611627

我的github:https://github.com/raylee2015/my_new_spring

推荐:从头认识Spring-3.4 简单的AOP日志实现-扩展增加检查订单功能,以便记录并检测输入的参数

这一章节我们再上一个章节的基础上加上一个检查订单功能 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_4;public class

这一章节我们使用注解来实现检查订单功能 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_7;public class Cake { private String name = ""; public S

相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。