CodesJava

Easy learning with example program codes

Spring callback methods example


Callback method:

A callback method in java is a method which is called when an event occurs. Normally we can implement that by passing an implementation of a certain interface to the system which is responsible for triggering the event.

Commonly used callback methods in spring:

Post-initialization callback methods:

Using InitializingBean interface:

The InitializingBean interface provides afterPropertiesSet() method which can be used for any post-initialization task.

Syntax:

public class TestBean implements InitializingBean {
   public void afterPropertiesSet() {
      // post-initialization task.
   }
}

Using init-method attribute:

In XML configuration metadata we can specify the name of the method which has a void no-argument signature in init-method attribute for any post-initialization task.

Syntax:

<bean id="testBean" class="Test" init-method="init"/>
In class definition:
public class Test {
   public void init() {
      // post-initialization task.
   }
}

Pre-destroy callback methods:

Using DisposableBean interface:

The DisposableBean interface provides destroy() method which can be used for any pre-destroy task.

Syntax:

public class TestBean implements DisposableBean {
   public void destroy() {
      // pre-destroy task.
   }
}

Using destroy-method attribute:

In XML configuration metadata we can specify the name of the method which has a void no-argument signature in destroy-method attribute for any pre-destroy task.

Syntax:

<bean id="testBean" class="Test" destroy-method=" destroy"/>
In class definition:
public class Test {
   public void destroy() {
      // pre-destroy task.
   }
}

 

Spring Tutorial

Spring framework.
Spring framework architecture.
Spring ioc container.
Spring bean.
Spring bean scopes.
Spring bean life cycle.
Spring callback methods.
Spring hello world.
Spring bean definition inheritance.
Spring bean definition template.
Spring dependency injection.
Spring constructor based injection.
Constructor injection type ambiguities.
Setter based dependency injection.
Spring dependency injection collections.
Spring autowire
Spring autowire by name
Spring autowire by type
Spring autowire by constructor
Spring JDBC tutorial
Spring JDBC Prepared Statement
Spring ResultSetExtractor
Spring RowMapper
Spring aop tutorial.
Spring AOP AspectJ Xml.
Spring AOP AspectJ Annotation.
Spring MVC tutorial.
Spring mvc framework.
Spring mvc configuration file.
Spring mvc hello world.
Spring MVC multiple controller.
Spring MVC login.
Spring mvc form handling.
Spring mvc exception handling.
Spring spel tutorial.
Spring spel hello world.
Spring spel operators.
Spring spel ternary operator.
Spring spel standardevaluationcontext.
Spring spel bean reference.
Spring spel method invocation.
Spring spel list, map.
Spring spel regex.
Maven Eclipse Spring
Spring boot overview
Spring boot architecture diagram
Spring boot components
Spring boot starter parent
Spring boot web app configuration
Run spring boot application
Spring boot change port
Spring boot change context path
Spring boot log sql statements
Spring boot hello world
Spring boot JSP
Spring boot thymeleaf
Spring boot with mysql
Spring security overview
Spring security architecture
Spring security maven dependency
Spring security xml
Spring security annotation
Spring security custom login xml
Spring security custom login annotation
Spring security form login
Spring security remember me
Spring security method level
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki