Spring三种实现自动代理

Spring三种实现自动代理

1.基于bean配置名规则的自动代理创建器:允许为一组特定配置名的bean自动创建代理实例的代理创建器,实现类为:BeanNameAutoProxyCreator

2.基于Advisor匹配机制的自动代理创建器,实现类为:DefaultAdvisorAutoProxyCreator

3.基于bean中的AspectJ注解标签的自动代理创建器

第一种:

Waiter类:

package aop.beannameauto;

public class Waiter {
		public void greet(String name) {
			System.out.println("Waiter.greet()"+name);
		}
}

前置方法增强:

package aop.beannameauto;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class GreetB implements MethodBeforeAdvice{

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2)
			throws Throwable {
		// TODO Auto-generated method stub
		String name= (String) arg1[0];
		System.out.println("GreetB.before()"+name);
		
	}
	
}

XML配置文件:




<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
		
		<bean id="waiter" class="aop.beannameauto.Waiter" />
	<bean id="greetingAdvice" class="aop.beannameauto.GreetB" />
           
           <bean
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
		p:beanNames="*er" p:interceptorNames="greetingAdvice"
		p:optimize="true"/>
           beans>

BeanNameAutoProxyCreator有一个beanname属性,允许用户指定一组需要自动代理的bean名称

测试类:

package aop.beannameauto;

import static org.junit.Assert.*;

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

public class BeanTest {
	
	@Test
	public void test() {
			ApplicationContext context = new ClassPathXmlApplicationContext("aop/beannameauto/beans.xml");
			Waiter waiter = (Waiter) context.getBean("waiter");
			waiter.greet("11");
			
	}

}

第二种:

Waiter类和增强类同上

配置文件:




<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	
	<bean id="waiter" class="aop.defaultauto.Waiter" />
	<bean id="greetingAdvice" class="aop.defaultauto.GreetB" />
	<bean id="regexpAdvisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
		p:patterns=".*greet.*" p:advice-ref="greetingAdvice" />
	<bean
		class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

beans>

测试类:

package aop.defaultauto;

import static org.junit.Assert.*;

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

public class BeanTest {

	@Test
	public void test() {
		ApplicationContext context = new ClassPathXmlApplicationContext("aop/defaultauto/beans.xml");
		Waiter waiter = (Waiter) context.getBean("waiter");
		waiter.greet("11");
	}

}

第三种:

Waiter类同上

增强类:

package aop.aspect;

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

@Aspect
public class GreetB {

	@Before("execution(* greet(..))")
	public void before() {
		System.out.println("GreetB.before()");
	}
}

在类处标注了@AspectJ注解,第三方处理程序就可以通过类是否拥有@AspectJ注解判断是否为一个切面

其次,在方法定义出标注了@Before注解,并为该注解提供了成员值"execution(* greet(..))",提供了两个信息:@Before表示前置增强,成员值是一个@AspectJ切点表达式,意思在目标类的greet方法上织入增强

测试类:

package aop.aspect;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;

public class AspectTest {

	@Test
	public void test() {
		Waiter waiter = new Waiter();
		AspectJProxyFactory factory = new AspectJProxyFactory();
		factory.setTarget(waiter);
		factory.addAspect(GreetB.class);
		Waiter waiter2 = factory.getProxy();
		waiter2.greet("11");
		
	}

}

你可能感兴趣的:(Spring学习)