Purpose: Add student ShowDetails () and Extrashowdetails () two methods for Studentadditionaldetails
AOP can add additional functionality to existing methods in spring, and AOP can add new methods to spring beans
<aop:declare-parents
Types-matching= "Previous primitive class/interface"
Implement-interface= "Interface for the features you want to add"
Defalut-impl= "The default implementation of the new concept"
/>
Or:
<aop:declare-Parents types-matching= "before the original class/interface" implement-interface= "The features you want to add Interface " delegate-ref=" The default implementation of the new concept "/>
Configuration file:
1<?xml version= "1.0" encoding= "UTF-8"?>2 3<beans xmlns= "Http://www.springframework.org/schema/beans"4Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"6xmlns:tx= "Http://www.springframework.org/schema/tx"7xmlns:p= "http://www.springframework.org/schema/p"8xsi:schemalocation="9http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdTenhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd Onehttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> A - - the -<!--add a method to the Studentadditionaldetails or Studentadditionaldetailsimpl student -<bean id= "Student"class= "Imple." Studentimpl "> -<property name= "Studentno" value= "1001"/> +<property name= "Studentname" value= "John Peter"/> -</bean> + A<bean id= "Studentadditionaldetails"class= "Imple." Studentadditionaldetailsimpl "> at<property name= "City" value= "NewYork"/> -<property name= "Country" value= "America"/> -</bean> - -<aop:config> -<aop:aspect> in<!--types-matching= "imple. studentadditionaldetailsimpl+ "the same effect is achieved in the following way -Description: types-matching can be either an implementation class or an interface to- +<aop:declare-parents types-matching= "Inter. Studentadditionaldetails+ " -implement-Interface= "Inter." Student " the delegate-ref= "Student"/> *</aop:aspect> $</aop:config>Panax Notoginseng - the</beans>
Or:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!--for studentadditionaldetails or Studentadditionaldetailsimpl add student inside the method--<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=" Coun Try "value=" America "/> </bean> <aop:config> <aop:aspect> <!--types-matching= "imple. studentadditionaldetailsimpl+ "the effect is the same as in the following way: Types-matching can be either an implementation class or an interface--<aop:declare-parents types-matching= "Inter. Studentadditionaldetails+ "Implement-Interface= "Inter." Student "default-impl= "imple. Studentimpl "/> </aop:aspect> </aop:config> </beans>
Interface:
1 package Inter; 2 Public interface studentadditionaldetails { 4 public void Showadditionaldetails (); 5 }
1 Package Inter; 2 3 Public Interface Student { 4public void showDetails (); 5 Public void extrashowdetails (); 6 }
Implementation method:
Packageimple;ImportInter. Studentadditionaldetails; Public classStudentadditionaldetailsimplImplementsStudentadditionaldetails {PrivateString City; PrivateString Country; PublicString getcity () {returnCity ; } Public voidsetcity (String city) { This. City =City ; } PublicString getcountry () {returnCountry; } Public voidSetcountry (String country) { This. Country =Country; } Public voidshowadditionaldetails () {System.out.println ("Studentadditionaldetailsimpl Showadditionaldetails () City:" + This. City); System.out.println ("Studentadditionaldetailsimpl showadditionaldetails () Country:" + This. Country); } }
1 Packageimple;2 3 ImportInter. Student;4 5 Public classStudentimplImplementsStudent {6 Private intStudentno; 7 PrivateString Studentname; 8 9 Public intGetstudentno () {Ten returnStudentno; One } A Public voidSetstudentno (intStudentno) { - This. Studentno =Studentno; - } the PublicString Getstudentname () { - returnStudentname; - } - Public voidsetstudentname (String studentname) { + This. Studentname =Studentname; - } + A Public voidshowDetails () { atSystem.out.println ("Studentimpl showDetails studentno:" + This. Studentno); -System.out.println ("Studentimpl showDetails studentname:" + This. Studentname); - } - Public voidextrashowdetails () { -System.out.println ("Studentimpl extrashowdetails studentno:" + This. Studentno); -System.out.println ("Studentimpl extrashowdetails studentname:" + This. Studentname); in } -}
Test method:
1 @Test2 Public voidTest9 () {3Studentadditionaldetails studentadditionaldetails = (studentadditionaldetails) Ctx.getbean (" Studentadditionaldetails "); 4 ((Student) studentadditionaldetails). ShowDetails (); 5 ((Student) studentadditionaldetails). Extrashowdetails (); 6 studentadditionaldetails.showadditionaldetails (); 7}
Output Result:
1 studentimpl showDetails studentno:10012studentimpl showDetails studentname:john Peter 3 studentimpl extrashowdetails studentno:10014Studentimpl extrashowdetails Studentname:john Peter5studentadditionaldetailsimpl showadditionaldetails () city:newyork6 Studentadditionaldetailsimpl showadditionaldetails () Country:america
Spring AOP Bean Add New method