Spring 注解版 学习笔记AnnotationConfigApplicationContext
Posted Adorable_Rocy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 注解版 学习笔记AnnotationConfigApplicationContext相关的知识,希望对你有一定的参考价值。
AnnotationConfigApplicationContext
1.传统XML配置类
2.注解版Spring
-
和XML不同的是,所有的注册方式全部使用注解的方式开发,下面小编演示基本操作。
-
创建一个测试实体类以及测试类。
测试实体类(TestAnnotaionBean)
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TestAnnotaionBean {
private String msg;
}
配置类( MainConfigura )
@Configuration(proxyBeanMethods = false)
public class MainConfigura {
@Bean
public TestAnnotaionBean testBean(){
return new TestAnnotaionBean("创建的msg测试Bean");
}
}
- 测试类完成注解配置测试
@Slf4j
public class AnnotationTest {
@Test
public void test1(){
//获取AnnotationConfigApplicationContext上下文
AnnotationConfigApplicationContext annotationConfigApplicationContext =
new AnnotationConfigApplicationContext(MainConfigura.class);
TestAnnotaionBean bean = annotationConfigApplicationContext.getBean(TestAnnotaionBean.class);
log.info("实例bean为:{}",bean);
}
}
- log日志打印如下
14:47:56.755 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2ef9b8bc
14:47:56.776 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
14:47:56.843 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
14:47:56.845 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
14:47:56.847 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
14:47:56.850 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
14:47:56.861 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'mainConfigura'
14:47:56.867 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testBean'
14:47:56.907 [main] INFO fatcats.top.demo.test.AnnotationTest - 实例bean为:TestAnnotaionBean(msg=创建的msg测试Bean)
Process finished with exit code 0
简单纯注解配置完成
以上是关于Spring 注解版 学习笔记AnnotationConfigApplicationContext的主要内容,如果未能解决你的问题,请参考以下文章