通过示例展示 Core Spring 功能

描述

这是一个独立的运行示例,包括/ showcasing: 所需的最小依赖项,Java 配置,注释和 Java 配置的 Bean 声明,构造函数和属性的依赖注入,以及 Pre / Post 挂钩。

依赖

类路径中需要这些依赖项:

  1. 弹簧核心
  2. 春天上下文
  3. 弹簧豆
  4. 春天的 AOP
  5. 弹簧表达
  6. 共享记录

主类

从最后开始,这是我们的 Main 类,它作为 main() 方法的占位符,通过指向 Configuration 类来初始化 Application Context 并加载展示特定功能所需的所有各种 bean。

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();
    }
}

应用配置文件

配置类由 @Configuration 注释,并在初始化的应用程序上下文中用作参数。配置类的类级别的 @ComponentScan 注释指向要扫描的 Bean 和使用注释注册的依赖项的包。最后,@Bean 注释在配置类中用作 bean 定义。

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 声明

@Component 注释用于将 POJO 划分为可在组件扫描期间注册的 Spring bean。

@Component
public class BeanDeclaredByAnnotation {

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

应用程序配置的 Bean 声明

请注意,我们不需要注释或以其他方式标记我们的 POJO,因为 bean 声明/定义发生在 Application Configuration 类文件中。

public class BeanDeclaredInAppConfig {

    public void sayHello() {
        System.out.println("Hello, World from BeanDeclaredInAppConfig !");
    }
}

构造函数注入

请注意,@Autowired 注释是在构造函数级别设置的。另请注意,除非通过名称明确定义,否则将根据 bean 的类型 (在本例中为 BeanToBeInjected) 进行默认自动装配。

package com.stackoverflow.documentation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BeanConstructorInjection {

    private BeanToBeInjected dependency;

    @Autowired
    public BeanConstructorInjection(BeanToBeInjected dependency) {
        this.dependency = dependency;
    }

    public void sayHello() {
        System.out.print("Hello, World from BeanConstructorInjection with dependency: ");
        dependency.sayHello();
    }
}

属性注入

请注意,@Autowired 注释划分了名称遵循 JavaBeans 标准的 setter 方法。

package com.stackoverflow.documentation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BeanPropertyInjection {

    private BeanToBeInjected dependency;

    @Autowired
    public void setBeanToBeInjected(BeanToBeInjected beanToBeInjected) {
        this.dependency = beanToBeInjected;
    }

    public void sayHello() {
        System.out.println("Hello, World from BeanPropertyInjection !");
    }
}

PostConstruct / PreDestroy 挂钩

我们可以通过 @PostConstruct@PreDestroy 钩子拦截 Bean 的初始化和破坏。

package com.stackoverflow.documentation;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class BeanPostConstructPreDestroy {

    @PostConstruct
    public void pre() {
        System.out.println("BeanPostConstructPreDestroy - PostConstruct");
    }

    public void sayHello() {
        System.out.println(" Hello World, BeanPostConstructPreDestroy !");
    }

    @PreDestroy
    public void post() {
        System.out.println("BeanPostConstructPreDestroy - PreDestroy");
    }
}