博客
关于我
@SpringBootConfiguration注解
阅读量:432 次
发布时间:2019-03-06

本文共 2361 字,大约阅读时间需要 7 分钟。

@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;@SpringBootConfigurationpublic 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@SpringBootApplicationpublic 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;            }}

 

转载地址:http://omxyz.baihongyu.com/

你可能感兴趣的文章
410. Split Array Largest Sum
查看>>
程序员视角:鹿晗公布恋情是如何把微博搞炸的?
查看>>
Spring+SpringMVC+MyBatis+easyUI整合进阶篇(七)一次线上Mysql数据库崩溃事故的记录
查看>>
系统编程-进程间通信-无名管道
查看>>
为什么我觉得需要熟悉vim使用,难道仅仅是为了耍酷?
查看>>
一个支持高网络吞吐量、基于机器性能评分的TCP负载均衡器gobalan
查看>>
HDOJ2017_字符串统计
查看>>
404 Note Found 团队会议纪要
查看>>
使用Redis作为Spring Security OAuth2的token存储
查看>>
【SOLVED】Linux使用sudo到出现输入密码提示延迟时间长
查看>>
springmvc转springboot过程中访问jsp报Whitelabel Error Page错误
查看>>
项目引入非配置的文件,打成war包后测试报错的可能原因
查看>>
Git学习笔记
查看>>
不需要爬虫也能轻松获取 unsplash 上的图片
查看>>
痞子衡嵌入式:语音处理工具pzh-speech诞生记(2)- 界面构建(wxFormBuilder3.8.0)
查看>>
痞子衡嵌入式:极易上手的可视化wxPython GUI构建工具(wxFormBuilder)
查看>>
痞子衡嵌入式:串口调试工具pzh-com诞生记(2)- 界面构建(wxFormBuilder3.8.0)
查看>>
elementUi源码解析(1)--项目结构篇
查看>>
Nmap扫描工具介绍
查看>>
算法笔记:递归、动态规划
查看>>