-
Feb 18th, 2010, 01:10 PM
#1
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
-
Feb 18th, 2010, 01:13 PM
#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.
http://static.springsource.org/sprin...eference/html/
Check that document from section 3.9 through 3.14 to see what you can do.
-
Feb 18th, 2010, 03:34 PM
#3
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?
-
Feb 18th, 2010, 04:00 PM
#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.
-
Feb 18th, 2010, 04:06 PM
#5
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
-
Forum Rules