目录结构
公共的类
package cn.yld.webchatapp.test;
public interface Sleepable {
public void sleep();
}
package cn.yld.webchatapp.test;
import org.springframework.stereotype.Service;
@Service
public class Human implements Sleepable {
@Override
public void sleep() {
System.out.println("开始睡觉");
}
}
package cn.yld.webchatapp.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.yld.webchatapp.test.Sleepable;
public class MainTest {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
"conf/spring-mybatis.xml", "conf/spring-mvc-manage.xml", "conf/spring.xml" });
// String[] aa = context.getBeanDefinitionNames();
// for (String b : aa) {
// System.out.println(b);
// }
Sleepable sleeper = (Sleepable) context.getBean("human");
sleeper.sleep();
}
}
1、基于代理的AOP
代码如下:
spring.xml配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd>
<!-- 扫描controller(controller层注入) -->
<context:component-scan base-package="cn.yld.webchatapp.test" />
<!-- aop 配置 -->
<bean id="sleepAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="sleepHelper" />
<property name="pattern" value=".*sleep" />
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
</beans>
测试类输出:--ok
睡觉前要脱衣服!
开始睡觉
睡醒了要穿衣服!
2、@AspectJ注解驱动的切面
spring.xml----
1)在命名空间需要加入的是
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
<!-- aop 配置 -->
<aop:aspectj-autoproxy />
package cn.yld.webchatapp.test;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* Before(前) org.apringframework.aop.MethodBeforeAdvice After-returning(返回后)
* After-returning(返回后) org.springframework.aop.AfterReturningAdvice
* After-throwing(抛出后) After-throwing(抛出后) org.springframework.aop.ThrowsAdvice
* Arround(周围) Arround(周围) org.aopaliance.intercept.MethodInterceptor
* Introduction(引入) Introduction(引入)
* org.springframework.aop.IntroductionInterceptor
*
* @author 龙
*
*/
@Component-----这个注解可以允许spring扫描到这个类的bean从而进行aop
@Aspect
public class SleepHelper {
public SleepHelper() {
}
@Pointcut("execution(* *.sleep())")
public void sleeppoint() {
}
@Before("sleeppoint()")
public void beforeSleep() {
System.out.println("睡觉前要脱衣服!");
}
@AfterReturning("sleeppoint()")
public void afterSleep() {
System.out.println("睡醒了要穿衣服!");
}
}
测试类输出:--ok
睡觉前要脱衣服!
开始睡觉
睡醒了要穿衣服!
3、使用Spring来定义纯粹的POJO切面
前面我们用到了<aop:aspectj-autoproxy/>标签,Spring在aop的命名空间里面还提供了其他的配置元素:
<aop:advisor> 定义一个AOP通知者
<aop:after> 后通知
<aop:after-returning> 返回后通知
<aop:after-throwing> 抛出后通知
<aop:around> 周围通知
<aop:aspect>定义一个切面
<aop:before>前通知
<aop:config>顶级配置元素,类似于<beans>这种东西
<aop:pointcut>定义一个切点
代码同例子2相同,只是修改配置文件,加入AOP配置即可:
<!-- aop 配置 -->
<aop:config>
<aop:aspect ref="sleepHelper">
<aop:before method="beforeSleep" pointcut="execution(* *.sleep(..))"/>
<aop:after method="afterSleep" pointcut="execution(* *.sleep(..))"/>
</aop:aspect>
</aop:config>
个人感觉还是使用注解的方式更方便,灵活一些。