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来进行注入, 导致了空指针..

posted @ 2018-10-29 10:57  Senyag  阅读(3140)  评论(1编辑  收藏  举报