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
    评论
Spring注解的底层原理是基于反射和动态代理技术实现的。在使用Spring注解时,需要在业务逻辑类和切面类上注入到容器中,并使用@Aspect注解标识切面类。这样一来,Spring就能够扫描并识别这些注解,并在运行时生成代理对象来增强目标对象的功能。 具体来说,当使用注解配置的ApplicationContext(如AnnotationConfigApplicationContext)时,Spring会根据配置的扫描路径扫描并识别注解,将被注解标记的类实例化为Bean对象,并将这些Bean对象保存在容器中。而在使用xml配置文件的ApplicationContext(如ClassPathXmlApplicationContext)时,Spring会读取xml配置文件,并根据配置来实例化Bean对象。 当涉及到事务处理时,Spring会查找带有@Transactional注解的方法,一旦发现这个注解Spring就会开启事务,并生成代理对象来执行方法。这个代理对象会在方法执行前后添加事务的相关操作,比如开始事务、提交事务或回滚事务等。这样,就能够保证方法在事务的控制下进行。 总结起来,Spring注解的底层原理是利用反射和动态代理技术,通过扫描注解或读取配置文件来实例化Bean对象,并在运行时生成代理对象来增强目标对象的功能,比如事务处理等。这种方式使得开发者能够更加方便地使用Spring框架,并实现各种功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值