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

你可能感兴趣的文章
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>