Results 1 to 2 of 2

Thread: component-scan and @beans components inside @configuration class

  1. #1
    Join Date
    Mar 2013
    Posts
    2

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •