PS:定義切面
package concert;import org.aspectj.lang.annotation.*;@Aspectpublic class Audience { @Pointcut("execution(** concert.Performance.perform(..))") public void performance() {} @Before("performance()") public void silenceCellPhones() { System.out.PS:java配置,@EnableAspectJAutoProxy啟用AspectJ自動代理package concert;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration@EnableAspectJAutoProxy@ComponentScanpublic class ConcertConfig { @Bean public Audience audience() { return new Audience(); }}PS:切點接口
package concert;public interface Performance { public void perform();}PS:切點接口實現
package concert;import org.springframework.stereotype.Component;@Componentpublic class PerformanceImpl implements Performance { public void perform() { System.out.println("Singing!!!"); }}PS:如果通過xml裝配bean
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="concert"/> <aop:aspectj-autoproxy /> <bean class="concert.Audience" /></beans>PS:把上面的切面代碼改為如下。
package concert;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;@Aspectpublic class Audience { @Pointcut("execution(** concert.Performance.perform(..))") public void performance() {} @Around("performance()") public void watchPerformance(ProceedingJoinPoint jp) { try { System.out.println("Silencing cell phones"); System.out.println("Taking seats"); jp.proceed(); System.out.println("CLAP CLAP CLAP!!!"); } catch (Throwable e) { System.out.println("Demanding a refund"); } }}參考:Spring Aop 使用注解引入新功能
PS:不需要,沒有用到
package concert;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;@Aspectpublic class Audience { @Pointcut("execution(** concert.Performance.perform(..))") public void performance() {} @Around("performance()") public void watchPerformance(ProceedingJoinPoint jp) { try { System.out.println("Silencing cell phones"); System.out.println("Taking seats"); jp.proceed(); System.out.println("CLAP CLAP CLAP!!!"); } catch (Throwable e) { System.out.println("Demanding a refund"); } }}PS:注入EncoreableIntroducer
package concert;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration@EnableAspectJAutoProxy@ComponentScanpublic class ConcertConfig { @Bean public Audience audience() { return new Audience(); } @Bean public EncoreableIntroducer encoreableIntroducer() { return new EncoreableIntroducer(); }}PS:Encoreable實現類
package concert;public class DefaultEncoreable implements Encoreable { public void performEncore() { System.out.println("performEncore!!!"); }}PS:Encoreable接口
package concert;public interface Encoreable { public void performEncore();}PS:EncoreableIntroducer切面,加號表示Performance所有子類型,不是Performance本身
package concert;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.DeclareParents;@Aspectpublic class EncoreableIntroducer { @DeclareParents(value="concert.Performance+",defaultImpl = DefaultEncoreable.class) public static Encoreable encoreable;}PS:Performance接口
package concert;public interface Performance { public void perform();}PS:Performance實現類
package concert;import org.springframework.stereotype.Component;@Componentpublic class PerformanceImpl implements Performance { public void perform() { System.out.println("Singing!!!"); }}新聞熱點
疑難解答