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

#笔试题目#
全部评论

相关推荐

10-25 23:12
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务