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

最新文章

  1. java文档注释--javadoc的用法
  2. 从0和1到Python
  3. memset 的实现分析
  4. UVa 103 Stacking Boxes --- DAG上的动态规划
  5. npm和bower
  6. 转载ASP.NET 状态管理Application,Session,Cookie和ViewState用法
  7. [Whole Web] [Node.js] [Browserify] [Grunt] Automation task with grunt-browserify &amp; grunt-contrib-watch
  8. Java:Date、Calendar、Timestamp的使用
  9. 扩展《C程序设计语言》练习2-3程序通用性
  10. Numpy之ndarray与matrix
  11. JavaEE Tutorials (5) - 运行企业bean示例
  12. PHP - 拒绝低版本PHP
  13. jquery一次绑定多个元素事件
  14. C#语言和SQL Server第八章笔记
  15. Java中不定项参数(可变参数)的使用
  16. Dispose in c#
  17. selenium验证码和错误截图
  18. WriteableBitmap/BitmapImage/MemoryStream/byte[]相互转换
  19. New JVM Option Enables Generation of Mixed-Mode Flame Graphs
  20. 代做JSP课程设计,毕业设计

热门文章

  1. Java volatile关键字详解
  2. 最简单的JVM内存结构图
  3. subprocess如何设置命令超时时间
  4. 颠覆你认知的Python3.9
  5. sqli-labs系列——第三关
  6. 开源组件编排引擎LiteFlow发布里程碑版本2.5.0
  7. ionic3 清除navpush的堆栈 (android真机返回键bug)
  8. 轻松理解 Java 静态代理/动态代理
  9. python3美化表格数据输出结果
  10. python基础(补充):python三大器之生成器