前言

@Configuation是spring最常见的注解之一,很多人都很熟悉。说起这个注解的作用,大家一定会说,当类上有@Configuration的时候,可以在这个类中使用@Bean注解向spring容器中注册bean;如果没有@Configuration的时候,就无法注册Bean。事实真的是这样吗?

不使用@Configurtion

我们先看看传统注册bean的方式

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="qiaoyingzi" class="beans.vo.Student">        <property name="id" value="10086">property>        <property name="age" value="18">property>        <property name="name" value="乔英子">property>        <property name="city" ref="beijing">property>    bean>    <bean id="fangyifan" class="beans.vo.Student">        <property name="id" value="10087">property>        <property name="age" value="18">property>        <property name="name" value="方一凡">property>        <property name="city" ref="beijing">property>    bean>    <bean id="beijing" class="beans.vo.City">        <property name="id" value="110000">property>        <property name="name" value="北京">property>    bean>beans>
public class ConfigurationTest {    public static void main(String[] args) {        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");        Student qiaoyingzi = (Student) ac.getBean("qiaoyingzi");        Student fangyifan = (Student) ac.getBean("fangyifan");        System.out.println(qiaoyingzi);        System.out.println(fangyifan);    }}

输出结果

Connected to the target VM, address: '127.0.0.1:59054', transport: 'socket'beans.vo.Student@367ffa75beans.vo.Student@49438269Disconnected from the target VM, address: '127.0.0.1:59054', transport: 'socket'Process finished with exit code 0

我们用xml向bean容器注册了三个Bean,一个是学生类的对象"乔英子",一个是学生类的对象"方一凡",一个是城市类的对象"北京",乔英子和方一凡都有个依赖对象"北京"。

使用@Bean,不用@Configuration

public class StudentConfig {    @Bean    public City beijing(){        System.out.println("城市类对象被创建");        return  new City(110000,"beijing");    }    @Bean    public Student qiaoyingzi(){        return new Student("10086","乔英子",18,beijing());    }    @Bean    public Student fangyifan(){        return new Student("10087","方一凡",18,beijing());    }}
public class ConfigurationTest {    public static void main(String[] args) {        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();        ctx.register(StudentConfig.class);        ctx.refresh();        StudentConfig stuConfig=(StudentConfig) ctx.getBean("studentConfig");        Student qiaoyingzi= (Student)ctx.getBean("qiaoyingzi");        Student fangyifan= (Student)ctx.getBean("fangyifan");        System.out.println(stuConfig);        System.out.println(qiaoyingzi);        System.out.println(fangyifan);    }}

输出结果

Connected to the target VM, address: '127.0.0.1:58487', transport: 'socket'城市类对象被创建城市类对象被创建城市类对象被创建beans.annotation.configuration.StudentConfig@4135c3bbeans.vo.Student@6302bbb1beans.vo.Student@31304f14Disconnected from the target VM, address: '127.0.0.1:58487', transport: 'socket'

我们没有加@Configuration同样向spring容器注册了Bean,配置类对象,学生类对象和城市类对象都被注入,其中城市类的对象被创建了三次

加上@Configuration和@Bean 

我们只在StudentConfig上加上 @Configuration注解,再看输出结果

Connected to the target VM, address: '127.0.0.1:58499', transport: 'socket'城市类对象被创建beans.annotation.configuration.StudentConfig$$EnhancerBySpringCGLIB$$4fa8418e@740cae06beans.vo.Student@26d9b808beans.vo.Student@f78a47eDisconnected from the target VM, address: '127.0.0.1:58499', transport: 'socket'

乍一看没啥区别,其实我们可以发现,虽然两个学生类对象都有依赖对象"北京",但是城市对象只创建了一次,并且我们的配置类Bean StudentConfig打印信息多了"$EnhancerBySpringCGLIB$"这么一串东西。说明spring容器会用cglib为@Configuration修饰的类创建代理对象,被@Bean修饰的方法会全部被拦截,并且只执行一次。

总结:

  1. 不管@Bean所在的类是否有@Configuration注解,都可以将@Bean修饰的方法注册到spring容器

  2. @Configuration注解修饰的类,spring会通过cglib产生一个代理对象,并且拦截@Bean修饰的方法,可以确保一些bean是单例(总结参考路人甲java)

@configuration注解_Spring注解@Configuration相关推荐

