Main menu

Spring: Make your java-based configuration more elegant

Hi everyone, I haven’t written new articles a long period of time. A lot of materials were accumulated which need to be posted in my blog in nearest future. But now I want to talk about Spring MVC application configurations. If to be more precisely, I want to talk about java based Spring configurations.
Despite that Spring java based configuration was introduced in 3.0 version, many developers still use XML-based approach. Personally I use annotation based configurations, because they are more convenient in management, in development and in maintenance. If you have read my blog before you could noticed that in all code examples I used exactly java based configurations.

Not so long time ago I have made a code review of one my project. I noticed that something is wrong with a structure of the configurations. Two aspects were bad as for me:

All beans were configured in a single class

Initialization on web app context was too complex

You can witness these two drawbacks in all my examples of Spring MVC applications. For example you can open one of my last tutorials about Spring REST services with CNVR. Take a look there on two classes: WebAppConfig and Initializer.

WebAppConfig

The first one represents the first point in this article. Definitely I need to do something to split beans configuration logically. To resolve this issue I decided to make two steps:

In this way you can separate single big configuration class in several smaller which will contain specific configs for them.

Initializer

Code of Initializer class is too verbose in the example which I mentioned above and provided the link to it. I registered there the web application root configurations, mapping and filter. How I can decrease a number of code lines? The answer I got looking on AbstractAnnotationConfigDispatcherServletInitializer class. Look on the class and you will notice that it implements WebApplicationInitializer interface, which I have implmented in my previous version of the Initializer class. So here is a new version of the Initializer:

Share and Enjoy

Your DatabaseConfig file is very interesting. From this inspiration I would like to change my Spring with JSF integration project to Java Config. My project uses Atomikos Transaction Manager and Apache DBCP Data Source for pooling.

i am glad to have your answer. My folder structure is something like this

WEB-INF
—- resources
—-properties
—settings
—-jsps

……

You mean- propertiySourcesPlaceholderConfigurer(ClassName.class.getClassLoader().getResourceAsStream(properties/settings/*.properties) in the PropertiySourcesPlaceholderConfigurer bean configuration right?

What is the ClassName to provide?

Whether i have to include /WEB-INF in the getResourcAsStream() or not?

Radha

Hi Alex Zvolinskiy,

i have followed this notes to create and destroy the http session. It is getting created and destroyed but, it is not redirected to specified url in spring security configuration as shown below,