Spring boot将配置属性注入到bean类中
一、@configurationproperties注解的使用
看配置文件,我的是yaml格式的配置:
// file application.yml my: servers: - dev.bar.com - foo.bar.com - jiaobuchong.com
下面我要将上面的配置属性注入到一个java bean类中,看码:
import org.springframework.boot.context.properties.configurationproperties; import org.springframework.stereotype.component; import java.util.arraylist; import java.util.list; /** * file: myconfig.java * created by jiaobuchong on 12/29/15. */ @component //不加这个注解的话, 使用@autowired 就不能注入进去了 @configurationproperties(prefix = "my") // 配置文件中的前缀 public class myconfig { private list<string> servers = new arraylist<string>(); public list<string> getservers() { return this.servers; } }
下面写一个controller来测试一下:
/** * file: hellocontroller * created by jiaobuchong on 2015/12/4. */ @requestmapping("/test") @restcontroller public class hellocontroller { @autowired private myconfig myconfig; @requestmapping("/config") public object getconfig() { return myconfig.getservers(); } }
下面运行application.java的main方法跑一下看看:
@configuration //标注一个类是配置类,spring boot在扫到这个注解时自动加载这个类相关的功能,比如前面的文章中介绍的配置aop和拦截器时加在类上的configuration @enableautoconfiguration //启用自动配置 该框架就能够进行行为的配置,以引导应用程序的启动与运行, 根据导入的starter-pom 自动加载配置 @componentscan //扫描组件 @componentscan(value = "com.spriboot.controller") 配置扫描组件的路径 public class application { public static void main(string[] args) { // 启动spring boot项目的唯一入口 springapplication app = new springapplication(application.class); app.setbannermode(banner.mode.off); app.run(args); }
在浏览器的地址栏里输入:
localhost:8080/test/config 得到:
[“dev.bar.com”,”foo.bar.com”,”jiaobuchong.com”]
二、@configurationproperties和@enableconfigurationproperties注解结合使用
在spring boot中使用yaml进行配置的一般步骤是,
1、yaml配置文件,这里假设:
my: webserver: #http 监听端口 port: 80 #嵌入web服务器的线程池配置 threadpool: maxthreads: 100 minthreads: 8 idletimeout: 60000
2、
//file mywebserverconfigurationproperties.java import org.springframework.boot.context.properties.configurationproperties; @configurationproperties(prefix = "my.webserver") public class mywebserverconfigurationproperties { private int port; private threadpool threadpool; public int getport() { return port; } public void setport(int port) { this.port = port; } public threadpool getthreadpool() { return threadpool; } public void setthreadpool(threadpool threadpool) { this.threadpool = threadpool; } public static class threadpool { private int maxthreads; private int minthreads; private int idletimeout; public int getidletimeout() { return idletimeout; } public void setidletimeout(int idletimeout) { this.idletimeout = idletimeout; } public int getmaxthreads() { return maxthreads; } public void setmaxthreads(int maxthreads) { this.maxthreads = maxthreads; } public int getminthreads() { return minthreads; } public void setminthreads(int minthreads) { this.minthreads = minthreads; } } }
3、
// file: mywebserverconfiguration.java import org.springframework.context.annotation.configuration; import org.springframework.boot.context.properties.enableconfigurationproperties; @configuration @enableconfigurationproperties(mywebserverconfigurationproperties.class) public class mywebserverconfiguration { @autowired private mywebserverconfigurationproperties properties; /** *下面就可以引用mywebserverconfigurationproperties类 里的配置了 */ public void setmyconfig() { string port = properties.getport(); // ........... } }
the @enableconfigurationproperties annotation is automatically applied to your project so that any beans annotated with @configurationproperties will be configured from the environment properties. this style of configuration works particularly well with the springapplication external yaml configuration.(引自spring boot官方手册)
三、@bean配置第三方组件(third-party configuration)
创建一个bean类:
// file threadpoolbean.java /** * created by jiaobuchong on 1/4/16. */ public class threadpoolbean { private int maxthreads; private int minthreads; private int idletimeout; public int getmaxthreads() { return maxthreads; } public void setmaxthreads(int maxthreads) { this.maxthreads = maxthreads; } public int getminthreads() { return minthreads; } public void setminthreads(int minthreads) { this.minthreads = minthreads; } public int getidletimeout() { return idletimeout; } public void setidletimeout(int idletimeout) { this.idletimeout = idletimeout; } }
引用前面第二部分写的配置类:mywebserverconfiguration.java和mywebserverconfigurationproperties.java以及yaml配置文件,现在修改mywebserverconfiguration.java类:
import com.jiaobuchong.springboot.domain.threadpoolbean; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.context.properties.enableconfigurationproperties; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; /** * created by jiaobuchong on 1/4/16. */ @configuration //这是一个配置类,与@service、@component的效果类似。spring会扫描到这个类,@bean才会生效,将threadpoolbean这个返回值类注册到spring上下文环境中 @enableconfigurationproperties(mywebserverconfigurationproperties.class) //通过这个注解, 将mywebserverconfigurationproperties这个类的配置到上下文环境中,本类中使用的@autowired注解注入才能生效 public class mywebserverconfiguration { @suppresswarnings("springjavaautowiringinspection") //加这个注解让ide 不报: could not autowire @autowired private mywebserverconfigurationproperties properties; @bean //@bean注解在方法上,返回值是一个类的实例,并声明这个返回值(返回一个对象)是spring上下文环境中的一个bean public threadpoolbean getthreadbean() { mywebserverconfigurationproperties.threadpool threadpool = properties.getthreadpool(); threadpoolbean threadpoolbean = new threadpoolbean(); threadpoolbean.setidletimeout(threadpool.getidletimeout()); threadpoolbean.setmaxthreads(threadpool.getmaxthreads()); threadpoolbean.setminthreads(threadpool.getminthreads()); return threadpoolbean; } }
被@configuration注解标识的类,通常作为一个配置类,这就类似于一个xml文件,表示在该类中将配置bean元数据,其作用类似于spring里面application-context.xml的配置文件,而@bean标签,则类似于该xml文件中,声明的一个bean实例。
写一个controller测试一下:
import com.jiaobuchong.springboot.domain.threadpoolbean; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; /** * created by jiaobuchong on 2015/12/4. */ @requestmapping("/first") @restcontroller public class hellocontroller { @autowired private threadpoolbean threadpoolbean; @requestmapping("/testbean") public object getthreadbean() { return threadpoolbean; } }
运行application.java的main方法,
在浏览器里输入:http://localhost:8080/first/testbean
得到的返回值是:
{“maxthreads”:100,”minthreads”:8,”idletimeout”:60000}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: PHP MYSQL简易交互式站点开发
下一篇: LINQ重写博客垃圾图片回收算法