Я учусь Представления (известные как объявления типов в AspectJ). Я получил пример из представлений SpringAOP с AspectJ с использованием xml. Я пытаюсь повторить то же самое, используя аннотации , но я не знаю, как поступить. Я провел много исследований в интернете, но не смог найти никаких образцов. Не могли бы вы помочь мне с этим.
PerformanceTest.class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ConcertConfig.class)
public class PerformanceTest {
@Autowired
public Audience audience;
@Autowired
public Performance liveOpera;
@Autowired
public EncoreableIntroducer encoreable;
@Test
public void testPerformance(){
liveOpera.perform();
}
}
LiveOpera.class
public class LiveOpera implements Performance{
@Override
public void perform() {
System.out.println("Live Opera Performance Started");
}}
Encoreable.interface
public interface Encoreable {
public void performEncore();
}
DefaultEncoreable.class
public class DefaultEncoreable implements Encoreable {
@Override
public void performEncore() {
System.out.println("WoW!! What an encore performance!!");
}
}
ConcertConfig.class
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class ConcertConfig {
@Bean
public Audience audience(){
return new Audience();
}
@Bean
public LiveOpera opera(){
return new LiveOpera();
}
@Bean
public EncoreableIntroducer encoreable(){
return new EncoreableIntroducer();
}
}
Интерфейс производительности
public interface Performance {
public void perform();
}
EncoreableIntroducer.class
@Aspect
public class EncoreableIntroducer {
@DeclareParents(value="com.example.introduction.Performance+",
defaultImpl=DefaultEncoreable.class)
public static Encoreable encoreable;
}
Audience.class
@Aspect
public class Audience {
@Pointcut("execution(** com.example.introduction.Performance.perform(..))")
public void performance() {
}
public void silenceMobilePhones() {
System.out.println("Silencing Mobile Phones");
}
public void takeSeats() {
System.out.println("Taking seats");
}
public void applause() {
System.out.println("CLAP CLAP CLAP!!");
}
public void demandRefund() {
System.out.println("Need a Refund!!");
}
@Around("performance()")
public void wrapPerformance(ProceedingJoinPoint jp) {
try {
silenceMobilePhones();
takeSeats();
jp.proceed();
applause();
} catch (Throwable e) {
System.out.println(e);
e.printStackTrace();
demandRefund();
}
}
}
Вывод: при выполнении метода @Test в PerformanceTest.class
Silencing Mobile Phones
Taking seats
Live Opera Performance Started
CLAP CLAP CLAP!!
Пожалуйста, дайте мне знать, как я могу использовать аспект в EncoreableIntroducer.class, используя AspectJ-Introductions, чтобы можно было использовать метод execute () в DefaultEncoreable.class?
Ожидается вывод, как показано ниже при использовании AspectJ Introduction:
Silencing Mobile Phones
Taking seats
WoW!! What an encore performance!!
Live Opera Performance Started
CLAP CLAP CLAP!!