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.

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.

@Autowired and @Resource in Spring

The @Autowired annotation is used to configure the dependency injection in Spring and is associated with a setter method, or a field or a constructor of a class.
In the case of a setter method or a field the bean used for dependency injection is chosen depending on the type.

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.

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.

Basic configuration of Spring Security 3

In this post I write about a basic configuration of Spring Security 3, the Spring framework for authentication and authorization of the users.
In this example there is only one user trying to access to index.html and he is redirected to the standard login page for authentication.
In this project I use and configure maven and log4j too.

Setting MyBatis in Spring

MyBatis, a free software distributed under the Apache License 2.0, helps to connect Spring with relational databases using XML or annotations.
Here’s a simple example loading a jsp page to display data from a mysql table.

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.

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.