How to set a default method to running code on the creation or destruction of a bean

In the post Executing code on the creation or destruction of a bean in Spring I explain three ways to execute code on the creation or destruction of a Bean in Spring.
With two of these ways you can set the execution of a specific custom method:

  • with the annotations @PostConstruct and @PreDestroy
  • configuring the properties init-method and destroy-method

There is a very convenient way in the case that more beans have methods with the same name to be executed on their creation and destruction, i.e. you can specify a default method for all the beans.
Continue reading “How to set a default method to running code on the creation or destruction of a bean”

MessageSource in Spring 3

The MessageSource interface supports the parameterization and internationalization of messages and provides 2 implementations:

  1. ResourceBundleMessageSource, built on top of the standard ResourceBundle
  2. ReloadableResourceBundleMessageSource, being able to reload message definitions without restarting the VM

The following is an example of ResourceBundleMessageSource to get messages in the specified language in Spring 3.
Continue reading “MessageSource in Spring 3”

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”

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”

Basic configuration of Log4j in Spring using a .xml file

There are 2 ways to configure Log4j in Spring using:

  • a .properties file
  • a .xml file

The configuration with the. xml file allows to take advantage of some aspects that can not be configured with the .properties file that is therefore regarded as obsolete.
This article will explain how to configure Log4j using the .xml file and the configuration with .properties file was treated in a previous article. Continue reading “Basic configuration of Log4j in Spring using a .xml file”

Basic configuration of Log4j in Spring using a .properties file

There are 2 ways to configure Log4j in Spring using:

  • a .properties file
  • a .xml file

The configuration with the. xml file allows to take advantage of some aspects that can not be configured with the .properties file that is therefore regarded as obsolete.
This article will explain how to configure Log4j using the .properties file and the configuration with .xml file will be treated in a subsequent article. Continue reading “Basic configuration of Log4j in Spring using a .properties file”