SpringBoot进行属性的注入,list,map,arr,和其他的属性的注入
Posted SmallCuteMonkey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot进行属性的注入,list,map,arr,和其他的属性的注入相关的知识,希望对你有一定的参考价值。
SpringBoot属性注入方式
总结:使用两种方式进行注入:
1.
@Value注解 加上@Configuration 让SpringBoot识别该类,
在Application.yml文件中的注入
写上注入的东西,注入的类User上面加上@ConfigurationPropertes(prefix=“nt”),@Configuration然后测试的类上面加上@EnableConfigurationProperties(User.class)
1.创建SpringBoot和环境
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.8</version>
<scope>test</scope>
启动类的导入:
package com.hou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.hou.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
application.yml文件中加入
nt:
id: 1
username: hou
password: roothouzhicong
arrs: 1,2,3,4,5
map:
key1: aaa
key2: bbb
list:
- hhhhhhhhh
- aaaaaaa
- cccccccc
com.hou.properties包下创建类:
package com.hou.properties;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration //相当于一个xml文件的配置
//不加上Configuration会报错 Error creating bean with name 'com.hou.properties.TestTeacher'
public class Teacher {
@Value("孔子")
String name;
@Value("论语")
String course;
}
package com.hou.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
@Configuration
@ConfigurationProperties(prefix = "nt")
@Data
public class User {
int id;
String username;
String password;
Integer[] arrs;
List<String> list;
Map<String,String> map;
}
package com.hou.properties;
import com.hou.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
//测试的时候要启动类运行
@SpringBootTest(classes = Application.class)
@EnableConfigurationProperties(User.class)
public class TestTeacher {
@Resource
private Teacher teacher;
@Autowired
private User user;
@Test
public void test(){
System.out.println(teacher);
}
@Test
public void testUser(){
System.out.println(user);
}
}
以上是关于SpringBoot进行属性的注入,list,map,arr,和其他的属性的注入的主要内容,如果未能解决你的问题,请参考以下文章
在SpringBoot中怎么注入各种类型(String,List,Map,Object)配置?
Springboot实战系列之@Autowired注入类的list