Announcement Announcement Module
No announcement yet.
XML and annotations Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • XML and annotations


    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
    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.

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


    • #3

      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

      class Service {}

      and XML

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

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

      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
        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
          Now I understand,

          Thanks Master