SpringBoot2.0之@Configuration注解

本文转载自:https://www.javaman.cn/sb2/springboot-configuration

前面我们介绍了Spring boot2.0的启动和第一个helloworld实例,今天我们来讲解一下springboot2.0比较关键的注解@Configuration

在讲解@Configuration这个注解之前我们首先说一个实例

我们有一个User的bean和一个Dog的bean

  • 现在我们通过原始的spring的方式去管理这两个bean
  1. 通过配置文件beans.xml配置user和dog的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="user01" class="com.dashi.bean.User">
    <property name="name" value="zhangsan"/>
    <property name="age" value="18"/>
    </bean> <bean id="dog01" class="com.dashi.bean.Pet">
    <property name="name" value="哈利"/>
    </bean> </beans>
  2. 创建ApplicationContext获取user bean

    /**
    * spring方式通过getbean
    /*
    public static void main(String[] args) {
    ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:beans.xml");
    User user01 = (User) ac.getBean("user01");
    }现在我们通过原始的spring的方式去管理这两个bean**
  • 接下来我们通过spring boot的@Configuration注解来实现bean的管理

    1. 创建配置类MyConfig(该名称可以自定义),该类通过@Configuration注解

      通过@Configuration注解的类就相当于spring的beans.xml文件,通过@Bean注解的方法即为一个个的bean。bean中的属性即为spring中的property属性

      /**
      * 相当于spring中的bean.xml的<bean id="user01"></bean>
      */
      @Configuration
      public class Myconfig { //方法的名称就是spring bean中的bean id 该方法为”user01“,属性为property
      @Bean
      public User user01(){
      return new User("zhangsan",18);
      } //方法的名称就是spring bean中的bean id 该方法为”dogPet“ 属性为property
      @Bean
      public Pet dogPet(){
      return new Pet("tom");
      }
      }
    2. 得到user和Pet的实体类

      @SpringBootApplication
      public class MainApplication {
      public static void main(String[] args) {
      //SpringApplication.run(MainApplication.class);
      ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class);
      // String[] names = run.getBeanDefinitionNames();
      // for(String name:names){
      // System.out.println(name);
      // } User user01 = run.getBean("user01", User.class);
      System.out.println(user01.getName());
      Pet dogPet = run.getBean("dogPet", Pet.class);
      System.out.println(dogPet.getName()); }
      }
    3. 运行结果如下:

        .   ____          _            __ _ _
      /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
      ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
      \\/ ___)| |_)| | | | | || (_| | ) ) ) )
      ' |____| .__|_| |_|_| |_\__, | / / / /
      =========|_|==============|___/=/_/_/_/
      :: Spring Boot :: (v2.0.6.RELEASE) 2021-05-09 10:45:08.692 INFO 15880 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
      2021-05-09 10:45:08.692 INFO 15880 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
      2021-05-09 10:45:09.136 INFO 15880 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
      2021-05-09 10:45:09.758 INFO 15880 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path ''
      2021-05-09 10:45:09.786 INFO 15880 --- [ main] com.dashi.MainApplication : Started MainApplication in 4.501 seconds (JVM running for 7.13) zhangsan
      tom

最后

通过以上说明@Configuration使用SpringBoot的方式去创建一个Bean,以代替传统通过xml的方式声明Bean,简便了beans.xml的配置,具有实际的试用意义

转载自 https://www.javaman.cn/sb2/springboot-configuration

