开发者

SpringBoot自动装配之@Import深入讲解

开发者 https://www.devze.com 2023-01-16 10:43 出处:网络 作者: 不死鸟.亚历山大.狼崽子
目录1 导入Bean2 导入配置类3 导入ImportSelector 实现类4 导入ImportBeanDefinitionRegistrar实现类@Enable*底层依赖于@Import注解导入一些类,使用@Import导入的类会被Spring加载到IOC容器中。而@Import提供4中用法
目录
  • 1 导入Bean
  • 2 导入配置类
  • 3 导入ImportSelector 实现类
  • 4 导入ImportBeanDefinitionRegistrar实现类

@Enable*底层依赖于@Import注解导入一些类,使用@Import导入的类会被Spring加载到IOC容器中。而@Import提供4中用法:

  • 导入Bean
  • 导入配置类
  • 导入ImportSelector 实现类。一般用于加载配置文件中的类
  • 导入ImportBeanDefinitionRegistrar实现类。

1 导入Bean

定义一个user实体类:

package com.enable.entity;
public class User {
}

测试功能:

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.UserConfig;
import com.enable.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import Java.util.Map;
@SpringBootApplication
@Import(User.class)
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        User user = context.getBean(User.class);
        System.out.println(user);
        Map<String,User> map = context.getBeansOfType(User.class);
        System.out.println(map);
    }
}

结果如下:

SpringBoot自动装配之@Import深入讲解

2 导入配置类

新建实体类:

package com.enable.entity;
public class Role {
}
package com.enable.entity;
public class User {
}

测试功能:

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.UserConfig;
import com.enable.entity.Role;
import com.enable.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframe编程work.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import java.util.Map;
@SpringBootApplication
@Import(UserConfig.class)
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        User user = context.getBean(User.class);
        System.out.println(user);
        Role role = context.getBean(Role.class);
        System.out.pwww.devze.comrintln(role);
    }
}

测试结果:

SpringBoot自动装配之@Import深入讲解

3 导入ImportSelector 实现类

新建实体类:

package com.enable.entity;
public class Role {
}
package com.enable.entity;
public class User {
}

编写ImportSelector:

package com.enable.config;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.enable.entity.Role","com.enable.entity.User"};
    }
}

测试功能:

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.MyImportSelector;
import com.enable.config.UserConfig;
import com.enable.entity.Role;
import com.enable.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import java.util.Map;
@SpringBootApplication
@Import(MyImportSelector.class)
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        User user = context.getBean(User.class);
        System.out.println(user);
        Role role = context.getBean(Role.class);
        System.out.println(role);
    }
}

结果如下:

SpringBoot自动装配之@Import深入讲解

4 导入ImportBeanDefinitionRegistrar实现类

新建实体类:

package com.enable.entity;
public class User {
}

ImportBeanDefinitionRegistrar实现类

package com.enable.config;
import com.enable.entity.User;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();
        registry.registerBeanDefinition("user",beanDefinition);
    }
}

测试结果:

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.MyImportBeanDejavascriptfinitionRegistrar;
import com.enable.config.MyImportSelector;
import com.enable.config.UserConfig;
import com.enable.entity.Role;
import com.enable.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springf编程客栈ramework.context.annotation.Import;
import java.util.Map;
@SpringBootApplication
@Import(MyImportBeanDefphpinitionRegistrar.class)
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        Object user1 = context.getBean("user");
        System.out.println(user1);
        User user = context.getBean(User.class);
        System.out.println(user);
    }
}

结果如下:

SpringBoot自动装配之@Import深入讲解

到此这篇关于SpringBoot自动装开发者_JS学习配之@Import深入讲解的文章就介绍到这了,更多相关SpringBoot @Import内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号