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

 更新时间:2023年01月16日 09:17:20   作者:不死鸟.亚历山大.狼崽子  
这篇文章主要介绍了SpringBoot自动装配之@Enable,SpringBoot中提供了很多Enable开头的注解,这些注解都是用于动态启用某些功能的。而其底层原理是使用@Import注 解导入一些配置类,实现Bean的动态加载

SpringBoot中提供了很多Enable开头的注解,这些注解都是用于动态启用某些功能的。而其底层原理是使用@Import注 解导入一些配置类,实现Bean的动态加载。

提问:SpringBoot 工程是否可以直接获取jar包中定义的Bean?

答:不可以

案例:

两个子模块

①子模块要得到

②子模块的User类的bean(这里用编号表示)

方法一:使用@ComponentScan扫描com.itheima.springbooyembal包

package com.enable.entity;
public class User {
}
package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
    @Bean
    public User user(){
        return new User();
    }
}

引入依赖:

        <dependency>
            <groupId>com.enable</groupId>
            <artifactId>springboot-enable-other</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

使用ComponentScan:

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.UserConfig;
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;
@SpringBootApplication
@ComponentScan("com.enable.config")
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        Object user = context.getBean("user");
        System.out.println(user);
    }
}

测试如下:

方法二:可以使用@Import注解,加载类,这些类都会被Spring创建,并放入IOC容器。

package com.enable.entity;
public class User {
}
package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
    @Bean
    public User user(){
        return new User();
    }
}

引入依赖

        <dependency>
            <groupId>com.enable</groupId>
            <artifactId>springboot-enable-other</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

使用Import注解

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.UserConfig;
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;
@SpringBootApplication
@Import(UserConfig.class)
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        Object user = context.getBean("user");
        System.out.println(user);
    }
}

测试如下:

方法三:对@Import注解进行封装

自定义@EnableUser注解

package com.enable.config;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(UserConfig.class)
public @interface EnableUser {
}

自定义配置类

package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
    @Bean
    public User user(){
        return new User();
    }
}

新建实体类:

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

引入依赖

        <dependency>
            <groupId>com.enable</groupId>
            <artifactId>springboot-enable-other</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

使用自定义的注解

package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.config.UserConfig;
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;
@SpringBootApplication
@EnableUser
public class SpringbootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        Object user = context.getBean("user");
        System.out.println(user);
    }
}

测试如下:

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

相关文章

  • SpringBoot集成RabbitMQ实现用户注册的示例代码

    SpringBoot集成RabbitMQ实现用户注册的示例代码

    这篇文章主要介绍了SpringBoot集成RabbitMQ实现用户注册的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • Spring国际化和Validation详解

    Spring国际化和Validation详解

    本文介绍了SpringBoot中国际化和Validation的融合实现,包括配置MessageSource和LocalValidatorFactoryBean,以及自定义约束注解和校验器,通过解析请求头中的Accept-Language,SpringBoot可以返回不同语言的文本信息
    2024-11-11
  • 详细介绍使用Java调用Python的四种方法

    详细介绍使用Java调用Python的四种方法

    这篇文章主要给大家介绍了关于使用Java调用Python的四种方法,每种方法根据实际项目需求有其适用场景,其中,推荐使用Runtime.getRuntime()方法,因为它更为简洁且易于实现,需要的朋友可以参考下
    2024-10-10
  • SpringBoot跨域问题的解决方法实例

    SpringBoot跨域问题的解决方法实例

    这篇文章主要给大家介绍了关于SpringBoot跨域问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • SpringBoot中定时任务@Scheduled的多线程使用详解

    SpringBoot中定时任务@Scheduled的多线程使用详解

    这篇文章主要为大家详细介绍了pring Boot定时任务@Scheduled的多线程原理以及如何加入线程池来处理定时任务,感兴趣的可以了解一下
    2023-04-04
  • Java中如何读取和写入zip文件问题

    Java中如何读取和写入zip文件问题

    这篇文章主要介绍了Java中如何读取和写入zip文件问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • SpringBoot 统一请求返回的实现

    SpringBoot 统一请求返回的实现

    这篇文章主要介绍了SpringBoot 统一请求返回的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Java开发基础日期类代码详解

    Java开发基础日期类代码详解

    这篇文章主要介绍了Java开发基础日期类的相关内容,代码通过日期工具类获取指定月份的星期与日期对应关系,以及获取指定月份的所有日期与星期集合等,具有一定参考价值,需要的朋友可以了解下。
    2017-10-10
  • java中Calendar与Date类型互相转换的方法

    java中Calendar与Date类型互相转换的方法

    这篇文章主要介绍了java中Calendar与Date类型互相转换的方法,Calendar与Date类型是我们日常开发中常用的两种数据类型,它们用于不同的场景,两者具有不同的方法,接下来通过实例给大家详解,需要的朋友可以参考下
    2022-09-09
  • JVM性能调优之运行时参数小结

    JVM性能调优之运行时参数小结

    jvm是java的运行环境,在jvm中有很多的参数可以进行设置,本文主要介绍了JVM性能调优之运行时参数小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-04-04

最新评论