Section 2: Showcasing Core Spring Features by Example

Description

This self-contained running example showcases various core Spring features, including minimum dependencies, Java Configuration, Bean declaration by annotation and Java Configuration, Dependency Injection by Constructor and by Property, and Pre/Post hooks.

Dependencies

Ensure the following dependencies are in the classpath:

  1. spring-core
  2. spring-context
  3. spring-beans
  4. spring-aop
  5. spring-expression
  6. commons-logging

Main Class

The main class serves as a placeholder for the main() method, initializing the Application Context using the Configuration class and loading beans to showcase specific functionality.

package com.stackoverflow.documentation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        // Initializing the Application Context once per application.
        ApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AppConfig.class);

        // Bean registered by annotation
        BeanDeclaredByAnnotation beanDeclaredByAnnotation =
                applicationContext.getBean(BeanDeclaredByAnnotation.class);
        beanDeclaredByAnnotation.sayHello();

        // Bean registered by Java configuration file
        BeanDeclaredInAppConfig beanDeclaredInAppConfig =
                applicationContext.getBean(BeanDeclaredInAppConfig.class);
        beanDeclaredInAppConfig.sayHello();

        // Showcasing constructor injection
        BeanConstructorInjection beanConstructorInjection =
                applicationContext.getBean(BeanConstructorInjection.class);
        beanConstructorInjection.sayHello();

        // Showcasing property injection
        BeanPropertyInjection beanPropertyInjection =
                applicationContext.getBean(BeanPropertyInjection.class);
        beanPropertyInjection.sayHello();

        // Showcasing PreConstruct / PostDestroy hooks
        BeanPostConstructPreDestroy beanPostConstructPreDestroy =
                applicationContext.getBean(BeanPostConstructPreDestroy.class);
        beanPostConstructPreDestroy.sayHello();
    }
}

Application Configuration file

The configuration class is annotated with @Configuration and is used as a parameter in the initialized Application Context. The @ComponentScan annotation at the class level of the configuration class points to a package to be scanned for Beans and dependencies registered using annotations. Finally, the @Bean annotation serves as a bean definition in the configuration class.

package com.stackoverflow.documentation;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.stackoverflow.documentation")
public class AppConfig {
    @Bean
    public BeanDeclaredInAppConfig beanDeclaredInAppConfig() {
        return new BeanDeclaredInAppConfig();
    }
}

Bean Declaration by Annotation

The @Component annotation demarcates the POJO as a Spring bean available for registration during component scanning.

@Component
public class BeanDeclaredByAnnotation {
    public void sayHello() {
        System.out.println("Hello, World from BeanDeclaredByAnnotation !");
    }
}