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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • spring学习之util:properties的使用

    spring学习之util:properties的使用

    这篇文章主要介绍了spring学习之util:properties的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Java AES256加密解密示例代码

    Java AES256加密解密示例代码

    这篇文章主要介绍了Java AES256加密解密示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • JavaMailSender实现邮箱验证功能

    JavaMailSender实现邮箱验证功能

    本篇文章主要给大家介绍了JavaMailSender实现邮箱注册验证的功能实现原理以及其中遇到的问题,一起跟着学习探讨下吧。
    2017-12-12
  • Spring BeanPostProcessor(后置处理器)的用法

    Spring BeanPostProcessor(后置处理器)的用法

    这篇文章主要介绍了Spring BeanPostProcessor(后置处理器)的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Spring Boot下如何自定义Repository中的DAO方法

    Spring Boot下如何自定义Repository中的DAO方法

    这篇文章主要介绍了Spring Boot下如何自定义Repository中的DAO方法,需要的朋友可以参考下
    2017-06-06
  • IDEA标签tabs多行显示的设置

    IDEA标签tabs多行显示的设置

    这篇文章主要介绍了IDEA标签tabs多行显示的设置方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Spring Boot 开发环境热部署详细教程

    Spring Boot 开发环境热部署详细教程

    这篇文章主要介绍了Spring Boot 开发环境热部署,本文给大家介绍了Spring Boot 开发环境热部署的原理及快速配置方法,通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • JavaWeb中Struts2拦截器深入分析(一)

    JavaWeb中Struts2拦截器深入分析(一)

    这篇文章主要为大家详细介绍了JavaWeb中Struts2拦截器的功能,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Java中List集合去重的几种方式详细解析

    Java中List集合去重的几种方式详细解析

    这篇文章主要介绍了Java中List集合去重的几种方式详细解析,在日常的业务开发中,偶尔会遇到需要将 List 集合中的重复数据去除掉的场景,那么今天我们来看看几种LIst集合去重的方式,需要的朋友可以参考下
    2023-11-11
  • idea git未提交代码文件名字变色(图解)

    idea git未提交代码文件名字变色(图解)

    这篇文章主要介绍了idea git未提交代码文件名字变色,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04

最新评论