博客
关于我
@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/

你可能感兴趣的文章
项目引入非配置的文件,打成war包后测试报错的可能原因
查看>>
Git学习笔记
查看>>
不需要爬虫也能轻松获取 unsplash 上的图片
查看>>
痞子衡嵌入式:语音处理工具pzh-speech诞生记(2)- 界面构建(wxFormBuilder3.8.0)
查看>>
痞子衡嵌入式:极易上手的可视化wxPython GUI构建工具(wxFormBuilder)
查看>>
痞子衡嵌入式:串口调试工具pzh-com诞生记(2)- 界面构建(wxFormBuilder3.8.0)
查看>>
elementUi源码解析(1)--项目结构篇
查看>>
Nmap扫描工具介绍
查看>>
算法笔记:递归、动态规划
查看>>
常用Windows 快捷键
查看>>
linux命令-压缩与打包
查看>>
ORACLE 11g 生产中高水位线(HWM)处理
查看>>
weblogic 服务器部署SSL证书
查看>>
Oracle Orion tool check io(ORACLE Orion 工具查看以及校验IO)
查看>>
oracle 11g not in 与not exists 那个高效?
查看>>
Linux 安装Redis 5.0(以及参数调优)
查看>>
html5 Game开发系列文章之 零[开篇]
查看>>
Golang Web入门(4):如何设计API
查看>>
ES6基础之——new Set
查看>>
玩玩小爬虫——试搭小架构
查看>>