Announcement Announcement Module
Collapse
No announcement yet.
component-scan and @beans components inside @configuration class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • component-scan and @beans components inside @configuration class

    Hi.

    In order to minimize xml configuration I would like to define all my beans in @Configuration classes. I am using spring mvc also and in order to achieve this I defined two lines in spring-servlet.xml:
    <context:component-scan base-package="xxx.yyy.config">
    <context:component-scan base-package="xxx.yyy.controller">

    in package xxx.yyy.controller I have all my @Controllers which are scanned successfully.
    in package xxx.yyy.config I have all other beans which are created by @Bean annotation methods (inside @Configuration annotated class). For instance InternalResourceViewResolver, some other characteristic for spring mvc beans and my internal services.

    The problem is that although @Configuration class is scanned (at least I think it is), @Bean methods are not and as a result I do not have my services and all other beans instantiated.
    Another problem I am struggling with is that - how can I inject my services to auto-scanned @Controller classes? Shall I just use @Autowired annotation? (assuming that @Configuration class successfully instantiate them)

    Thank you in advance!

  • #2
    Please use [ code][/code ] tags when posting code/xml/stacktraces, that way it will remain readable (at least larger blocks).

    @Configuration classes are @Component clases and as such will be detected... Assuming you are using the correct classes make sure you aren't using the older Spring JavaConfig project classes but are using the spring classes. Also for @Configuration classes to work you MUST have cglib on your classpath (at least before spring 3.2).

    Another problem I am struggling with is that - how can I inject my services to auto-scanned @Controller classes? Shall I just use @Autowired annotation? (assuming that @Configuration class successfully instantiate them)
    Use @Autowired, @Inject or @Resource to inject the resources.

    Comment

    Working...
    X