spring AOP Bean添加新方法

时间:2022-04-30 07:21:39

目的:为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="新功能的默认的实现"
/>

配置文件:

 <?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="studentAdditionalDetailsImpl" 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"
delegate-ref="student" />
</aop:aspect>
</aop:config> </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

接口:

 package inter;

 public interface StudentAdditionalDetails {
public void showAdditionalDetails();
}
 package inter;

 public interface Student {
public void showDetails();
public void ExtraShowDetails();
}

实现方法:

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);
}
}
 package imple;

 import inter.Student;

 public class StudentImpl implements Student {
private int studentNo;
private String studentName; public int getStudentNo() {
return studentNo;
}
public void setStudentNo(int studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
} public void showDetails(){
System.out.println("StudentImpl showDetails studentNo :"+this.studentNo);
System.out.println("StudentImpl showDetails studentName :"+this.studentName);
}
public void ExtraShowDetails() {
System.out.println("StudentImpl ExtraShowDetails studentNo :"+this.studentNo);
System.out.println("StudentImpl ExtraShowDetails studentName :"+this.studentName);
}
}

测试方法:

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

     @Test
public void test9(){
StudentAdditionalDetails studentAdditionalDetails = (StudentAdditionalDetails) ctx.getBean("studentAdditionalDetailsImpl");
((Student) studentAdditionalDetails).showDetails();
((Student) studentAdditionalDetails).ExtraShowDetails();
studentAdditionalDetails.showAdditionalDetails();
}

输出结果:

 StudentImpl showDetails studentNo :1001
StudentImpl showDetails studentName :John Peter
StudentImpl ExtraShowDetails studentNo :1001
StudentImpl ExtraShowDetails studentName :John Peter
StudentAdditionalDetailsImpl showAdditionalDetails() city:Newyork
StudentAdditionalDetailsImpl showAdditionalDetails() country:America

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

@DeclareParents(
value=" AspectJ语法类型表达式",
defaultImpl=引入接口的默认实现类)
private Interface interface;