Java aop ComponentScan not working & AnnotationConfigApplicationContext getBean not working

后端 未结 3 1785
一个人的身影
一个人的身影 2021-01-14 10:35

I wrote a simple set of classes to show a friend about using Annotations for AOP (instead of xml config) . We couldnt get the @ComponentScan to work AND AnnotationConfigAppl

3条回答
  •  无人共我
    2021-01-14 11:35

    1. Usually you should use @ComponentScan along with a @Configuration annotated class and keep in mind that @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages..

    2. The @Component class tells Spring to create a bean of that type so you no longer need to use xml configuration, and the bean is a class that can be instantiated => no interface / abstract classes. So, in your case, you should remove the @Component from PersonOperationsI and leave it only in PersonOperations. When you annotate a class with @Component, the default name given to the bean is the class name with lower first letter, so you should call ctx.getBean("personOperationsI") or ctx.getBean(PersonOperations.class)

    And for the future read these naming conventions for interfaces and implementations. In your case I would modify the following : PersonOperationsI to Operations

提交回复
热议问题