  1. java spring注解_spring注解是如何实现的?

    注解呢,是java本身自带的一个东西,它基于java的接口进行实现,是一种特殊的接口类型,通常对于注解来说,三种情况,一个是在编译前就会被丢弃的,一个是编译后留在class中的,另一种是会一直存在,运 ...

  2. @import注解_Spring 注解之@Import 注入的各种花活

    今天来分享一下 pig4cloud 中涉及的 @Import 的注入形式.通过不同形式的注入方式,最大程度使得架构简洁. @Import导入一个组件 来看 EnablePigxDynamicRoute ...

  3. Spring @Configuration 和 @Bean 注解

    @Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...

  4. @Configuration和@Bean注解详解

    本文来详细说下@Configuration和@Bean注解 文章目录 @Configuration注解 @Bean注解 @Configuration注解 @Configuration注解源码 pack ...

  5. Spring学习笔记——@Configuration和@Bean注解

    前言 在Spring框架中,有两种对容器的配置和启动方式,一种是基于XML文档的配置方式,一种则是"零配置",即Java Based Configuration. 而在零配置中,最 ...

  6. @configuration注解_SpringBoot的@Configuration扫盲

    @Configuration注解标识的类中声明了1个或者多个@Bean方法,Spring容器可以使用这些方法来注入Bean,比如: @Configuration public class AppCon ...

  7. 【视频分享】尚硅谷Java视频教程_Spring注解驱动开发视频教程

    <Spring注解驱动开发>是一套帮助我们深入了解Spring原理机制的教程: 现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了S ...

  8. Java注解配置rest服务_Spring Boot 注解—常用注解

    注:该部分内容包含一些常用注解,如果没有学习过java注解的同学可以先看一下上一小节的内容Spring Boot 注解-基本知识 ,不看也没关系,下面就开始本节内容. @Configuration注解 ...

  9. java获取方法上的注解_Spring:使用Spring AOP时,如何获取目标方法上的注解

    当使用spring AOP时,判断目标方法上的注解进行相关操作,如缓存,认证权限等 自定义注解 packagecom.agent.annotation;importjava.lang.annotati ...

最新文章

  1. Ubuntu安装MongoDB
  2. 进入编译器后,一个函数经历了什么?
  3. @override怎么加上去_不知道怎么学?java后端5年经验和技术总结(附思维导图)
  4. mysql的本地id可以随便设置马_MySQL中的账号与权限管理
  5. QQuickWidget + QML编程实现酷炫动态动画效果
  6. java多线程编程(三)- 线程的创建
  7. 一张图概括淘宝直播背后的前端技术 | 赠送多媒体前端手册
  8. 计算机扫描服务开启,win7系统,电脑打印机右键中无扫描选项(WIA已启动)
  9. python:等间距分割pdf文件
  10. 在Ubuntu 14.04上安装了tfp和tftp服务
  11. Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return
  12. 给大家分享几款浪漫的唯美APP
  13. 一起talk C栗子吧(第一百九十三回:C语言实例--DIY less命令二 )
  14. 在git bash中输入git init 提示错误信息: fatal: open /dev/null or dup failed: No such file or directory的解决办法
  15. 软件测试技术之APP专项测试方法汇总
  16. 云计算中存储基础知识
  17. 计算机可以存储人类记忆吗,神经科学:人类大脑是否100%在工作?人类记忆是否有极限?...
  18. alternate端口什么意思_alternate是什么意思
  19. HTML背景颜色的渐变
  20. 三国志5剧本修改器 1.1

热门文章

  1. 关于“小米盒子”等的被喷
  2. 惠普应用监控解决方案
  3. linux在当前目录下打开终端,linux - 终端:在窗口中打开当前路径? - Ubuntu问答...
  4. 华为怎么删除自带的音乐_华为手机独有的这个模式,让睡觉更舒畅
  5. C# 子类实例化基类 基类使用不了子类的方法_C#中的类、方法和属性
  6. 软件测试工程师,需要达到什么水平才能顺利拿到 20k+ 无压力?
  7. c语言入门敲打,C语言基础三(敲打键盘、寻找资料,循环语句)
  8. 有序列表ol与无序列表ul用法
  9. python创建员工_python作业员工信息表程序(第四周)
  10. python selenium 小知识点整理笔记(更新中...)