`
garrincha
  • 浏览: 12850 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

6Spring AOP 盗梦空间之一—— before和after

阅读更多
现在我们开始聊聊AOP
各位应该有看过盗梦空间。
影片开始的时候,莱昂纳多(简称LEO)要盗取日本人斋藤(简称日本人)的信息。
在这里,日本人睡觉的流程和LEO的流程是相互独立的,LEO是需要无声无息(没有发生显式调用)的侵入到日本人的睡觉流程中。
1、使用AOP还需要导入更多的包,这里我们总共需要6个包
aspectjweaver  aspectjrt  spring spring-aspects common-annotations commons-logging cglib-nodep
见图


2、新建类Person,包com.spring.service
代码如下
package com.spring.service;

import org.springframework.stereotype.Component;


@Component
public class Person {

	public void haveSleep()
	{
		System.out.println(this.getClass().getName());
		System.out.println("睡着了");
		System.out.println("睡醒了");
	}
}

这里我们使用注释的方式进行自动装配,所以Person类上需要做Component注释,我们之后会在haveSleep方法上做拦截。


3、新建类LeoIncept用于表示LEO的盗梦空间
package com.spring.aop;

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

@Aspect
@Component
public class LeoIncept {

	@Before("execution(public void com.spring.service.Person.haveSleep())")
	public void beforeSleep()
	{
	    
		System.out.println(this.getClass().getName());
		System.out.println("马上要睡着了,开始编织梦境");
	}
	@After("execution(public void com.spring.service.Person.haveSleep())")
	public void afterSleep()
	{
		System.out.println(this.getClass().getName());
		System.out.println("醒了,赶快圆梦撤退");
	}
	
}

首先LEOIncept类需要加Aspect注释,表示这是一个切面,用于向其他的方法插入本类。
Component注释表示使用Spring装配为一个类。

方法beforeSleep是一个切入的业务,切入点在Person类的haveSleep方法。@Before表示在haveSleep方法之前执行。
afterSleep方法同理。
这样实现的效果是
1执行LeoIncept beforeSleep
2执行Person haveSleep
3执行 afterSleep

4、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"
        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"
            >
            <context:component-scan base-package="com.spring"></context:component-scan>
            <context:annotation-config></context:annotation-config>
            <aop:aspectj-autoproxy></aop:aspectj-autoproxy>            
</beans>

xml中多了很多AOP的内容。
5、测试类PersonTest包com.spring.service.test
package com.spring.service.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.service.Person;

public class PersonTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ApplicationContext ctx = new ClassPathXmlApplicationContext("springbeans.xml");
		Person person = (Person) ctx.getBean("person");
		
		person.haveSleep();
		
	}

}


所有的代码中并没有显示的调用beforeSleep和afterSleep
最后看看执行结果吧
com.spring.aop.LeoIncept
马上要睡着了,开始编织梦境
com.spring.service.Person
睡着了
睡醒了
com.spring.aop.LeoIncept
醒了,赶快圆梦撤退
  • 大小: 23.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics