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

现在我们开始聊聊AOP
各位应该有看过盗梦空间。
影片开始的时候,莱昂纳多(简称LEO)要盗取日本人斋藤(简称日本人)的信息。
在这里,日本人睡觉的流程和LEO的流程是相互独立的,LEO是需要无声无息(没有发生显式调用)的侵入到日本人的睡觉流程中。
1、使用AOP还需要导入更多的包,这里我们总共需要6个包
aspectjweaver aspectjrt spring spring-aspects common-annotations commons-logging cglib-nodep
见图
[img]http://dl2.iteye.com/upload/attachment/0100/7536/8bf807f3-7b09-3316-be86-259d5687e3df.png[/img]

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
醒了,赶快圆梦撤退
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值