- 编写configuration
@Configuration public class HelloWorldConfiguration { @Bean public String helloWorld(){ return "Hello, yicj" ; } }
- 编写EnableXXX导入配置类
- 直接导入配置类(方式一)
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Import(HelloWorldConfiguration.class) public @interface EnableHelloWorld { }
- 通过ImportSelector导入配置类(方式二)
public class HelloWorldImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{ HelloWorldConfiguration.class.getName()}; } } @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Import(HelloWorldImportSelector.class) public @interface EnableHelloWorld { }
- 直接导入配置类(方式一)
- 启动类编写,导入EnableXXX注解类
@Slf4j @EnableHelloWorld public class EnableHelloWorldBootstrap { public static void main(String[] args) { ConfigurableApplicationContext ctx = new SpringApplicationBuilder(EnableHelloWorldBootstrap.class) .web(WebApplicationType.NONE) .run(args); String bean = ctx.getBean("helloWorld", String.class); log.info("=====> bean : {}", bean); ctx.close(); } }
Springboot自定义Enable基于接口的实现
猜你喜欢
转载自blog.csdn.net/yichengjie_c/article/details/114159587
今日推荐
周排行