引介加強的目標是在目標類中添加一些新的方法和屬性。以Waiter類爲例,如今想給其添加一個Management接口中的manage()方法而不修改Waiter類的代碼。java
一、須要加強的類spring
package com.test.springadvicetype; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; /** * 服務員類 */ @Component public class Waiter { /** * 服務 * @param name */ public String serve(String name) { System.out.println(name + ",您好,很高興爲您服務。"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); return name + ",您好,如今是北京時間" + format.format(new Date()); } /** * 開車 * @param name */ public void driving(String name) { throw new RuntimeException(name + ",您好,禁止酒後駕車!"); } }
二、爲Waiter加強新的類 Managementide
package com.test.springadvicetype.introductioninterceptor; public interface Management { /** * 管理 * @param name */ void manage(String name); }
三、Management 的實現類,DelegatingIntroductionInterceptor已經實現了引介加強接口IntroductionInterceptor,直接繼承。測試
package com.test.springadvicetype.introductioninterceptor; import org.springframework.aop.support.DelegatingIntroductionInterceptor; /** * 經理類 */ public class Manager extends DelegatingIntroductionInterceptor implements Management { @Override public void manage(String name) { System.out.println(name + ",您好,我是經理,負責管理服務員"); } }
四、xml配置,spring-chapter3-springintroductioninterceptor.xmlspa
<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啓註解掃描 --> <context:component-scan base-package="com.test.springadvicetype"/> <bean id="waiter" class="com.test.springadvicetype.Waiter"/> <bean id="manager2" class="com.test.springadvicetype.introductioninterceptor.Manager"/> <bean id="waiterProxy" class="org.springframework.aop.framework.ProxyFactoryBean" p:interfaces="com.test.springadvicetype.introductioninterceptor.Management" p:interceptorNames="manager2" p:target-ref = "waiter" p:proxyTargetClass="true"/> </beans>
五、測試代碼code
package com.test.springadvicetype.introductioninterceptor; import com.test.springadvicetype.Waiter; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Spring引介加強測試 */ public class SpringIntroductionInterceptorDemo { public static void main(String[] args) { System.out.println("Spring引介加強測試"); System.out.println("==========我是分割線=========="); ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-springintroductioninterceptor.xml"); Waiter waiterProxy = (Waiter) context.getBean("waiterProxy"); waiterProxy.serve("Michael"); System.out.println("==========我是分割線=========="); Management manager = (Management)waiterProxy; manager.manage("Michael"); } }
六、運行結果component
Spring環繞加強測試 ==========我是分割線========== 前置加強執行了 Michael,您好,很高興爲您服務。 後置加強執行了 ==========我是分割線========== 前置加強執行了 Tommy,您好,很高興爲您服務。 後置加強執行了