Spring学习笔记(十二)-----Spring in Action

Spring学习笔记(十二)-----Spring in Action
当我们有很多类需要通知时,显示的创建每个代理就会显得很笨拙。幸运的是,Spring有一个自动代理机制,它可以让容器为我们产生代理。Spring有2个类提供这种服务:BeanNameAutoProxyCreate和DefaultAdvisorAutoProxyCreator.

BeanNameAutoProxyCreate:为匹配一系列名字的Bean自动创建代理。它也允许在名字的2端进行通配符的匹配。
<? xml version="1.0" encoding="UTF-8" ?>
< 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-2.0.xsd" >
    
< bean  id ="performanceThresholdInterceptor"  class ="com.wyq.spring.common.aopinstance.autoproxy.PerformanceThresholdInterceptor" >
        
< constructor-arg >
            
< value > 5000 </ value >
        
</ constructor-arg >
    
</ bean >
    
<!--  
        如果Bean是一个Advisor或拦截器,它将应用到代理对象的所有方法上。如果是通知的话,Advisor切入点
        会根据不同Bean将通知应用到不同的地方。
     
-->
    
< bean  id ="performanceThresholdProxyCreator"  class ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
        
< property  name ="beanNames" >
            
< list >
                
< value > *Service </ value >
            
</ list >
        
</ property >
        
< property  name ="interceptorNames" >
            
< value > performanceThresholdInterceptor </ value >
        
</ property >
    
</ bean >
</ beans >
更强大的自动代理创建器是DefaultAdvisorAutoProxyCreator.当ApplicationContext读入所有Bean的配置信息后,DefaultAdvisorAutoProxyCreator将扫描上下文,寻找所有的Advisor.它将这些Advisor应用到所有符合Advisor切入点的Bean中。
<? xml version="1.0" encoding="UTF-8" ?>
< 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-2.0.xsd" >
    
< bean  id ="performanceThresholdInterceptor"  class ="com.wyq.spring.common.aopinstance.autoproxy.PerformanceThresholdInterceptor" >
        
< constructor-arg >
            
< value > 5000 </ value >
        
</ constructor-arg >
    
</ bean >
    
<!--  
        一个Advisor是一个切入点和一个通知的结合体。不用显示的将Advisor与其他东西结合,现在只要简单的定义他们,然后让他们自动
        应用到他们匹配的地方。这样松耦合Bean以及他们的通知就实现了。你只管写好你的Bean,写好你的通知,让容器来充当媒婆。
     
-->
    
< bean  id ="advisor"  class ="org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
        
< property  name ="advice" >
            
< bean  class ="com.wyq.spring.common.aopinstance.autoproxy.PerformanceThresholdInterceptor" ></ bean >
        
</ property >
        
< property  name ="pattern" >
            
< value > .+Service\..+ </ value >
        
</ property >
    
</ bean >
    
< bean  id ="autoProxyCreator"  class ="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" >
    
</ bean >
</ beans >

你可能感兴趣的:(Spring学习笔记(十二)-----Spring in Action)