Storing spring configuration in a class versus xml file - java

Currently my spring configurations are in a xml file (the traditionaly way).
One thing that i like about this is during deployment I can deploy a different version that has my production settings, or say in a test environment I can have test settings there.
I like the idea of having things configured in a class, but that will get compiled into my war and then it won't be as flexible.
Is there a way around this?

Java configuration is great and it has several advantages:
refactoring-friendly
type-safety
much more flexible (you can write any Java code, not being bound to XML semantics and capabilities).
I can deploy a different version that has my production settings, or say in a test environment I can have test settings there.
Investigate Spring #Profiles. They are orthogonal to your question (work both in XML and #Configuration) but are best suited in your situation.

Those are the only two ways available. If you don't want configuration baked in code, then you have to go with xml.

The spring reference manual includes a section on combining both Java and XML configuration. See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-java-combining
If you tend to be more XML centric you can bootstrap your app using XML and then introduce Java config as needed in an ad-hoc fashion. This might be a good way to ease into it. You might decide to go Java config all the way.

Related

XML vs Java configuration. SpringMVC web-application config

I am starting to learn about the Spring framework. I have seen two ways to configure a web application, one uses a web.xml to configure the servlets ect. The other uses a Java class that implements a WebApplicationInitializer and is annotation driven.
I was told that, "XML is the old way, no new project should use XML anymore". Can anyone tell me why this is? Lots of online resources quote "Convention over Configuration", however, using the online learning tools I have access to, the vast majority of examples are using xml configuration. I am finding it very difficult to find relevant examples with Java configuration.
I would also like to know what the pro's and con's of using one over the other are? If it it easier to find resources based around xml configuration, then would it be destructive to future job prospects to side step the Java configuration and focus on xml?
The advantages of Java Config is the type safty. The Compiler can check if you wire your application correctly (based on the types). Refactoring is a little bit easier.
Using XML configuration this can only checked during runtime.
My own opinion is that there is not a big difference between the two approches. You only "tell Spring" differnetly how to wire the application. The Java Config brings in some nice features (e.g. Spring Security Config), but also hides some "magic" which is sometimes harder to understand.
You may also have a look at earlier questions about this topic.
In Projects we still do here a lot of XML configuration which works pretty well. New Configs are often written as Java Config and integrated into the "lagacy" configs.

where to store/keep configuration data for an Java Enterprise application

What is the best way to store parameters and data for an EE7 application. I have to provide the web applications with information like a member fee or similar data (which may/can be altered several times in a year). The owner of the application should also have a central place where these data are stored and an application to change them.
Thanks in advance for any input
Franz
This is one question we are currently struggling with as we re-architect some of our back-end systems here, and I do agree with the comment from #JB Nizet that it should be stored on the database, however I will try to add some additional considerations and options to help you make the decision that is right for you. The right option will depend on a few factors though.
If you are delivering source code and automation to build and deploy your software, the configuration can be stored in a source code repository (i.e. as YAML or XML) and bundled with your deployable during the build process. This is a bit archaic but certainly widely adopted practice and works well, for the most part.
If you are delivering deployable binaries, you have a couple of options.
First one is to have a predetermined place in the file system where your application will look for an "override" configuration file (i.e. home directory of the user used to run your application server). This way you can have your binary deployable file completely separate from your configuration, but you will still need to build some sort of automation and version control for that configuration file so that your customer can roll back versions if/when necessary. This can also be one or many configuration files (i.e. separate files for your app server, vs. the application itself).
The option we are contemplating currently is having a configuration database where all of our applications can query for their own configuration. This can either be a very simple or complex solution depending on your particular needs - for us these are internal applications and we manage the entire lifecycles ourselves, but we have a need to have a central repository since we have tens of services and applications running with a good number of common configuration keys, and updating these keys independently can be error prone.
We are looking at a few different solutions, but I would certainly not store the configuration in our main database as: 1) I don't think SQL is best repository for configuration, 2) I believe we can get better performance from NoSQL databases which can be critical if you need to load some of those configuration keys for every request.
MongoDB and CouchDB both come to mind as good candidates for storing the our configuration keys if you need clearly defined hierarchy for you options, whereas Redis or Memcached are great options if you just need a key-value storage for your configuration (faster than document based too). We will also likely build a small app to help up configure and version the configuration and push changes to existing/active servers, but we haven't spec'd out all the requirements for that.
There are also some OSS solutions that may work for you, although some of them add too much complexity for what we are trying to achieve at this point. If you are using springframework, take a look at the Spring Cloud Config Project, it is very interesting and worth looking into.
This is a very interesting discussion and I am very willing to continue it if you have more questions on how to achieve distributed configurations. Food for thought, here are some of my personal must haves and nice to haves for our new configuration architecture design:
Global configuration per environment (dev,staging,prod)
App specific configuration per environment (dev,staging,prod)
Auto-discovery (auto environment selection depending on requestor)
Access control and versioning
Ability to push updates live to different services
Roger,thanks a lot. Do you have an example for the version predetermined place in the file system"predetermined place in the file system"? Does it make sense to use a singleton which reads the configuration file (using Startup annotation) and provides then the configuration data? But this does not support a dynamic solution.kind regards Franz

