标签:ram cto auto main cat meta conf new ons
在 Spring Boot 场景下,基于约定大于配置的原则,实现 Spring 组件自动装配的目的。其中使用了底层装配技术
@EnableHelloWorld
public class EnableHelloWorldBootstrap {
@Bean
@ConditionalOnSystemProperty(name = "user.name", value = "MAIBENBEN")
public String helloWorld() {
return "Hello,World 小马哥";
}
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(EnableHelloWorldBootstrap.class)
.web(WebApplicationType.NONE)
.run(args);
// helloWorld Bean 是否存在
String helloWorld =
context.getBean("helloWorld", String.class);
System.out.println("helloWorld Bean : " + helloWorld);
String helloWorld2020 =
context.getBean("helloWorld2020", String.class);
System.out.println("helloWorld Bean : " + helloWorld2020);
// 关闭上下文
context.close();
}
}
@Configuration // Spring 模式注解装配
@EnableHelloWorld // Spring @Enable 模块装配
@ConditionalOnSystemProperty(name = "user.name", value = "MAIBENBEN") // 条件装配
public class HelloWorldAutoConfiguration {
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.imooc.diveinspringboot.configuration.HelloWorldAutoConfiguration
标签:ram cto auto main cat meta conf new ons
原文地址:https://www.cnblogs.com/fjf3997/p/13029163.html