飞道的博客

【SpringBoot】@Enable*注解和@Import

447人阅读  评论(0)

@Enable*

创建一个主启动类


  
  1. package com.you.boot;
  2. import com.you.config.EnableUser;
  3. import com.you.config.UserConfig;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.context.ConfigurableApplicationContext;
  7. import org.springframework.context.annotation.ComponentScan;
  8. import org.springframework.context.annotation.Import;
  9. @SpringBootApplication
  10. public class BootEnable02Application {
  11. public static void main (String[] args) {
  12. ConfigurableApplicationContext context = SpringApplication.run(BootEnable02Application.class, args);
  13. // 获取Bean
  14. Object user = context.getBean( "user");
  15. System.out.println(user);
  16. }
  17. }

 新建一个Module

【如果创建完,不是并列,如下图(并列),而是包含】

【错误原因】路径写错了,需要把红框类的去掉

在新Module里写一个用户类和配置类

User类


  
  1. package com.you.domain;
  2. public class User {
  3. }

 UserConfig


  
  1. package com.you.config;
  2. import com.you.domain.User;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration
  6. public class UserConfig {
  7. @Bean
  8. public User user ()
  9. {
  10. return new User();
  11. }
  12. }

 在主启动类的Pom里引入配置类的依赖


  
  1. <dependency>
  2. <groupId>com.you</groupId>
  3. <artifactId>boot-enable-other</artifactId>
  4. <version> 0.0 .1-SNAPSHOT</version>
  5. </dependency>

启动。问题SpringBoot不可以直接获取Jar包中定义的Bean

解决办法

解放方案一:

@ConponentScan("配置类包名")

 

 效果:

解决方案2:@Import

效果图​​​​​​​

 解决方案3:@Import的封装

在配置类包下,创建如下类


  
  1. package com.you.config;
  2. import org.springframework.context.annotation.Import;
  3. import java.lang.annotation.*;
  4. @Target({ElementType.TYPE})
  5. @Retention(RetentionPolicy.RUNTIME)
  6. @Documented
  7. @Import(UserConfig.class)
  8. public @interface EnableUser {
  9. }

 效果:

@Import

@Import提供了四种用法

1、导入Bean

2、导入配置类

3、导入ImportSelector实现类,一般用于加载配置文件的类

4、导入ImportBeanDefinitionRegistrar实现类

1、导入Bean


  
  1. @SpringBootApplication
  2. @Import(User.class)
  3. public class BootEnable02Application {
  4. public static void main (String[] args) {
  5. ConfigurableApplicationContext context = SpringApplication.run(BootEnable02Application.class, args);
  6. User user = context.getBean(User.class);
  7. System.out.println(user);
  8. }
  9. }

 可以获取到

 2、导入配置类,和上面@Enable*详解中的一样

3、 导入ImportSelector实现类,一般用于加载配置文件的类

首先需要写一个ImporeSelector的实现类,还是写在config包下


  
  1. package com.you.config;
  2. import org.springframework.context.annotation.ImportSelector;
  3. import org.springframework.core.type.AnnotationMetadata;
  4. public class MySelector implements ImportSelector {
  5. @Override
  6. public String[] selectImports(AnnotationMetadata importingClassMetadata) {
  7. return new String[]{ "com.you.domain.User"};
  8. }
  9. }

效果 

 4、导入ImportBeanDefinitionRegistrar实现类

首先需要写一个ImportBeanDefinitionRegistrar实现类,同样写在config包下


  
  1. package com.you.config;
  2. import com.you.domain.User;
  3. import org.springframework.beans.factory.support.AbstractBeanDefinition;
  4. import org.springframework.beans.factory.support.BeanDefinitionBuilder;
  5. import org.springframework.beans.factory.support.BeanDefinitionRegistry;
  6. import org.springframework.beans.factory.support.BeanNameGenerator;
  7. import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
  8. import org.springframework.core.type.AnnotationMetadata;
  9. public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
  10. @Override
  11. public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
  12. AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();
  13. registry.registerBeanDefinition( "user",beanDefinition);
  14. }
  15. }

效果

​​​​​​​

 


转载:https://blog.csdn.net/m0_59792745/article/details/125823408
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场