Spring 注解版 学习笔记(一)AnnotationConfigApplicationContext

AnnotationConfigApplicationContext

1.传统XML配置类

Spring配置文件版–》传送门

2.注解版Spring

  1. 和XML不同的是,所有的注册方式全部使用注解的方式开发,下面小编演示基本操作。

  2. 创建一个测试实体类以及测试类。

    测试实体类(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");
        }
}
  1. 测试类完成注解配置测试
@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);
    }
}
  1. 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

简单纯注解配置完成

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值