One
The role of 1.Introduction is to increase the dynamic method of the class
When Spring discovers a beans annotated with @Aspect, it'll automatically create a proxy that delegates calls to either The proxied bean or to the introduction implementation, depending on whether the method called belongs to the proxied bean Or to the introduced interface.
Ii. examples
To add encore functionality to performers
1.
Package Concert; Public Interface Performance { void performance ();}
2.
1 PackageConcert;2 3 Importorg.springframework.stereotype.Component;4 5 @Component6 Public classEminemImplementsperformance{7 8 @Override9 Public voidperformance () {TenSystem.out.println ("You can do anything set to your mind"); One } A -}
3.
Package Concert; Public Interface encoreable { void performencore ();}
4.
1 PackageConcert;2 3 Public classDefaultencoreableImplementsencoreable{4 5 @Override6 Public voidPerformencore () {7System.out.println ("Defaultencoreable---> Performencore ()");8 }9 Ten}
5.
1 PackageConcert;2 3 ImportOrg.aspectj.lang.annotation.Aspect;4 Importorg.aspectj.lang.annotation.DeclareParents;5 Importorg.springframework.stereotype.Component;6 7 @Aspect8 @Component9 Public classEncoreableintroducer {Ten@DeclareParents (value = "concert. Performance+ ", Defaultimpl = defaultencoreable.class) One Public Staticencoreable encoreable; A}
Analytical:
The role of @DeclareParents is to introduce "encoreable" to "performance", which consists of three parts
(1) Value: The interface type of the bean to add new functionality
(2) Defaultimpl: the implementation of the function interface to be added
(3) Added static variable of the function interface
6. Configuration Files
Package Concert; Import Org.springframework.context.annotation.ComponentScan; Import org.springframework.context.annotation.Configuration; Import org.springframework.context.annotation.enableaspectjautoproxy;@ Configuration@componentscan@enableaspectjautoproxypublicclass Config {}
7. Testing
1 Importorg.junit.Test;2 ImportOrg.junit.runner.RunWith;3 Importorg.springframework.beans.factory.annotation.Autowired;4 ImportOrg.springframework.context.ApplicationContext;5 Importorg.springframework.test.context.ContextConfiguration;6 ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;7 8 Importconcert. Config;9 Importconcert. encoreable;Ten One@RunWith (Springjunit4classrunner.class) A@ContextConfiguration (Classes=config.class) - Public classIntroductiontest { - @Autowired the ApplicationContext context; - - @Test - Public voideddieshouldbeacontestant () { +encoreable Eminem = (encoreable) Context.getbean ("Eminem"); - Eminem.performencore (); + //Performance Eminem = (performance) Context.getbean ("Eminem"); A //eminem.performance (); at } -}
In addition: The configuration method of XML:
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5Xsi:schemalocation= "Http://www.springframework.org/schema/beans6http//www.springframework.org/schema/beans/spring-beans.xsd7http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP8http//www.springframework.org/schema/aop/spring-aop.xsd ">9 Ten<bean id= "Eddie" One class= "Com.springinaction.springidol.Instrumentalist" > A<property name= "Instrument" > -<beanclass= "Com.springinaction.springidol.Guitar"/> -</property> the</bean> - -<!--<start id= "Audience_bean"/>--> -<bean id= "Audience" + class= "Com.springinaction.springidol.Audience"/> -<!--<end id= "Audience_bean"/>--> + A<!--<start id= "Audience_aspect"/>--> at<aop:config> -<aop:aspect ref= "Audience" > -<aop:pointcut id= "Performance" expression= -"Execution (* com.springinaction.springidol.Performer.perform (..))" -/> -<Aop:before inpointcut-ref= "Performance" -Method= "Takeseats ()"/> to<Aop:before +pointcut-ref= "Performance" -method= "Turnoffcellphones"/> the<aop:after-returning *pointcut-ref= "Performance" $Method= "Applaud"/>Panax Notoginseng<aop:after-throwing -pointcut-ref= "Performance" themethod= "Demandrefund"/> +</aop:aspect> A the<!--<start id= "Contestant_introduction"/>--> +<aop:aspect> -<aop:declare-Parents $Types-matching= "com.springinaction.springidol.performer+" $implement-Interface= "Com.springinaction.springidol.Contestant" - default-impl= "Com.springinaction.springidol.GraciousContestant" -/> the</aop:aspect> -<!--<end id= "Contestant_introduction"/>-->Wuyi the<!-- -<start id= "Delegate_ref"/> Wu<aop:declare-Parents -Types-matching= "com.springinaction.springidol.performer+" Aboutimplement-Interface= "Com.springinaction.springidol.Contestant" $delegate-ref= "Contestantdelegate" -/> -<end id= "Delegate_ref"/> - A<start id= "Contestant_delegate"/> +<bean id= "Contestantdelegate" the class= "Com.springinaction.springidol.GraciousContestant"/> -<end id= "Contestant_delegate"/> $ the- the the</aop:config> the<!--<end id= "Audience_aspect"/>--> - in</beans>
SPRING in ACTION 4th Note-chapter fourth aspect-oriented spring-010-introduction adding new methods to classes