background
As a JVM scripting language, Kotlin is favored by many Java developers.
The project uses Java+Kotlin mixed programming. In Spring Boot application development, configurations that will not change are placed in the local configuration file, and configurations that may change are placed in the remote Apollo Server.
question
Because of business needs, it is necessary to add a configuration that may be continuously updated, and then if else
judge it through logic.
Apollo configuration:
The hard coding in the screenshot below is the back-up solution for the unresolved problem:
Console printing:
In fact, when developing locally, it is customary to implement the business logic first. At the beginning, there was no new configuration in Apollo screen.channel
, but using The hard coding method in Figure 2 above:
@Value("\${screen.channel: xhwjk_screen}")
var screenChannel: String? = null
This method does not read the local configuration file, nor does it read the Apollo Server configuration. It directly sets a default value, that is, xhwjk_screen
during breakpoint debugging, the data cannot be obtained.
bootstrap.yml
Add configuration in local file:
screen:
channel: xhwjk_screen,pdwjk_screen
I also application.yml
tried adding the same configuration items.
The conclusion is: the configuration cannot be obtained.
Compared
The same configuration can be placed in a Java Controller without any problem:
@Slf4j
@RestController
@RequestMapping("/dialog")
public class AuthenticationCheckController {
@Value("${screen.channel: xhwjk_screen}")
private String screenChannel;
@GetMapping(value = "/authentication")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void checkAuthentication() {
LOGGER.info("ff:{}", screenChannel);
// This resource just returns a 204 No Content in case the request is authenticated.
}
}
Console prints:
AuthenticationCheckController | checkAuthentication | 28 | - ff:xhwjk_screen,pdwjk_screen
Note:
JDK version: OpenJDK-11
Spring Boot version: 2.1.6.RELEASE
Spring Cloud version: Greenwich.RELEASE
Kotlin version: 1.3.72
troubleshooting
Try 1
Searching with the problem phenomenon described above, I found stackoverflow-how-to-get-value-from-application-yml-in-springboot .
Apollo adds a new configuration:
adds a new configuration DialogConfig.kt
class:
@Component
@ConfigurationProperties("dialog")
class DialogConfig {
var screenChannel: String? = null
}
Quoting this configuration:
@Resource
private val dialogConfig: DialogConfig? = null
Still can't get the configuration:
Try 2
Found another stackoverflow-how-to-get-variable-from-spring-application-yaml-in-kotlin)
or the one above DialogConfig.kt
, quoting the configuration:
@Resource
lateinit var dialogConfig: DialogConfig
Report an error directly:
kotlin.UninitializedPropertyAccessException: lateinit property dialogConfig has not been initialized
Reference: stackoverflow-uninitializedpropertyaccessexception . Make a judgment before using it. There is no need to judge, and you still cannot get the data.
Try 3
DialogConfig becomes a Java class:
@Component
@ConfigurationProperties("dialog")
public class DialogConfig {
public String screenChannel;
}
Quoting this configuration:
@Resource
var dialogConfig: DialogConfig? = null
Or not!
solve
There is really no way, hard coding solves the problem, refer to screenshot 1.
Vomited. .
Bald.