• @SpringBootConfiguration注解


    @SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,
    并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。

    如下所示:
    我定义了一个配置类

    复制代码
    package com.lhkj.pluto.config;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.boot.SpringBootConfiguration;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootConfiguration
    public class Config {
        @Bean
        public Map createMap(){
            Map map = new HashMap();
            map.put("username","gxz");
            map.put("age",27);
            return map;
        }
    }
    复制代码

    在main方法中,可以直接这样使用:

    复制代码
    package com.lhkj.pluto;
    
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
    import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.lhkj.pluto.user.entity.User;
    
    
    
    /*
     * 发现@SpringBootApplication是一个复合注解,
     * 包括@ComponentScan,和@SpringBootConfiguration,@EnableAutoConfiguration
     * 
     */
    
    @RestController
    @SpringBootApplication
    public class App 
    {   
        
        @RequestMapping(value="/hello")
        public String Hello(){
            return "hello";
        }
        
        
        @Bean
        public Runnable createRunnable() {
            return () -> System.out.println("spring boot is running");
        }
    
        public static void main( String[] args )
        {
            System.out.println( "Hello World!" );
            ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
            context.getBean(Runnable.class).run();
            System.out.println(context.getBean(User.class));
            Map map = (Map) context.getBean("createMap");   //注意这里直接获取到这个方法bean
            int age = (int) map.get("age");
            System.out.println("age=="+age);
        }
        
        
        @Bean
        public EmbeddedServletContainerFactory servletFactory(){
            TomcatEmbeddedServletContainerFactory tomcatFactory = 
                    new TomcatEmbeddedServletContainerFactory();
            //tomcatFactory.setPort(8011);
            tomcatFactory.setSessionTimeout(10,TimeUnit.SECONDS);
            return tomcatFactory;
            
        }
    }
    复制代码
    逆天改命!我命由我不由天!
  • 相关阅读:
    提高网站访问速度的34条军规(7-10)
    指针与函数传参的思考
    提高网站访问速度的34条军规(11-13)
    [置顶] 程序员面试之道(《程序员面试笔试宝典》)之如何回答技术性的问题?
    CentOS6.4 编译安装Python 3.3.2
    hdu 4055 Number String(有点思维的DP)
    解读ASP.NET 5 & MVC6系列(4):核心技术与环境配置
    解读ASP.NET 5 & MVC6系列(6):Middleware详解
    解读ASP.NET 5 & MVC6系列(5):Configuration配置信息管理
    解读ASP.NET 5 & MVC6系列(7):依赖注入
  • 原文地址:https://www.cnblogs.com/huhewei/p/14214697.html
Copyright © 2020-2023  润新知