SpringBoot模块自动组装

SpringBoot模块自动装配

方式一 Import 导入模块类:

1.模块定义

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * HelloWorld Configuration Class
 */
@Configuration
public class HelloWorldConfiguration {
    @Bean
    public String helloWorld(){
        return "hello world";
    }
}

2.激活注解

import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
 * 激活 HelloWorld 模块
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloWorldConfiguration.class)
public @interface EnableHelloWorld {
}

3.方法测试

@EnableHelloWorld
public class EnableModuleDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 注册 Bean
        context.register(EnableModuleDemo.class);
        // 启动 spring 上下文
        context.refresh();
        String helloWorld = context.getBean("helloWorld",String.class);
        System.out.println(helloWorld);
        // 关闭 Spring 应用上下文
        context.close();
    }
}

方式二 Import 导入ImportSelector接口实现类:

1.模块定义

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * HelloWorld Configuration Class
 */
@Configuration
public class HelloWorldConfiguration {
    @Bean
    public String helloWorld(){
        return "hello world";
    }
}

2.ImportSelector接口实现

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
/**
 * HelloWorld 模块 {@link ImportSelector} 实现
 * @see ImportSelector
 */
public class HelloWorldImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"cn.modular.HelloWorldConfiguration"};
    }
}

3.激活注解

import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
 * 激活 HelloWorld 模块
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloWorldImportSelector.class)
public @interface EnableHelloWorld {
}

4.方法测试

@EnableHelloWorld
public class EnableModuleDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 注册 Bean
        context.register(EnableModuleDemo.class);
        // 启动 spring 上下文
        context.refresh();
        String helloWorld = context.getBean("helloWorld",String.class);
        System.out.println(helloWorld);
        // 关闭 Spring 应用上下文
        context.close();
    }
}

方式三 Import 导入ImportBeanDefinitionRegistrar接口实现类:

1.模块定义

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * HelloWorld Configuration Class
 */
@Configuration
public class HelloWorldConfiguration {
    @Bean
    public String helloWorld(){
        return "hello world";
    }
}

2.ImportBeanDefinitionRegistrar接口实现

public class HelloWorldImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(HelloWorldConfiguration.class);
        BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition,registry);
    }
}

3.激活注解

import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
 * 激活 HelloWorld 模块
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloWorldImportBeanDefinitionRegistrar.class)
public @interface EnableHelloWorld {
}

4.方法测试

@EnableHelloWorld
public class EnableModuleDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 注册 Bean
        context.register(EnableModuleDemo.class);
        // 启动 spring 上下文
        context.refresh();
        String helloWorld = context.getBean("helloWorld",String.class);
        System.out.println(helloWorld);
        // 关闭 Spring 应用上下文
        context.close();
    }
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值