The meaning of @Value("${helloWorld}") in Java

In Java, @Value is an annotation in the Spring framework for injecting property values ​​into fields or method parameters of a class. It is commonly used to inject values ​​from external configuration files into Spring components to set properties dynamically at runtime.

In the code you provided, @Value(" hello World " ) means to inject the value of the attribute named hello World into the hello field. {helloWorld}") means to inject the attribute value named helloWorld into the hello field.h e ll o W or l d " ) indicates that the attribute value named h e ll o W or l d is injected into the h e ll o field. {helloWorld} is a placeholder that instructs Spring to parse the attribute named helloWorld in the external configuration file (such as the properties file) at runtime and inject its value into the hello field.

Suppose you have a configuration file (say application.properties) with the following content:

helloWorld=Hello, World!

When the Spring container starts, it parses the configuration file and injects the value of Hello, World! into the hello field annotated with @Value("${helloWorld}"). Therefore, the value of the hello field will be Hello, World!.

In this way, you can use the hello field in the code to access the attribute value in the configuration file without directly hardcoding the value in the code, thereby realizing dynamic configuration and injection of attributes.

Guess you like

Origin blog.csdn.net/hmwz0001/article/details/131800671