现在的位置: 首页 > 综合 > 正文

Spring_AOP_0100_Annotation入门

2016年09月01日 ⁄ 综合 ⁄ 共 4828字 ⁄ 字号 评论关闭

为了以后更好的实习,今天了解了一下spring的aop的基础知识,做了一些简单的测试,算入门aop吧,记录下来,方便以后查阅!

spring的版本2.5.6,由于目前aspectj的aop比spring做得更出色,而且spring也提倡直接使用aspectj的底层实现,所以我直接采用了spring提供的aspectj实现;首先基于Annotation的方式,因为这种方式简单一点:

假设场景:一个普通的服务员,能打招呼,提供服务,这个服务员不会使用礼貌用语;于是公司给她们进行了一次培训,好让她们每个人都有学会礼貌用语,显得都有素质,于是每个服务员都变成了有礼貌有素质的服务员。

第一步:加入jar包

不多讲,先加入几个基本的jar包,测试时少了jar包再添加进去就行

第二步:定义一个接口,对应文件: Waiter.java

package com.aop.interfaces;

/**
 * @author 紫竹
 * 模拟没有礼貌的服务生
 */
public interface Waiter {
	/**
	 * 打招呼
	 * @param name 顾客的姓名
	 */
	void greetTo(String name);
	
	/**
	 * 提供服务 
	 */
	void serverTo();
	
	/**
	 * 服务结束,bye
	 */
	void bye();
	
}

提供一个实现类:NaiveWaiter.java

package com.aop.impl;

import org.springframework.stereotype.Component;

import com.aop.interfaces.Waiter;

/**
 * @author 紫竹
 * Waiter的实现类,简单实现里面的方法,直接使用System打印出来
 */
@Component("naiveWaiter")
public class NaiveWaiter implements Waiter {

	@Override
	public void greetTo(String name) {
		System.out.println("greet to " + name + "....");
	}

	@Override
	public void serverTo() {
		System.out.println("server to ....");
	}

	@Override
	public void bye() {
		System.out.println("bye !");
	}

}

写一个测试类,测试spring的环境是否配置正确了

package com.aop.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.aop.interfaces.Waiter;

/**
 * @author 紫竹
 * 测试spring的环境
 */
public class Client {
	private ApplicationContext context = null;
	
	@Before
	public void before(){
		context = new ClassPathXmlApplicationContext("beans.xml");
	}
	
	@Test
	public void testNaiveWaiter(){
		Waiter w = (Waiter)context.getBean("naiveWaiter");
		String name = "Tom";
		w.greetTo(name);
		w.serverTo();
		w.bye();
	}
	
}

配置文件: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:context="http://www.springframework.org/schema/context"
	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-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

	<context:annotation-config />
    <context:component-scan base-package="com.aop" />
<!--	<aop:aspectj-autoproxy />-->

</beans>

输出结果:

greet to Tom....
server to ....
bye !
环境没有问题,继续测试;

公司给她们培训之后,于是他们每个人都有素质了,那么她们每个人都有素质了,在给顾客打招呼之前需要说hello,服务前后需要记录时间,结束后需要祝福语,have  a good day!每个人都需要添加这些礼貌用语,使用aop进行统一操作

有礼貌的服务生代理类:NiceWaiterProxy.java

package com.aop.proxy;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author 紫竹
 * 礼貌的服务生代理:在打招呼之前说一声hello,服务的时候记录时间,结束后say:good bye, have a good day
 */
@Aspect
@Component
public class NiceWaiterProxy {
	
	/**
	 * 测试execution语法:以下10种都能匹配,懂的,匹配范围依次增大
	 */
//	@Pointcut("execution(public void com.aop.impl.NaiveWaiter.greetTo(String))")
//	@Pointcut("execution(public void com.aop.impl.NaiveW aiter.greetTo(*))")
//	@Pointcut("execution(public void com.aop.impl.NaiveWaiter.greetTo(..))")
//	@Pointcut("execution(public * com.aop.impl.NaiveWaiter.greetTo(..))")
//	@Pointcut("execution(* com.aop.impl.NaiveWaiter.greetTo(..))")
//	@Pointcut("execution(* com.aop.impl.*.greetTo(..))")
//	@Pointcut("execution(* com.aop.*.*.greetTo(..))")
//	@Pointcut("execution(* com.aop.impl..greetTo(..))")//多级目录不确定的情况
//	@Pointcut("execution(* com.aop.impl..greet*(..))")
	@Pointcut("execution(* *..greet*(..))")
	public void greetTo(){};
	
	@Pointcut("execution(public void com.aop.impl.NaiveWaiter.server*(..))")
	public void server(){}
	
	@Pointcut("execution(public void com.aop.impl.NaiveWaiter.bye())")
//	@Pointcut("execution(public void com.aop.impl.NaiveWaiter.bye(..))")//匹配既可以有参数,也可以无参数
//	@Pointcut("execution(public void com.aop.impl.NaiveWaiter.bye(*))")//只匹配有参数的
	public void bye(){};
	
	//在pointcut匹配的方法之前执行
	@Before("greetTo()")//注解里面的名字和Pointcut处声明的方法名字相同
	public void before(){
		System.out.print("hello , ");
	}
	
	//在pointcut匹配的方法之后执行
	@After("bye()")
	public void after(){
		System.out.print("have a good day!!!");
	}
	
	//在pointcut匹配的方法的前后都执行
	@Around("server()")
	public void around(ProceedingJoinPoint pjp){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		System.out.println("I am a clock , record: " + sdf.format(new Date()) );
		try {
			pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("I am a clock , record: " + sdf.format(new Date()) );
	}
	
}

然后打开beans.xml配置文件中的注释,进行aspect 切面的配置

重新运行测试类,输出结果:

hello , greet to Tom....
I am a clock , record: 2011-48-11 22:48:42
server to ....
I am a clock , record: 2011-48-11 22:48:42
bye !
have a good day!!!
很好,文明用语都添加上了,aop 最基础的东西测试完毕了

抱歉!评论已关闭.