spring aop 代码面试
有没有大神对 spring aop 比较熟悉
import org.aspectj.lang.*; import org.aspectj.lang.annotation.*; import org.springframework.context.annotation.*; import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.*; import java.lang.annotation.*; import java.util.*; @Aspect @Component public class LoggerAOP { @Autowired private Logger logger; public void loggingAdvice(JoinPoint jp) { } public static void main(String[] args) { AnnotationConfigApplicationContext config = new AnnotationConfigApplicationContext(); config.register(Config.class); config.refresh(); NameRepository repository = config.getBean(NameRepository.class); System.out.println(repository.getNames()); } } @Component class NameRepository { @LogExecution public List<String> getNames() { List<String> names = new ArrayList<>(); names.add("John"); names.add("Mary"); return names; } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @interface LogExecution {} interface Logger { public void log(String data); } @Configuration @EnableAspectJAutoProxy @Import({LoggerAOP.class, NameRepository.class}) class Config { @Bean public Logger logger() { return (message) -> System.out.println(message); } }
这个代码块现在有如下问题,请问该怎么改,先谢过了,不胜感激!
- logger will be invoked on NameRepository getName method: Wrong answer
- logger will be invoked on methods annotated with LogExecution: Wrong answer
- logger will be invoked only on public methods annotated with LogExecution: Wrong answer