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. redhat配置问题

    redhat开机自动连接网络配置 vim  /etc/sysconfig/network-scripts/ifcfg-eth0 将  ONBOOT=no 更改为 yes 即可 redhat配置 yum ...

  2. .Net Core 2.1 升级3.1 问题整理

    随着技术的不断拓展更新,我们所使用的技术也在不断地升级优化,项目的框架也在不断地升级,本次讲解 .net core 2.1  升级到3.1所需要注意的事项: 当项目框架升级后,所有的Nuget引用也会 ...

  3. Java并发编程之并发关键字

    volatile 保证可见性 一个线程修改volatile变量的值时,该变量的新值会立即刷新到主内存中,这个新值对其他线程来说是立即可见的 一个线程读取volatile变量的值时,该变量在本地内存中缓 ...

  4. Mybatis中由于${}直接注入引发的问题

    一.问题引入 我们先来看这段代码,我想从取值为${category}的表中查询全部信息. @Mapper public interface CategoryMapper { @Select(" ...

  5. 对控制器类型“StudentController”的操作“Edit”的当前请求在下列操作方法之间不明确:

    "/"应用程序中的服务器错误. 对控制器类型"StudentController"的操作"Edit"的当前请求在下列操作方法之间不明确:类型 ...

  6. ethtool - 命令

    ethtool 导览:     1. 如何查看 Linux 中可用的网卡接口     2. 如何查看 Linux 中网卡信息     3. 如何查看网卡驱动版本以及硬件版本     4. 如何查看网络 ...

  7. 第22 章 : 有状态应用编排 StatefulSet

    有状态应用编排 StatefulSet 本文将主要分享以下四方面的内容: "有状态"需求 用例解读 操作演示 架构设计 "有状态"需求 课程回顾 我们之前讲到过 ...

  8. 再谈synchronized锁升级

    在图文详解Java对象内存布局这篇文章中,在研究对象头时我们了解了synchronized锁升级的过程,由于篇幅有限,对锁升级的过程介绍的比较简略,本文在上一篇的基础上,来详细研究一下锁升级的过程以及 ...

  9. Java 获取Word中的标题大纲(目录)

    概述 Word中的标题可通过"样式"中的选项来快速设置(如图1), 图1 在添加目录时,可将"有效样式"设置为"目录级别"显示(如图2),一 ...

  10. 2021年春软件工程"助教团队"成员介绍

    2021年春软件工程 助教团队 成员介绍 项目 内容 这个作业属于那个课程 2021春季学期软件工程(罗杰.任健) 这个作业的要求在哪里 团队介绍 姓名 照片 个人介绍 分工 刘Q 本团队的PM,和助 ...