码迷,mamicode.com
首页 > 编程语言 > 详细

spring AOP Bean添加新方法

时间:2015-12-10 19:15:37      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

目的:为studentAdditionalDetails中添加Student的showDetails()和ExtraShowDetails()两个方法

 

spring  中AOP能够为现有的方法添加额外的功能,AOP也能为Spring Bean添加新方法 

<aop:declare-parents  
      types-matching="之前原始的类/接口"
      implement-interface="想要添加的功能的接口"
      defalut-impl="新概念的默认的实现"
/>

 或者:

<aop:declare-parents  
      types-matching="之前原始的类/接口"
      implement-interface="想要添加的功能的接口"
      delegate-ref="新概念的默认的实现"
/>

 

 

配置文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5         xmlns:aop="http://www.springframework.org/schema/aop"
 6         xmlns:tx="http://www.springframework.org/schema/tx"
 7         xmlns:p="http://www.springframework.org/schema/p"
 8         xsi:schemaLocation="
 9             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
11             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
12 
13 
14 
15      
16      <!-- 为StudentAdditionalDetails或者StudentAdditionalDetailsImpl添加student里面的方法 -->
17      <bean id="student" class="imple.StudentImpl">  
18         <property name="studentNo" value="1001" />  
19         <property name="studentName" value="John Peter" />  
20     </bean>  
21        
22     <bean id="studentAdditionalDetails" class="imple.StudentAdditionalDetailsImpl">  
23         <property name="city" value="Newyork" />  
24         <property name="country" value="America" />  
25     </bean>  
26        
27     <aop:config>  
28         <aop:aspect>  
29                                 <!-- types-matching="imple.StudentAdditionalDetailsImpl+"  改成下面的方式 实现的效果相同 
30                                       说明:types-matching既可以是实现类也可以是接口                                
31                                  -->
32             <aop:declare-parents types-matching="inter.StudentAdditionalDetails+"  
33                                  implement-interface="inter.Student"  
34                                  delegate-ref="student" />  
35         </aop:aspect>  
36     </aop:config>  
37 
38 
39 </beans>

或者:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">



     
     <!-- 为StudentAdditionalDetails或者StudentAdditionalDetailsImpl添加student里面的方法 -->
     <bean id="student" class="imple.StudentImpl">  
        <property name="studentNo" value="1001" />  
        <property name="studentName" value="John Peter" />  
    </bean>  
       
    <bean id="studentAdditionalDetails" class="imple.StudentAdditionalDetailsImpl">  
        <property name="city" value="Newyork" />  
        <property name="country" value="America" />  
    </bean>  
       
    <aop:config>  
        <aop:aspect>  
                                <!-- types-matching="imple.StudentAdditionalDetailsImpl+"  改成下面的方式 实现的效果相同 
                                      说明:types-matching既可以是实现类也可以是接口                                
                                 -->
            <aop:declare-parents types-matching="inter.StudentAdditionalDetails+"  
                                 implement-interface="inter.Student"  
                                 default-impl="imple.StudentImpl"
                                 />  
        </aop:aspect>  
    </aop:config>  


</beans>

 

 

 

接口:

1 package inter;
2 
3 public interface StudentAdditionalDetails {  
4     public void showAdditionalDetails();  
5 }  
1 package inter;
2 
3 public interface Student {  
4     public void showDetails();  
5     public void ExtraShowDetails();
6 }

实现方法:

package imple;

import inter.StudentAdditionalDetails;

public class StudentAdditionalDetailsImpl implements StudentAdditionalDetails {  
    private String city;  
    private String country;  
       
    public String getCity() {  
        return city;  
    }  
    public void setCity(String city) {  
        this.city = city;  
    }  
    public String getCountry() {  
        return country;  
    }  
    public void setCountry(String country) {  
        this.country = country;  
    }  
       
    public void showAdditionalDetails(){  
        System.out.println("StudentAdditionalDetailsImpl showAdditionalDetails() city:"+this.city);  
        System.out.println("StudentAdditionalDetailsImpl showAdditionalDetails() country:"+this.country);  
    }  
}
 1 package imple;
 2 
 3 import inter.Student;
 4 
 5 public class StudentImpl implements Student {  
 6     private int studentNo;  
 7     private String studentName;  
 8        
 9     public int getStudentNo() {  
10         return studentNo;  
11     }  
12     public void setStudentNo(int studentNo) {  
13         this.studentNo = studentNo;  
14     }  
15     public String getStudentName() {  
16         return studentName;  
17     }  
18     public void setStudentName(String studentName) {  
19         this.studentName = studentName;  
20     }     
21        
22     public void showDetails(){  
23         System.out.println("StudentImpl showDetails studentNo :"+this.studentNo);  
24         System.out.println("StudentImpl showDetails studentName :"+this.studentName);  
25     }
26     public void ExtraShowDetails() {
27         System.out.println("StudentImpl ExtraShowDetails studentNo :"+this.studentNo);  
28         System.out.println("StudentImpl ExtraShowDetails studentName :"+this.studentName);  
29     }  
30 }  

测试方法:

1     @Test
2     public void test9(){
3         StudentAdditionalDetails studentAdditionalDetails = (StudentAdditionalDetails) ctx.getBean("studentAdditionalDetails");  
4         ((Student) studentAdditionalDetails).showDetails();  
5         ((Student) studentAdditionalDetails).ExtraShowDetails();  
6         studentAdditionalDetails.showAdditionalDetails();  
7     }

输出结果:

1 StudentImpl showDetails studentNo :1001
2 StudentImpl showDetails studentName :John Peter
3 StudentImpl ExtraShowDetails studentNo :1001
4 StudentImpl ExtraShowDetails studentName :John Peter
5 StudentAdditionalDetailsImpl showAdditionalDetails() city:Newyork
6 StudentAdditionalDetailsImpl showAdditionalDetails() country:America

 

spring AOP Bean添加新方法

标签:

原文地址:http://www.cnblogs.com/a757956132/p/5036672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!