Utility of Annotations as compared to XML files for configuration

In Java, is it a good practice to use annotations to configure an application rather than using XML files? I am more skeptical about it because, using annotations involves changing the java source files and it is as good as declaring constants in java files and then using those constants, whereas when we make the configurations using XML files, we can keep all the configuration changes away from java source files and keep the configurations in separate XML files, this approach sounds more neat. Also, when we need to make changes to configuration, we know which XML file to change rather than searching the java files for the annotations. Also, we can update an XML file in an EAR without compiling the code again, whereas if we make any change in an annotation, then we need to compile the code again.
Can anybody please throw some light on why should we use annotations and not XML files for configuration?
Which to use may vary depending on what's being configured, how the configuration is used, project/cultural conventions, etc. Good IDE support makes using either more convenient and reliable.
Personally, while I use both XML and annotations, I tend to prefer XML configuration for many tasks, particularly on larger projects. For example, with Spring, I prefer XML configuration: it's easier to manage the configuration itself, configuration changesets, and environment-specific changes (e.g., testing, server-based, etc.), when it's more localized.
For other configurations, annotations are often more appropriate and convenient. For quick projects with little or no domain class customization, Hibernate annotations may be more convenient.
Ultimately it's a matter of preference and convenience rather than a purely technical one. (Except when the XML and annotations support different features; sometimes one offers more-complete capabilities.)
I prefer annotations since my IDE can help me validate my configurations. Configurations stored in xml-files cannot be validated before runtime (I'm thinking mostly about spring and injections)
Also, I find that for anything more than a tiny project, a large xml-config is hard to maintain.
When working with annotations, you have to take care of only one place to configure your stuff (java code). When configuring with XML, many times a programmer can "forget" to configure a new property or class in the XML, and after the error must correct and restart, resulting in a waste of time.
I would say it very much depends on what you are configuring.
If configuration maybe or should be changed after deployment, then in most cases it is preferable to use XML (or other text-based format). This would include Hibernate server configuration, Tomcat/Jetty configuration, Log4j configuration, etc. The main advantage is flexibility.
For cases when configuration does not need change after deployment, configuration using annotations is preferable. Too many configuration files also complicate your software, so it's best to keep it to a minimum. Good examples would be of annotation-based configurations: Hibernate bean mapping configuration, Spring dependency injection, Guice, etc (some give you both options, but I would prefer annotations here). The advantage is better manageability, and compile-time checking for errors (this depends on the API, of course).
Personally, when I've tried to understand a new system, having the annotations right there with the code makes it easier to follow and comprehend. Hunting for references of the class in configuration files can be a little annoying.

multiple java servers and batch programs - XML configuration nightmare

I have an application that consists of approx. 20 java components.
About half of the components are servers and the other half batch programs.
Almost all of them talk directly to an oracle database (jdbc via some of our infrastructure code jars) the other couple of components talk to some of the servers which talk to the database.
Anyway, each component is configured with numerous XML configuration files.
These are becoming almost impossible to maintain.
Some of the configuration is specific to a component others are similar (database URLs, connectors etc)
What is worse is that the application is not installed in many environments - in fact only about 10 environments (qa, dev, production etc etc).
But the people who own these environments don't seem able to maintain the configs correctly.
In particular whenever there is an upgrade there is invariably configuration errors.
I have even started checking in some of the environments configurations into SVN along with the code.
I tried an xml schema validator at one point (it consisted of defining the valid XML in .xsd files and then throwing an error if the schema rules were breached but that didnt work)
I'm thinking I am missing something basic here - perhaps there is a tool to manage this or perhaps I should be storing the configuration in the database.
The application was largely designed by a colleague but I feel myself that it's overly configurable - in fact many of the config actually refers to classes - i.e. one can choose handlers and parsers etc - the XML config almost looks like code.
Any advice greatly appreciated
Peter
Substituting XML for code is usually a bad idea; things that are declarative are probably OK, but things that are procedural probably aren't.
If all that configuration was defined in Java code, a lot of the upgrade issues would turn into compilation issues. The compiler would pick them out for you, and you could correct them.
So you've got a multi-part problem. You need to rationalize your configuration information into a set of partitions (per-component, per-installation, global). You need to try to verify configuration information at compile-time, where possible. And you need to write validation for the loaded configurations, to sanity check them.
To the extent possible, shift configuration relatively static stuff into Guice (at least, it's what I prefer). A lot of things happen in a nice, type-safe way with it.
Consider running a WebDAV server for each instance of the app, and storing configuration into it. Each can hit a simple URL to pull the current versions of the configuration files.
Or, stand up a lightweight XML database like BaseX with its REST capability, then store and load your configuration information there. Use JSLP or something like it to have your components find the central configuration repository.
An additional advantage to using an XML DB is that you'll be able to do a lot of sanity checking and updating by querying across the set of all configuration files. For example, if a given instance of the application should have the same JDBC parameters in each configuration file, a simple xquery will tell you if that's true.
If you don't have the ability to modify the applications that are pulling the configuration file (the config file format is fixed), then consider writing a query servlets for the XML database that assemble the required configuration information, from nested blocks or templates. That will allow you to figure out what's common between the configuration files and dynamically generate parameterized versions of those blocks.
Sounds like the key here is making incremental improvements. Allow the old way to configure, but have the configuration load look for a central config source first.
I don't think that the syntax of the configuration files is at the heart of the problem: using Java properties files instead of XML would leave you with exactly the same issues. There may be an issue that the configuration information is too dispersed - it's hard to tell. The main issue seems to be that the whole thing is too fragile - the application is too dependent on manual configuration, and it seems that the configuration for each environment needs to be different. You should try to focus on reducing the number of configuration parameters that need to be set to make the system work (without necessarily reducing the options available for diagnostics etc for use when they are really needed.), on having intelligent defaults and self-configuration. Perhaps even invest in creating an installation wizard.
As you have some Oracle databases handy why not store your configuration in there?
Then you only need one or two configuration parameters to point to an Oracle database suitable for that environment and download the rest of the configuration from the database.
The contents of the configuration table should be pretty static for any given environment so there should be no need to amend anything except the jdbc connection when you migrate your software through its life cycle.

Best practices when using Spring 3 annotations

I'm looking for some best practices when using Spring 3 annotations.
I'm currently moving to Spring 3 and from what I've read so far I see a lot of accent placed on using annotations and moving away from XML configuration.
Actually what is recommended is a mix of both styles, with annotations covering things that won't change often or from one run to the next (e.g. a #Controller will remain like that for the life time of the application), while the things that change and must be configurable go into XML (e.g. a mail smtp address, endpoints for web services that your application talks to etc).
My question is what should go into annotations and to what extent?
At which point annotations make things harder instead of easier? Is the technology (Spring 3) fully adopted as to be able to make such statements or does it take some more time for people to gain experience with it and then reflect on the issue?
It is always difficult to get real advanced information.
The easy tutorial with "look on my blog, I copied the hello word tutorial from Spring Source website... Now you can put fancy annotations everywhere, it the solution of all of our problems including cancer and starvation." is not really usefull.
If you remember right spring core had several purposes, among them:
to be non intrusive
to change any
implementation/configuration of a
bean at any time
to give a centralized and controlled
place to put your configuration
Anotation fail for all theses needs:
They introduce coupling with spring
(you can use standard anotation only
but as soon as you have at least one
spring anotation this is no longer
true)
You need to modify source code and
recompile to change bean
implementation or configuration
Annotations are everywhere in your
code. It can be difficult to find
what bean will be really used just by
reading the code or XML configuration
file.
In fact we have shifted our focus:
We realized that we almost never
provide several implementations of
our services.
We realised that being dependant of
an API is not that bad.
We realized that we use spring not only
for real dependancy injection
anymore, but mainly to increase
productivity and reduce java code
verbosity.
So I would use anotations when it make sence. When it is purerly to remove boilerplate code, verbosity. I would take care of using the XML configuration file for thing that you want to make configurable, even if it is only to provide a stub implementation of the service in unit tests.
I use #Value for properties that are configured in external properties file via PropertyPlaceholderConfigurer, as kunal noted.
There is no strict line for when to use xml, but I use xml:
when the bean is not a class I control
when the object is related to the infrastructure or configuration rather than to the business logic.
when the class has some primitive properties that I would like configurable, but not necessarily via externalized configurations.
In response to your comment: spring is very widely adopted, but "good" and "bad" are very subjective. Even my lines are not universal truths. XML, annotations and programmatic configuration all exists for a purpose, and each developer / company have their preferences.
As I said - there is no strict line, and no universal good practice for annotations.
Annotations are surely the way by which "newer" programming in java will continue. I use annotations for various uses...like #Scope for scope of bean, #Required for making dependency necessary, #Aspect for configuring advices,#Autowired for constructor injection using annotations. Since spring 2.5, annotation support has been good.
See here for spring tutorial, where annotation based issue are covered here.
I think that two cases that the usage of annotations could cause some problems. Firstly, if you want to write complex named queries (JPA) in your entities. I saw some entity code samples and asked myself whether the code is really java code or not. To many metadata in program code will reduce the readability of it which kills clean code principles.
Second problem is portability between JVM versions. Annotation is a feature 1.5+. If your software should support earlier JVM versions, then you may not use these.
Anyway, you can enjoy with annotations everytime without having any doubt and spare your time not changing IDE tabs to check XMLs if the property is still there or not, or entered correct etc.
For very small projects you could still XML version if you haven't too many stuff to be declared in spring. But, if you are in a huge project, the things could be very troublesome if you had 10 xml configs.
This will perhaps not help you much but at work they don't want to use autowiring because it needs a classpath scan (but that can be package-defined i think). So it increases the startup time of the application according to the size of the project.

Categories