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

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

  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


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