栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

@Enbale*注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

@Enbale*注解

首先查看引导类中的注解@SpringBootApplication,有@SpringBootConfiguration与@EnableAutoConfiguration、@ComponentScan

继续查看@SpringBootConfiguration注解里有Configuration,所以添加了@SpringBootApplication就是配置类,里面就可以定义Bean

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

测试在SpringBoot工程中直接获取其它模块jar中的Bean

创建other模块,定义一个Bean注入当前模块IOC中,等待其它模块获取使用
创建Bean类

package domain;

public class User {
}

创建配置类,将Bean注入当前模块的spring IOC

package config;

import domain.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class UserConfig {

    @Bean
    public User user(){
        return new User();
    }
}

再创建springboot-enable模块
pom.xml引入刚才模块的坐标


            com.yy
            springboot_enable_other
            0.0.1-SNAPSHOT
        

修改引导类获取springboot_enable_other模块中的User bean

package com.yy.springboot_enable;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringbootEnableApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);

        //获取other模块中的Bean
        Object user = context.getBean("user");
        System.out.println(user);
    }

}


此时会报错,无法直接获取!
因为开头中@ComponentScan的扫描范围是当前引导类所在包及其子包。

要解决有以下方法:
1.将User配置类的包加入扫描

package com.yy.springboot_enable;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.yy.config")
public class SpringbootEnableApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);

        //获取另一个模块中的Bean
        Object user = context.getBean("user");
        System.out.println(user);
    }

}

2.可以使用@import注解,加载类
用import注解,加载类。这些类都会被Spring创建,并放入IOC容器中。

package com.yy.springboot_enable;

import com.yy.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.yy.config")
@import(UserConfig.class)
public class SpringbootEnableApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);

        //获取另一个模块中的Bean
        Object user = context.getBean("user");
        System.out.println(user);
    }

}

3.可以对import注解进行封装
在springboot_enable_other中再添加EnableUser.java

package com.yy.config;


import org.springframework.context.annotation.import;

import java.lang.annotation.*;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@documented
@import(UserConfig.class)
public @interface EnableUser {
}

同时在springboot_enable模块引导类调整注解

package com.yy.springboot_enable;

import com.yy.config.EnableUser;
import com.yy.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.yy.config")
//@import(UserConfig.class)
@EnableUser
public class SpringbootEnableApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);

        //获取另一个模块中的Bean
        Object user = context.getBean("user");
        System.out.println(user);
    }

}

整体目录

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/439500.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号