通過示例展示 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");
    }
}