spring AOP Bean添加新方法

目的:为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="studentAdditionalDetailsImpl" 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>

 注意:若采用

default-impl="imple.StudentImpl"

输出结果:不知道为何没有取到值?

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

接口:

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 }  

测试方法:

/*
* 基于xml 切面引入性能(为已经实现的类添加新的方法)
* Aop 常见的作用是为现有的方法添加新的功能,该测试方法可以通过Aop为Spring Bean添加新方法
*/

1     @Test
2     public void test9(){
3         StudentAdditionalDetails studentAdditionalDetails = (StudentAdditionalDetails) ctx.getBean("studentAdditionalDetailsImpl");  
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

--------------------------------------------------------以下基于注解实现-----------------------------------------------------------------------------

@DeclareParents(
    value=" AspectJ语法类型表达式",
    defaultImpl=引入接口的默认实现类)
private Interface interface;
原文地址:https://www.cnblogs.com/a757956132/p/5036672.html