当前位置:网站首页>Two types of attribute injection methods
Two types of attribute injection methods
2022-06-25 19:45:00 【A thousand miles of grass】
Attribute general injection
Write a test class :
@Component
@PropertySource("classpath:book.properties")
public class Book {
@Value("${book.name}")
private String Name;
@Value("${book.author}")
private String Autor;// character string
@Value("${book.tags}")
private String[] tags;// Array of strings
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAutor() {
return Autor;
}
public void setAutor(String autor) {
Autor = autor;
}
public String[] getTags() {
return tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
@Override
public String toString() {
return "Book{" +
"Name='" + Name + '\'' +
", Autor='" + Autor + '\'' +
", tags=" + Arrays.toString(tags) +
'}';
}
}
Then , To write book.properties, because application.properties It is a system configuration class , So we create a new configuration file , adopt java Code annotation @PropertySource Put your own configuration file inside ,@Value Add the self-defined attribute variables to
book.properties The content of :
book.name= A dream of red mansions
book.author= Cao xueqin
book.tags= classics , classic , Ming Dynasty
Test code :
@SpringBootTest
class Demo06ApplicationTests {
@Autowired
Book book;// Injection class
@Test
void contextLoads() {
System.out.println("book = " + book);// Print the contents of this class
}
}
Then we run unit tests to see the results , It is found that there will be garbled code when printed , This is because the file code is not set correctly , We should change the file encoding to UTF-8, Pictured :
The final results :
Type safe property Injection
We are Book Class plus @ConfigurationProperties annotation , Inside prefix The prefix is set to book, Then it will automatically scan the properties in the configuration file , Delete @Value attribute
@Component
@PropertySource("classpath:book.properties")
@ConfigurationProperties(prefix = "book")// Add new annotation
public class Book {
private String Name;
private String Autor;
private String[] tags;
....// The following code is omitted
Finally, it runs successfully , Of course, after testing , The case of attributes does not affect automatic scanning , Case insensitive .
Maven Attribute injection
application.properties To configure :
app.encoding=@project.build.[email protected]
aap.version=@java.[email protected]
obtain :
app.encoding=UTF-8
aap.version=11.0.6
This configuration must be written in application.properties Configuration in progress , Other customized configuration classes will not be scanned , There is a filter
Please refer to this article for details Maven Engineering problems
边栏推荐
- Network security detection and prevention test questions (4)
- From the perspective of the core competitiveness of ABT, the Internet giant, who fell first
- Ali visual AI training camp -day05- creativity day - your image recognition project
- QQ robot: self forbidden words management of group members [latest beta2 version]
- PHP little knowledge record
- Is it safe to open an account with flush?
- QQ机器人闪照转发/撤回消息转发【最新beta2版本】
- 广州华锐互动打造VR展厅全景在线虚拟展厅
- Processing method for uniapp or applet onload not receiving parameters
- Genicam gentl standard ver1.5 (1)
猜你喜欢

Trend ea- fixed stop loss and profit per order

Miner's Diary: why should I go mining on April 5, 2021

Paddleocr learning (II) paddleocr detection model training
Android Development Notes - Quick Start (from sqllite to room licentiousness) 2

广州华锐互动VR全景为各行各业带来发展

SEO outsourcing reliable company, enterprise SEO outsourcing company which reliable?

Connecting PHP to MySQL instances in the lamp environment of alicloud's liunx system

Web components - Basics

Embark on a new journey and reach the world with wisdom

Principles of MySQL clustered index and non clustered index
随机推荐
揭秘GES超大规模图计算引擎HyG:图切分
Electronic package to generate EXE file
Tcp/ip test questions (II)
PostgreSQL user role permissions
Why are life science enterprises on the cloud in succession?
QQ机器人闪照转发/撤回消息转发【最新beta2版本】
1、 Hikaricp source code analysis of connection acquisition process I
Panda weekly -2022/02/18
QQ机器人官方插件加载配置方法【beta2版本】
Electronic basic project construction & communication between main thread and rendering thread
5、 Initialization analysis II of hikaricp source code analysis
Processing method for uniapp or applet onload not receiving parameters
MySQL prompt performance_ Schema missing table
QQ robot official plug-in loading configuration method [beta2 version]
广州华锐互动VR全景为各行各业带来发展
Error record: preg_ match(): Compilation failed: range out of order in character class at offset 13
Network security detection and prevention test questions (II)
Simple native JS tab bar switching
Leetcode-78-subset
User management and permissions