【好奇害死猫】@import

1、简介

@import 是用来倒入配置class或者是导入前置需要加载的class。

2、源码解析

2.1、导入配置的三种类型

@Import支持三种方式
1、带有@Configuration的配置类(4.2之前只支持配置类,4.2之后支持普通类)。
2、ImportSelector的实现。
3、ImportBeanDefinitionRegistrar 的现实

2.2、@Import源码

/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Indicates one or more <em>component classes</em> to import &mdash; typically
 * {@link Configuration @Configuration} classes.
 *
 * <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
 * Allows for importing {@code @Configuration} classes, {@link ImportSelector} and
 * {@link ImportBeanDefinitionRegistrar} implementations, as well as regular component
 * classes (as of 4.2; analogous to {@link AnnotationConfigApplicationContext#register}).
 *
 * <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes should be
 * accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
 * injection. Either the bean itself can be autowired, or the configuration class instance
 * declaring the bean can be autowired. The latter approach allows for explicit, IDE-friendly
 * navigation between {@code @Configuration} class methods.
 *
 * <p>May be declared at the class level or as a meta-annotation.
 *
 * <p>If XML or other non-{@code @Configuration} bean definition resources need to be
 * imported, use the {@link ImportResource @ImportResource} annotation instead.
 *
 * @author Chris Beams
 * @author Juergen Hoeller
 * @since 3.0
 * @see Configuration
 * @see ImportSelector
 * @see ImportBeanDefinitionRegistrar
 * @see ImportResource
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {

	/**
	 * {@link Configuration @Configuration}, {@link ImportSelector},
	 * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.现在应该是所有的class 都可以加载了。
	 */
	Class<?>[] value();

}

3、实践

3.1、测试用例

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
class NormalClassTest {

    @Autowired
    private NormalClass normalClass;

    @Autowired
    private NormalConfig normalConfig;

    @Autowired
    private NormalSelectorClass normalSelectorClass;

    @Autowired
    private NormalRegisterClass normalRegisterClass;

    @Test
    void print() {
        normalClass.print();
        normalConfig.print();
        normalSelectorClass.print();
        normalRegisterClass.print();
    }
}

3.2、AllConfig 导入需要导入的class

package com.example.demo;

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

@Import({NormalClass.class,NormalConfig.class,NormalSelector.class,NormalImportBeanDefinitionRegistrar.class})
@Configuration
public class AllConfig {
}

3.3、NromalClass 普通类

package com.example.demo;

public class NormalClass {
    public void print() {
        System.out.println("NromalClass");
    }
}

3.4、NormalConfig 有config注解的类

package com.example.demo;

import org.springframework.context.annotation.Configuration;

@Configuration
public class NormalConfig {

    public void print() {
        System.out.println("NormalConfig");
    }

}

3.5、NormalSelectorClass| ImportSelector导入class

package com.example.demo;

public class NormalSelectorClass {
    public void print(){
        System.out.println("NormalSelectorClass");
    }
}

package com.example.demo;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

public class NormalSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        System.out.println(NormalSelectorClass.class.getName());
        return new String[]{NormalSelectorClass.class.getName()};
    }
}

3.6、NormalRegisterClass |ImportBeanDefinitionRegistrar导入class

package com.example.demo;

public class NormalRegisterClass {
    public void print(){
        System.out.println("NormalRegisterClass");
    }
}

package com.example.demo;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class NormalImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        RootBeanDefinition root = new RootBeanDefinition(NormalRegisterClass.class);
        registry.registerBeanDefinition("normalRegisterClass", root);
    }

}

4、总结

4.1、Import可以导入所有的Class。

4.2、ImportSelector导入可以批量导入Class。

4.3、ImportBeanDefinitionRegistrar 方式注册bean。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌路Molu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值