Executing code on the creation or destruction of a bean in Spring

There are 3 ways to execute code to create a bean in Spring:

  • with the annotation @PostConstruct
  • implementing the interface InitializingBean
  • configuring the property init-method

The reason why you run code when you create a bean is often to check the dependencies, and assign a default value, or automated actions such as starting a scheduled task.
The constructor of the bean may not be suitable for this purpose because the bean is not fully initialized until the constructor has not completed. Continue reading “Executing code on the creation or destruction of a bean in Spring”

The Comparator interface

In the previous post about the Comparable interface I explained how you can extend a class of objects that implement the Comparable interface and then order them in a list according to the logic defined in the overridden method compareTo.
If you want to be able to sort the list according to a number of different criteria you must use the Comparator interface, for example, in the case of a list of countries, it is possible that you want to order them not only alphabetically but also in order of area or population. Continue reading “The Comparator interface”

Basic configuration of Spring Security 3 and MySQL

In this post I explain how to implement Spring Security in a web application, as I did in a previous post but the authenticated user’s username and password are not saved to a file .xml but in a MySQL database.
As in the previous post, I configure one user only trying to access index.html and he is redirected to the standard login page for authentication.
In this project I configure log4j and maven too. Continue reading “Basic configuration of Spring Security 3 and MySQL”

How to access to the members of outer class from inner class

You can’t access to the members of an outer class from an inner class using the keyword “this”.
In fact, the keyword “this” in an inner class is a reference to the inner class itself.
To have a reference to the outer class, you must use the syntax:


where [OuterClassName] is the name of the outer class. Continue reading “How to access to the members of outer class from inner class”