SpringBoot2.0之@Configuration注解

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

posted @ 2021-05-09 17:24  Java大师-  阅读(359)  评论(0编辑  收藏  举报