本文共 806 字,大约阅读时间需要 2 分钟。
由于大量项目都会在主要的配置类上添加 @Configuration @EnableAutoConfiguration @ComponentScan三个注解
因此Spring Boot提供了@SpringBootApplication注解,该注解可以替代上面三个注解(使用Spring注解继承实现)。
标注在类上,相当于把该类作为spring的xml配置文件中的<beans>
,
作用为:配置spring容器(应用上下文)
@Configurationpublic class TestConfiguration { public TestConfiguration() { System.out.println("TestConfiguration容器启动初始化。。。"); }}public class TestMain { public static void main(String[] args) { // 如果加载spring-context.xml文件: // ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml"); // @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); }}
转载地址:http://ukgxi.baihongyu.com/