SpringBoot入门教程(十八)@value、@Import、@ImportResource、@PropertySource
Spring Boot提倡基于Java的配置。这两篇博文主要介绍springboot 一些常用的注解介绍
v@value
通过@Value可以将外部的值动态注入到Bean中。
添加application.properties的属性,方便后面演示。
domain.name=cnblogs
@Value("字符串1")
private String testName; // 注入普通字符串
@Value("#{systemProperties['os.name']}")
private String systemPropertiesName; // 注入操作系统属性
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private double randomNumber; //注入表达式结果
@Value("${domain.name}")
private String domainName; // 注入application.properties的配置属性
效果如下:

v@Import
SpringBoot 的 @Import 用于将指定的类实例注入之Spring IOC Container中。
package com.cnblogs.demo;
public class Dog { }
package com.cnblogs.demo;
public class Cat {
}
在启动类中需要获取Dog和Cat对应的bean,需要用注解@Import注解把Dog和Cat的bean注入到当前容器中。
package com.cnblogs.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import; //@SpringBootApplication
@ComponentScan
/*把用到的资源导入到当前容器中*/
@Import({Dog.class, Cat.class})
public class App { public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
System.out.println(context.getBean(Dog.class));
System.out.println(context.getBean(Cat.class));
context.close();
}
}
v@ImportResource
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上.
@ImportResource(locations = {"classpath:applicationContext.xml"})
@SpringBootApplication
public class SpringBootConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootConfigApplication.class, args);
}
}
v@PropertySource
自定义配置文件名称,多用于配置文件与实体属性映射。
person.properties person.lastName=Jack
person.age=18
person.birth=2018/12/9
person.boss=true
person.maps.key1=value1
person.maps.key2=value2
person.lists=a,b,c
person.dog.name=tom
person.dog.age=1
@PropertySource(value = {"classpath:person.properties"})
@ConfigurationProperties(prefix = "person")
@Component
public class Person {
private String lastName;
private Integer age;
private boolean isBoss;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
...setter/getter/toString...
}
这样一个注解(@PropertySource(value = {"classpath:person.properties"}))就可以搞定不在主配置里读取,按照不同的功能模块划分出不同的配置文件。
v补充
@Bean注解用在方法上,作用:将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名。springboot不推崇使用配置文件,推崇使用全配置方式开发,如何定义一个配置类呢?在类名上加@Configuration标签标明这是一个配置类,方法上则用@Bean
v源码地址
https://github.com/toutouge/javademosecond/tree/master/hellospringboot
作 者:请叫我头头哥
出 处:http://www.cnblogs.com/toutou/
关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
SpringBoot入门教程(十八)@value、@Import、@ImportResource、@PropertySource的更多相关文章
- SpringBoot入门教程(十九)@ControllerAdvice+@ExceptionHandler全局捕获Controller异常
在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@ModelAttribute,并应用到所有@Requ ...
- SpringBoot入门教程(十五)集成Druid
Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB ...
- SpringBoot入门教程(十四)导出Excel
用JavaPOI导出Excel时,我们会考虑到Excel版本及数据量的问题.针对不同的Excel版本,要采用不同的工具类.HSSFWorkbook:是操作Excel2003以前(包括2003)的版本, ...
- SpringBoot入门教程(十二)DevTools热部署
devtools模块,是为开发者服务的一个模块.主要的功能就是代码修改后一般在5秒之内就会自动重新加载至服务器,相当于restart成功.与JRebel不同的是,JRebel是一款商业插件,devto ...
- SpringBoot入门教程(十)应用监控Actuator
Actuator可能大家非常熟悉,它是springboot提供对应用自身监控,以及对应用系统配置查看等功能.spring-boot-starter-actuator模块的实现对于实施微服务的中小团队来 ...
- SpringBoot入门教程(十六)@Autowired、@Inject、@Resource
@Resource,@Autowired,@Inject 这3种都是用来注入bean的,它们属于不同的程序中.详情参见下表: v区别 ANNOTATION PACKAGE SOURCE 作用域 实现方 ...
- 34.无废话ExtJs 入门教程十八[树:TreePanel]
转自:https://www.cnblogs.com/iamlilinfeng/archive/2012/06/28/2566350.html 1. <!DOCTYPE html PUBLIC ...
- MyBatis基础入门《十八》动态SQL(if-where)
MyBatis基础入门<十八>动态SQL(if-where) 描述: 代码是在<MyBatis基础入门<十七>动态SQL>基础上进行改造的,不再贴所有代码,仅贴改动 ...
- RabbitMQ入门教程(十六):RabbitMQ与Spring集成
原文:RabbitMQ入门教程(十六):RabbitMQ与Spring集成 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...
随机推荐
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
<题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...
- java自动化-数据驱动junit演示,下篇
本文旨在帮助读者介绍,如何使用excle实现数据驱动 本文是上文https://www.cnblogs.com/xuezhezlr/p/9096063.html的继续,如果没看上文建议自己看一下,对理 ...
- BZOJ.5305.[HAOI2018]苹果树(组合 计数)
LOJ BZOJ 洛谷 BZOJ上除了0ms的Rank1啦.明明这题常数很好优化的. 首先,\(n=1\)时有\(2\)个位置放叶子,\(n=2\)时有\(3\)个... 可知\(n\)个点的有标号二 ...
- Django合集
Django基础 Django--简介 Django--web框架简介 浅析uWSGI.uwsgi.wsgi Django--url(路由)配置 Django--模板层 Django--视图层 Dja ...
- vscode设置中文语言
https://jingyan.baidu.com/article/7e44095377c9d12fc1e2ef5b.html
- :nth-child() 与 :nth-of-type(n)的区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mysql练习
1.表关系 创建如下表格,并创建相关约束 ##(1)创建一个数据库 create database db2 default charset utf8; ##切换到db2数据库中 use db2 ##创 ...
- IDEA使用Git传放项目
使用Git下载项目到IDEA工具上开发 1. 下载Git 软件工具 https://git-scm.com/ 2. 下载安装 3.打开IDEA 配置Git 4. 搜索Git 在登入 5.选择自己Git ...
- Java线程安全相关概
- 购物车自己sql错误
$user_id=$_GET['user_id']; if(!$user_id){ $arr=array('code'=>-1,'data'=>"用户不存在"); ec ...