Results 1 to 5 of 5

Thread: XML and annotations

  1. #1
    Join Date
    Feb 2010
    Posts
    7

    Default XML and annotations

    Hi,

    I have a problem. I always used XML file to configure the container. Now we have, annoations. But still XML is more powerfull than annotatations. I wonder how to mix that two configurations i.e. Can I define bean via @Comoponent and then override properties via XML.

    Thanks for your help

  2. #2
    Join Date
    May 2009
    Location
    Chicago, USA
    Posts
    87

    Default

    If you have

    <context:annotation-config />

    in your XML then any @Component will @Autowire anything that's marked as such if you don't set the values yourself manually.

    Also you can use XML in your @Configuration's using @ImportResource.

    http://static.springsource.org/sprin...eference/html/

    Check that document from section 3.9 through 3.14 to see what you can do.

  3. #3
    Join Date
    Feb 2010
    Posts
    7

    Default

    Thanks,

    But I talked about XML-centric configuration.
    For example: I annotaded a bean as @Component, and how to address it from XML descriptor. I've done some experiments ant it turned out that when you use the same id in @Component annotation and XML file, the container creates only one object. But it is not documented feature

    @Component
    class Service {}

    and XML

    <beans ...>
    //component scan and stuff

    <bean id="service" class="my.Service" /> -- here I addressed component via the XML descriptor
    </beans>

    I noticed than XML has higher priority than annotation, ie: when you use @Value to init value of the property and <property/> tag at the same time, the XML definition wins.

    Do you know what I mean?

  4. #4
    Join Date
    May 2009
    Location
    Chicago, USA
    Posts
    87

    Default

    I think I understand your confusion.

    There is no direct way to access @Component's and their children. The idea of scanning for @Components is supposed to be used along with @Autowire which together allows you to omit XML configuration. Perhaps this makes more sense when you realize there is an @Scope annotation as well to define a @Components scope.

    As for creating one object, how did you test what object was created with the auto scan? I don't think Spring would even bother with the @Component if there are no references to it.

    If you want XML-centric WITH @Components then I believe the goal would be to @Component/@Autowire annotate as much of your code as possible then only define the beans that you need to set/get manually. This way you write only the XML code that you need and the rest of your instances are autowired on their own.

    Any XML or @Bean setting/injection will usually override (if not always) your @Autowire, @Qualify, and @Value annotations.

  5. #5
    Join Date
    Feb 2010
    Posts
    7

    Default

    Now I understand,

    Thanks Master

Tags for this Thread

Posting Permissions

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