Spring-AOP 自动创建代理之AnnotationAwareAspectJAutoProxyCreator

2021-08-16 16:47:54 浏览数 (1)

文章目录

  • 导读
  • 实例

导读

Spring-AOP 自动创建代理

Spring-AOP 自动创建代理之BeanNameAutoProxyCreator

Spring-AOP 自动创建代理之DefaultAdvisorAutoProxyCreator

Spring-AOP 自动创建代理之AnnotationAwareAspectJAutoProxyCreator


实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

准备工作参考 :http://blog.csdn.net/yangshangwei/article/details/77519375#准备工作

接口

代码语言:javascript复制
package com.xgj.aop.spring.advisor.aspectJ.aspectJByConf;

public interface Waiter {

	public void greetTo(String name);

	public void serverTo(String name);
}

POJO

代码语言:javascript复制
package com.xgj.aop.spring.advisor.aspectJ.aspectJByConf;

public class NaiveWaiter implements Waiter {

	@Override
	public void greetTo(String name) {
		System.out.println("NaiveWaiter Greet To "   name);
	}

	@Override
	public void serverTo(String name) {
		System.out.println("NaiveWaiter Server To "   name);
	}

}

切面POJO

代码语言:javascript复制
package com.xgj.aop.spring.advisor.aspectJ.aspectJByConf;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * 
 * 
 * @ClassName: PreGreetingAspect
 * 
 * @Description: 切面类
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年8月24日 下午3:21:15
 */

// 通过@Aspect将PreGreetingAspect标注为一个切面
@Aspect
public class PreGreetingAspect {

	// 定义切点和增强类型
	@Before("execution(* greetTo(..))")
	public void beforeGreeting() {// 增强的横切逻辑
		System.out.println("How are you ?");
	}
}

配置文件

代码语言:javascript复制
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


 

<bean id="waiter" class="com.xgj.aop.spring.advisor.aspectJ.aspectJByConf.NaiveWaiter"/>

<bean class="com.xgj.aop.spring.advisor.aspectJ.aspectJByConf.PreGreetingAspect"/>

<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>

beans>

单元测试

代码语言:javascript复制
package com.xgj.aop.spring.advisor.aspectJ.aspectJByConf;

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

/**
 * 
 * 
 * @ClassName: PreGreetingAspectTest
 * 
 * @Description:
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年8月24日 下午3:21:29
 */
public class PreGreetingAspectTest {

	@Test
	public void test() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"classpath:com/xgj/aop/spring/advisor/aspectJ/aspectJByConf/conf-aspectJ.xml");

		Waiter naiveWaiter = ctx.getBean("waiter", Waiter.class);

		naiveWaiter.greetTo("XiaoGongJiang");
		naiveWaiter.serverTo("XiaoGongJiang");
	}
}

运行结果:

代码语言:javascript复制
2017-08-24 17:41:16,210  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@24177697: startup date [Thu Aug 24 17:41:16 BOT 2017]; root of context hierarchy
2017-08-24 17:41:16,609  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/aspectJByConf/conf-aspectJ.xml]
How are you ?
NaiveWaiter Greet To XiaoGongJiang
NaiveWaiter Server To XiaoGongJiang

可以看到greetTo被正确的织入了增强.

0 人点赞