SpringFramework|@Import注解的使用


@Import注解的使用

前述

Java: 1.8

Maven: 3

SpringFramework版本以及各組件成員: 5.1.1.RELEASE

  • spring-context
  • spring-core
  • spring-beans

(@yag)

@Import - 表示要導入的一個或多個@Configuration類。

就像在Spring XML文件中使用<import/>元素來幫助模塊化配置一樣,@ Immort注釋允許從另一個配置類加載@Bean定義,如下例所示:

以下是一個抽象的使用例子:

@Configuration
public class ConfigA {

    @Bean
    public A a() {
        return new A();
    }
}

@Configuration
@Import(ConfigA.class)
public class ConfigB {

    @Bean
    public B b() {
        return new B();
    }
}

然后在執行類中:

public static void main(String[] args) {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigB.class);

    // now both beans A and B will be available...
    A a = ctx.getBean(A.class);
    B b = ctx.getBean(B.class);
}

示例(跨JavaConfig實例的依賴Bean)

之前這篇記錄文章沒有核查仔細就匆匆下結論, 給需要的人指了條錯路, 實在抱歉...

HelloWorld是一個bean, 而HelloWorldUser需要一個HelloWorld實例來調用HelloWorld中的方法sayHello().

這里使用兩個分別的Config: AB, 一個配置HelloWorld; 一個配置HelloWorldUser. (它們之間存在依賴關系)

Bean - HelloWorld.java

package yag;

import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloWorld {

    public void sayHelloWorld(){
        System.out.println("Hello World");
    }
}

Bean使用者 - HelloWorldUser.java

package yag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloWorldUser {

    private HelloWorld helloWorld;

    @Autowired
    public void setHelloWorld(HelloWorld helloWorld) {
        this.helloWorld = helloWorld;
    }

    public void useBean(){
        helloWorld.sayHelloWorld();
    }
}

兩個配置文件 - ConfigA.javaConfigB.java

ConfigA.java:

package yag;

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

@Configuration
public class ConfigA {

    @Bean
    public HelloWorld helloWorldBean(){
        return new HelloWorld();
    }
}

ConfigB.java:

package yag;

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

@Configuration
@Import(ConfigA.class)
public class ConfigB {

    @Bean
    public HelloWorldUser beanUser(){
        return new HelloWorldUser();
    }
}

注解掃描 - spring-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config/>
<context:component-scan base-package="yag"/>
</beans>

執行者 - Runner.java

package yag;

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

public class Runner {

    public static void main(String[] args){
        ApplicationContext context = new AnnotationConfigApplicationContext(ConfigB.class);
        HelloWorldUser helloWorldUser = context.getBean(HelloWorldUser.class);
        helloWorldUser.useBean();
    }
}

執行結果

Hello World

Process finished with exit code 0

反過來Import呢?

將ConfigB反過來Import到ConfigA中, 此處省略代碼.

執行者 - Runner.java

package yag;

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

public class Runner {

    public static void main(String[] args){
        ApplicationContext context = new AnnotationConfigApplicationContext(ConfigA.class);
        HelloWorldUser helloWorldUser = context.getBean(HelloWorldUser.class);
        helloWorldUser.useBean();
    }
}

執行結果(可以)

Hello World

Process finished with exit code 0

抱歉

之前匆忙下結論, 實際上是自己檢查不仔細, 沒有@Autowired來進行注入, 導致了空指針..


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM