博客
关于我
@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锁与脏读、不可重复读、幻读详解
查看>>
MySQL锁机制
查看>>
mysql锁机制,主从复制
查看>>
Mysql锁机制,行锁表锁
查看>>
MySQL锁表问题排查
查看>>
Mysql锁(2):表级锁
查看>>
MySQL锁,锁的到底是什么?
查看>>
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
查看>>
Mysql错误2003 -Can't connect toMySQL server on 'localhost'(10061)解决办法
查看>>
MySQL错误提示mysql Statement violates GTID consistency
查看>>
mysql错误:This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de
查看>>
mysql长事务
查看>>
mysql问题记录
查看>>
MySQL集群解决方案(1):MySQL数据库的集群方案
查看>>
MySQL集群解决方案(4):负载均衡
查看>>
MySQL集群解决方案(5):PXC集群
查看>>
MySQL面试宝典
查看>>
WAP短信:融合传统短信和互联网的新型通信方式
查看>>
mysql面试题学校三表查询_mysql三表查询分组后取每组最大值,mysql面试题。
查看>>