详解SpringBoot注入数据的方式
作者:pangsir8983 发布时间:2022-05-09 06:34:24
关于注入数据说明
1.不通过配置文件注入数据
通过@Value将外部的值动态注入到Bean中,使用的情况有:
注入普通字符串
注入操作系统属性
注入表达式结果
注入其他Bean属性:注入Student对象的属性name
注入文件资源
注入URL资源
辅助代码
package com.hannpang.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component(value = "st")//对student进行实例化操作
public class Student {
@Value("悟空")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试@Value的代码
package com.hannpang.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
@Component
public class SimpleObject {
@Value("注入普通字符串")
private String normal;
//关于属性的KEY可以查看System类说明
@Value("#{systemProperties['java.version']}")//-->使用了SpEL表达式
private String systemPropertiesName; // 注入操作系统属性
@Value("#{T(java.lang.Math).random()*80}")//获取随机数
private double randomNumber; //注入表达式结果
@Value("#{1+2}")
private double sum; //注入表达式结果 1+2的求和
@Value("classpath:os.yaml")
private Resource resourceFile; // 注入文件资源
@Value("http://www.baidu.com")
private Resource testUrl; // 注入URL资源
@Value("#{st.name}")
private String studentName;
//省略getter和setter方法
@Override
public String toString() {
return "SimpleObject{" +
"normal='" + normal + '\'' +
", systemPropertiesName='" + systemPropertiesName + '\'' +
", randomNumber=" + randomNumber +
", sum=" + sum +
", resourceFile=" + resourceFile +
", testUrl=" + testUrl +
", studentName='" + studentName + '\'' +
'}';
}
}
Spring的测试代码
package com.hannpang;
import com.hannpang.model.SimpleObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo04BootApplicationTests {
@Autowired
private SimpleObject so;
@Test
public void contextLoads() {
System.out.println(so);
}
}
运行结果为:SimpleObject{normal='注入普通字符串', systemPropertiesName='1.8.0_172', randomNumber=56.631954541947266, sum=3.0, resourceFile=class path resource [os.yaml], testUrl=URL [http://www.baidu.com], studentName='悟空'}
2.通过配置文件注入数据
通过@Value将外部配置文件的值动态注入到Bean中。配置文件主要有两类:
application.properties、application.yaml application.properties在spring boot启动时默认加载此文件
自定义属性文件。自定义属性文件通过@PropertySource加载。@PropertySource可以同时加载多个文件,也可以加载单个文件。如果相同第一个属性文件和第二属性文件存在相同key,则最后一个属性文件里的key启作用。加载文件的路径也可以配置变量,如下文的${anotherfile.configinject},此值定义在第一个属性文件config.properties
在application.properties中加入如下测试代码
app.name=一步教育
在resources下面新建第一个属性文件config.properties
内容如下
book.name=西游记
anotherfile.configinject=system
在resources下面新建第二个属性文件config_system.properties
内容如下
我的目的是想system的值使用第一个属性文件中定义的值
book.name.author=吴承恩
下面通过@Value(“${app.name}”)
语法将属性文件的值注入bean属性值,详细代码见:
package com.hannpang.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"classpath:config.properties","classpath:config_${anotherfile.configinject}.properties"})
public class LoadPropsTest {
@Value("${app.name}")
private String appName; // 这里的值来自application.properties,spring boot启动时默认加载此文件
@Value("${book.name}")
private String bookName; // 注入第一个配置外部文件属性
@Value("${book.name.author}")
private String author; // 注入第二个配置外部文件属性
@Autowired
private Environment env; // 注入环境变量对象,存储注入的属性值
//省略getter和setter方法
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("bookName=").append(bookName).append("\r\n")
.append("author=").append(author).append("\r\n")
.append("appName=").append(appName).append("\r\n")
.append("env=").append(env).append("\r\n")
// 从eniroment中获取属性值
.append("env=").append(env.getProperty("book.name.author")).append("\r\n");
return sb.toString();
}
}
测试代码
package com.hannpang;
import com.hannpang.model.SimpleObject;
import com.hannpang.test.LoadPropsTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo04BootApplicationTests {
@Autowired
private LoadPropsTest lpt;
@Test
public void loadPropertiesTest() {
System.out.println(lpt);
}
}
运行结果为:
bookName=西游记
author=吴承恩
appName=一步教育
env=StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[ConfigurationPropertySourcesPropertySource {name='configurationProperties'}, MapPropertySource {name='Inlined Test Properties'}, MapPropertySource {name='systemProperties'}, OriginAwareSystemEnvironmentPropertySource {name='systemEnvironment'}, RandomValuePropertySource {name='random'}, OriginTrackedMapPropertySource {name='applicationConfig: [classpath:/application.properties]'}, ResourcePropertySource {name='class path resource [config_system.properties]'}, ResourcePropertySource {name='class path resource [config.properties]'}]}
env=吴承恩
3. #{...}和${...}的区别演示
A .${…}
的用法
{}里面的内容必须符合SpEL表达式,通过@Value(“${app.name}”)
可以获取属性文件中对应的值,但是如果属性文件中没有这个属性,则会报错。可以通过赋予默认值解决这个问题,如@Value("${app.name:胖先森}")
部分代码
// 如果属性文件没有app.name,则会报错
// @Value("${app.name}")
// private String name;
// 使用app.name设置值,如果不存在则使用默认值
@Value("${app.name:胖先森}")
private String name;
B.#{...}
的用法
部分代码直接演示
// SpEL:调用字符串Hello World的concat方法
@Value("#{'Hello World'.concat('!')}")
private String helloWorld;
// SpEL: 调用字符串的getBytes方法,然后调用length属性
@Value("#{'Hello World'.bytes.length}")
private String helloWorldbytes;
C.#{...}
和${...}
混合使用
${...}和#{...}
可以混合使用,如下文代码执行顺序:通过${server.name}从属性文件中获取值并进行替换,然后就变成了 执行SpEL表达式{‘server1,server2,server3'.split(‘,')}。
// SpEL: 传入一个字符串,根据","切分后插入列表中, #{}和${}配置使用(注意单引号,注意不能反过来${}在外面,#{}在里面)
@Value("#{'${server.name}'.split(',')}")
private List<String> servers;
在上文中在#{}外面,${}在里面
可以执行成功,那么反过来是否可以呢${}在外面,#{}在里面
,如代码
// SpEL: 注意不能反过来${}在外面,#{}在里面,这个会执行失败
@Value("${#{'HelloWorld'.concat('_')}}")
private List<String> servers2;
答案是不能。
因为spring执行${}是时机要早于#{}。
在本例中,Spring会尝试从属性中查找#{‘HelloWorld'.concat(‘_')},那么肯定找到,由上文已知如果找不到,然后报错。所以${}在外面,#{}在里面是非法操作
D.用法总结
#{…} 用于执行SpEl表达式,并将内容赋值给属性
${…} 主要用于加载外部属性文件中的值
#{…} 和${…} 可以混合使用,但是必须#{}外面,${}在里面
4.@Value获取值和@ConfigurationProperties获取值比较
@ConfigurationProperties | @Value | |
---|---|---|
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定(松散语法) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
关于数据校验的部分代码
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
//lastName必须是邮箱格式
private String lastName;
5. @ImportResource引入配置文件
不推荐的使用方式
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效
编写配置文件信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloService" class="com.hanpang.springboot.service.HelloService"></bean>
</beans>
大概了解就好,我们基本上不使用这种方式
6.@Configuration注解
SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式
1、配置类@Configuration
作用于类上,相当于一个xml配置文件
2、使用@Bean
给容器中添加组件,作用于方法上
/**
* @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件
*
* 在配置文件中用<bean><bean/>标签添加组件
* <bean id="helloService" class="com.hanpang.springboot.service.HelloService"></bean>
*/
@Configuration
public class MyAppConfig {
//将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
@Bean
public HelloService helloService02(){
System.out.println("配置类@Bean给容器中添加组件了...");
return new HelloService();
}
}
使用Bean注入太麻烦,我们更加喜欢使用扫描的方式
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.wx.dao.IUserDao;
import com.wx.dao.UserDaoImpl;
//通过该注解来表明该类是一个Spring的配置,相当于一个传统的ApplicationContext.xml
@Configuration
//相当于配置文件里面的<context:component-scan/>标签,扫描这些包下面的类的注解
@ComponentScan(basePackages="com.hanpang.dao,com.hanpang.service")
public class SpringConfig {
// 通过该注解来表明是一个Bean对象,相当于xml中的<bean>
//bean的id值默认是方法名userDao
/*
@Bean
public HelloService helloService02(){
System.out.println("配置类@Bean给容器中添加组件了...");
return new HelloService();
}
*/
}
附录
随机数
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
来源:https://segmentfault.com/a/1190000017485165
猜你喜欢
- 前提说明之前公司有一个项目是由androidstudio接入高德地图实现导航,定位等功能,然后还有一部分登陆页面和其他逻辑都放在Unity方
- 本文主要记录JAVA中对象的初始化过程,包括实例变量的初始化和类变量的初始化以及final关键字对初始化的影响。另外,还讨论了由于继承原因,
- 如今,企业级应用程序的常见场景是同时支持HTTP和HTTPS两种协议,这篇文章考虑如何让Spring Boot应用程序同时支持HTTP和HT
- 前言Spring Cloud默认为Zuul编写并启用了一些过滤器,这些过滤器有什么作用呢?我们不妨按照@EnableZuulServer、@
- 方式一public class Test{ public static void main(String[] args) throws Ex
- 数组的长度是固定的,无法适应数据变化的需求。为了解决这个问题,Java提供了另一个容器 java.util.ArrayList集合类,让我们
- 1. 定义在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。2. 使用的
- 1. 前言在Java开发中接触的开发者大多数不太注重对接口的测试,结果在联调对接中出现各种问题。也有的使用Postman等工具进行测试,虽然
- intellij idea是一款非常优秀的软件开发工具,它拥有这强大的插件体系,可以帮助开发者完成很多重量级的功能。今天,我们来学习一下如何
- 在分支较多的时候,switch的效率比if高,在反汇编中我们即可看到效率高的原因一、switch语句1、在正向编码时,switch语句可以看
- 1. 场景描述本节结合springboot2、springmvc、mybatis、swagger2等,搭建一个完整的增删改查项目,希望通过这
- 解决Spring in action @valid验证不生效按照书上的示例代码来实现但是,添加了验证但是没有生效。Spring提供了校验Ap
- Filter简介Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所
- 前言:什么是多数据源?最常见的单一应用中最多涉及到一个数据库,即是一个数据源(Datasource)。那么顾名思义,多数据源就是在一个单一应
- Java接口回调产生接口回调的场景在现实生活中,产生接口回调的场景很简单,比如我主动叫你帮我做一件事,然后你做完这件事之后会通知我,&quo
- SpringCloud 整合ribbon的时候出现了这个问题java.lang.IllegalStateException: No inst
- Spring Cloud Gateway(以下简称 SCG)做为网关服务,是其他各服务对外中转站,通过 SCG 进行请求转发。在请求到达真正
- 概述Spring boot 中的 @Conditional 注解是一个不太常用到的注解,但确实非常的有用,我们知道 Spring Boot
- LocalDateTime 是 Java 8 中日期时间 API 提供的一个类,在日期和时间的表示上提供了更加丰富和灵活的支持。LocalD
- 1. 背景在业务处理完之后,需要调用其他系统的接口,将相应的处理结果通知给对方,若是同步请求,假如调用的系统出现异常或是宕机等事件,会导致自