SpringBoot2.0之@Configuration注解的更多相关文章

  1. Springboot2.0整合Redis(注解开发)

    一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...

  2. springBoot2.0使用@slf4j注解记录日志

    1. idea上安装Lombok插件 File --> setting --> Plugins 安装完后重启idea 2. 在springboot项目中修改pom.xml,添加如下配置引入 ...

  3. SpringBoot2.0 浅谈注解@ControllerAdvice的作用

    我们都知道做项目一般都会有全局异常统一处理的类,那么这个类在Spring中可以用@ControllerAdvice来实现,费话不多说,先看代码: import org.springframework. ...

  4. springBoot2.0+redis+fastJson+自定义注解实现方法上添加过期时间

    springBoot2.0集成redis实例 一.首先引入项目依赖的maven jar包,主要包括 spring-boot-starter-data-redis包,这个再springBoot2.0之前 ...

  5. SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...

  6. (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)

    一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...

  7. Springboot2.0(Spring5.0)中个性化配置项目上的细节差异

    在一般的项目中,如果Spring Boot提供的Sping MVC不符合要求,则可以通过一个配置类(@Configuration)加上@EnableWebMvc注解来实现完全自己控制的MVC配置.但此 ...

  8. springboot2.0.3源码篇 - 自动配置的实现,发现也不是那么复杂

    前言 开心一刻 女儿: “妈妈,你这么漂亮,当年怎么嫁给了爸爸呢?” 妈妈: “当年你爸不是穷嘛!‘ 女儿: “穷你还嫁给他!” 妈妈: “那时候刚刚毕业参加工作,领导对我说,他是我的扶贫对象,我年轻 ...

  9. spring-boot-2.0.3源码篇 - pageHelper分页,绝对有值得你看的地方

    前言 开心一刻 说实话,作为一个宅男,每次被淘宝上的雄性店主追着喊亲,亲,亲,这感觉真是恶心透顶,好像被强吻一样.........更烦的是我每次为了省钱,还得用个女号,跟那些店主说:“哥哥包邮嘛么叽. ...

随机推荐

  1. 单链表及基本操作(C语言)

    #include <stdio.h> #include <stdlib.h> /** * 含头节点单链表定义及基本操作 */ //基本操作函数用到的状态码 #define TR ...

  2. js 算数组平均值、最大值、最小值、偏差、标准差、中位数、数组从小打大排序、上四分位数、下四分位数

    要算的数组命名为data var sum = function(x,y){ return x+y;}; //求和函数 var square = function(x){ return x*x;}; / ...

  3. Django之cookie 与session组件

    一.会话跟踪技术 1.1 什么是会话跟踪 我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而 ...

  4. 技术分享PPT整理(二):C#常用类型与数据结构

    这篇博客起源于我对Dictionary.List.ArrayList这几个类区别的好奇,当时在改造公司的旧系统,发现很多地方使用了ArrayList,但我们平时用的多是泛型集合List,改造的时候要全 ...

  5. c++一些概念

    面向对象语言三大特征: 封装,多态,继承 封装: 1.将函数定义到结构体内部,就是封装. 2.编译器会自动传递结构体的指针给函数. 类: 带有函数的结构体,称为类. 成员函数: 结构体里面的函数,称为 ...

  6. 致命错误:Python.h:没有那个文件或目录

    yum search python3 | grep dev sudo yum install python3xxx-devel

  7. ffmpeg第五篇:让水印图片旋转起来

    这篇把上次挖的坑填上 ffmpeg正式篇的上一篇(传送门)说了,这一篇要让水印旋转起来,但是后面有事情一直没有时间搞,今天,它来了............ 如果想实现旋转的功能,需要使用ffmpeg过 ...

  8. Linux命令的应用

    目录 Linux命令 Linux文件管理命令 用户管理 权限管理 vi文本编辑器 find查找命令 磁盘管理命令 压缩及解压 Linux 进程 Linux运行tomcat Linux安装mysql 卸 ...

  9. Python数据分析入门(十七):绘制条形图

    条形图的绘制方式跟折线图非常的类似,只不过是换成了plt.bar方法.plt.bar方法有以下常用参数: x:一个数组或者列表,代表需要绘制的条形图的x轴的坐标点. height:一个数组或者列表,代 ...

  10. Ubuntu安装docker/docker-compose(在全新系统状态下的安装)

    设置仓库 更新 apt 包索引. $ sudo apt-get update 安装 apt 依赖包,用于通过HTTPS来获取仓库: $ sudo apt-get install \ apt-trans ...