HI when would annotation actually be good. When would using xml configuration be beneficial.
Is it good to have mixed or not. Is the rule never use xml configuration good or never use annotation good.
Thanks
HI when would annotation actually be good. When would using xml configuration be beneficial.
Is it good to have mixed or not. Is the rule never use xml configuration good or never use annotation good.
Thanks
Both have their uses, both have pros and cons and a lot depends on personal preference.
When using annotations you will reduce the amount of xml, the big drawback here is is that your configuration is scattered around all your classes. No central location for your configuration.
When using xml it can become verbose, but you have 1 location to look for your configuration.
So I tend to end up with a mixture of both. The coarse grained configuration is done in xml and the fine grained is done within the class. So that you have a somewhat reduced configuration and a central place for your main configuration and finer grained inside your classes. For me it works.
For making your configuration clear also tools like Spring IDE can really help.
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
thanks.
HOw about in terms of using hibernate search which supports using annotations only? Would that count as "fined grained".
Personally, I avoid annotations. In the purest of POJO designs, using an annotation couples that source file to the provider of the annotation. The config file approach, while perhaps being slightly more effort to maintain, preserves the POJO in that your source code has no direct coupling.
I strongly agree with this point. Furthermore a centralized location of configuration is important to me.
In some cases though maybe annotations fit better.
I see there is a @Controller annotation in spring 2.5.x but I have not gone through it yet. While annotating your services and domain layer is invasive, annotating controllers is ok since they are already framework dependant.
I concur with this point. Despite many peoples adversion to xml config (or any external config) I find it better to decouple from code as much as possible with frameworks and true config type data. Now, annotations that are part of the base language structure I use including ones that allow injection of Spring items. In addition, having all your config / injection in one place allows for easy knowledge of where a bean is used.
Another place where I often 'violate' this standard is in JUnit tests where I annotate injection more to save on test specific config. At least so far that has not been a 'bad' thing.
Totally agree with this, its really useful to be able to reference a single location to get the bigger picture about your application flow and basic 'plumbing'.
I think viewnames for controllers (e.g. formView, successView and so on) come under configuration as well and for goods reasons, for instance if you need to reuse the logic in a given controller for presenting different views and the fact that you dont want to hardcode the flow of the application in your code.
Specifying viewnames in spring config was done by convention before the introduction of Annotated controllers in 3.0 because the good old Abstract Controller classes sort of 'advised' us to do so along with some other stuff. However after the advent of Annotated controllers, a lot of the 'guiding principles' (some viewed these as constraints) are gone with the abstract controllers getting deprecated in 3.0.
For people who despise XML configuration annotations is seen as the way to go to get to this utopia zero XML config controller configuration, and in getting to this the tendency is to hardcode things like viewnames in the controllers just to get away with not having it in configuration which I think is wrong as you are taking away the flexibility of your application just to save a handful of XML config.
Any thoughts?
I prefere annotations as it is clearer and you have the information right there where the code is as well - so i feel its easier to maintain. However annotations have the drawback that you can't change them without recompiling the code. So if you have settings that need to be configured at install time e.g. performance tuning etc. then xml configuratio might be the better choice.
Annotations should be used for the metadata that is rarely going to change and xml should be used for the configuration that needs to be changed frequently.I believe that most of the data we puts in xml is rarely needs to be changed and hence they are better to be put as annotations.Therefore,in spring version 3.0,the @Configuration,@Bean annotations is introduced to rid off from the xml.