Spring提供自动生成代理,让容器自动生成代理,把用户繁琐的配置工作中解放出来
在Spring使用BeanPostProcessor完成这项工作
BeanNameAutoProxyCreator
1 public class Waiter { 2 public void greetTo(String name) { 3 System.out.println("waiter greet to " + name + "..."); 4 } 5 6 public void serveTo(String name) { 7 System.out.println("waiter serving " + name + "..."); 8 } 9 }
1 public class Sheer { 2 public void greetTo(String name) { 3 System.out.println("seller greet to " + name + "..."); 4 } 5 }
1 public class GreetingBeforeAdvice1 implements MethodBeforeAdvice{ 2 public void before(Method method, Object[] args, Object obj) 3 throws Throwable { 4 String clientName = (String) args[0]; 5 System.out.println(obj.getClass().getName() + "." + method.getName()); 6 System.out.println("How are you!Mr." + clientName + "."); 7 } 8 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 <bean id="waiter" class="com.asm.Waiter" /> 16 <bean id="seller" class="com.asm.Sheer" /> 17 <bean id="greetingAdvice" class="com.asm.GreetingBeforeAdvice1" /> 18 19 20 21 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" 22 p:beanNames="*er" //由于只有一个bean名称,所以直接使用属性进行配置。 23 p:interceptorNames="greetingAdvice" 24 p:optimize="true"/> //强制使用CGLIB代理 25 26 </beans>
1 package com.asm; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class SpringTest1 { 7 public static void main(String[] args) { 8 9 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 10 Waiter waiter = (Waiter)ctx.getBean("waiter"); 11 Sheer sheer = (Sheer)ctx.getBean("seller"); 12 waiter.greetTo("join"); 13 waiter.serveTo("join"); 14 sheer.greetTo("join"); 15 } 16 }
DefaultAdvisorAutoProxyCreator
切面是切点和增强的复合体
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 <bean id="waiter" class="com.asm.Waiter" /> 16 <bean id="seller" class="com.asm.Sheer" /> 17 <bean id="greetingAdvice" class="com.asm.GreetingBeforeAdvice1" /> 18 19 <bean id="regexAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" 20 p:patterns=".*greet.*" 21 p:advice-ref="greetingAdvice" 22 /> 23 24 <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> 25 26 </beans>