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