博客
关于我
@SpringBootConfiguration注解
阅读量:441 次
发布时间: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/

你可能感兴趣的文章
mysql报错级别_更改MySQL日志错误级别记录非法登陆(Access denied)
查看>>
Mysql报错:too many connections
查看>>
MySQL报错:无法启动MySQL服务
查看>>
mysql授权用户,创建用户名密码,授权单个数据库,授权多个数据库
查看>>
mysql排序查询
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
Mysql推荐书籍
查看>>
Mysql插入数据从指定选项中随机选择、插入时间从指定范围随机生成、Navicat使用存储过程模拟插入测试数据
查看>>
MYSQL搜索引擎
查看>>
mysql操作数据表的命令_MySQL数据表操作命令
查看>>
mysql操作日志记录查询_如何使用SpringBoot AOP 记录操作日志、异常日志?
查看>>
MySQL支持的事务隔离级别,以及悲观锁和乐观锁的原理和应用场景?
查看>>
mysql支持表情
查看>>
MySQL支撑百万级流量高并发的网站部署详解
查看>>
MySQL改动rootpassword的多种方法
查看>>
mysql数据分组索引_MYSQL之索引配置方法分类
查看>>
mysql数据取差,mysql屏蔽主外键关联关系
查看>>
MySQL数据和Redis缓存一致性方案详解
查看>>
MySQL数据和Redis缓存一致性方案详解
查看>>