资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringBoot2.0中@Configuration注解有什么用

SpringBoot2.0中@Configuration注解有什么用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联专业为企业提供振兴网站建设、振兴做网站、振兴网站设计、振兴网站制作等企业网站建设、网页设计与制作、振兴企业网站模板建站服务,十年振兴做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

  1. 通过配置文件beans.xml配置user和dog的bean

    
    
    
    
        
        
    
    
    
        
    
    
    

  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的
       */
      @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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


当前名称:SpringBoot2.0中@Configuration注解有什么用
链接URL:http://cdkjz.cn/article/gpodih.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220