[JAVA] [Spring Boot] The story that the bean of the class with ConfigurationProperties annotation was not found

What is Configuration Properties?

One of the methods to handle the value of the configuration file in the application in Spring Boot.

application.yaml

sample:
  hoge: hogeValue
  huga: hugaValue
  piyo: piyoValue

When there is a setting value such as

package com.wakye.property

import ........

@ConstructorBinding
@ConfigurationProperties("sample")
data class SampleProperty(
        val hoge: String,
        val huga: String,
        val piyo: String
)

By adding the ConfigurationProperties annotation to the class that has the field corresponding to the setting value in this way, the value is bound and then registered as a bean.

The ConstructorBinding annotation has nothing to do with the main line, but if you are developing a SpringBoot application with kotlin, it is used to add the ConfigurationProperties annotation to the data class.

Now, the problem is when getting this bean.

I don't know the bean name

Normally, there is no problem in getting the function of Spring Boot to be injected as a constructor.

However, I had to get the registered bean by specifying the bean name, but there was a problem. By using the getBean method defined in ApplicationContext, it is possible to get the registered bean from the bean name.

In Spring Boot, the bean name defaults to the lower camel case of the class name. (The class name is defined only in the upper camel case, so maybe the battle is just in lowercase)

So

applicationContext.getBean("sampleProperty")

Exception occurred even if I tried to get it like this, it seems that the bean defined as sampleProperty does not exist.

Try to find out

A few hours to google. .. .. .. .. .. ..

Find those descriptions in the documentation! !! !!

When the @ConfigurationProperties bean is registered that way, the bean has the traditional name \ -\ . is the environment key prefix specified in the @ConfigurationProperties annotation and is the fully qualified name of the bean. If the annotation does not provide a prefix, only the bean's fully qualified name is used.

[Official document quote](https://spring.pleiades.io/spring-boot/docs/2.1.3.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config- typesafe-configuration-properties)

Apparently it doesn't register with a normal bean name.

In this case

prefix is sample fqn is com.wakye.property

So the bean name is

sample-com.wakye.property.SampleProperty

It seems to be.

applicationContext.getBean("sample-com.wakye.property.SampleProperty")

Successful acquisition here.

It was a pitfall that was difficult to understand.

Recommended Posts

[Spring Boot] The story that the bean of the class with ConfigurationProperties annotation was not found
A story packed with the basics of Spring Boot (solved)
The story of encountering Spring custom annotation
The story of raising Spring Boot 1.5 series to 2.1 series
Command that the registered information was not found
Access the built-in h2db of spring boot with jdbcTemplate
The story of raising Spring Boot from 1.5 series to 2.1 series part2
Introduction of library ff4j that realizes FeatureToggle with Spring Boot
[Gradle] The story that the class file did not exist in the jar file
A story that made me regret when a "NotReadablePropertyException" occurred during the development of the Spring Boot application.
A story that struggled with the introduction of Web Apple Pay
See the behavior of entity update with Spring Boot + Spring Data JPA
The story that ARM's processing performance of Open JDK was low
The story of making it possible to build a project that was built by Maven with Ant
[Java] The story that the expected array was not obtained by the String.split method.
Form class validation test with Spring Boot
When introducing JOOQ to Spring boot, a story that was dealt with because an error occurred around Liquibase
Introducing the Spring Boot Actuator, a function that makes the operation of Spring Boot applications easier.
Organize the differences in behavior of @NotBlank, @NotEmpty, @NotNull with Spring Boot + Thymeleaf
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
Get the path defined in Controller class of Spring boot as a list
Resource handler settings when delivering SPA with the static resource function of Spring Boot
The story of tuning android apps with libGDX
When @Transactional of Spring Boot does not work
The story that the port can no longer be used in the Spring boot sample program
A memo that I was addicted to when making batch processing with Spring Boot
A story that stumbled when deploying a web application created with Spring Boot to EC2
Customize the display when an error such as 404 Not Found occurs in Spring Boot
[Java] The problem that true was returned as a result of comparing Integer with ==
Specify the encoding of static resources in Spring Boot
Switch environment with Spring Boot application.properties and @Profile annotation
05. I tried to stub the source of Spring Boot
The story of making a reverse proxy with ProxyServlet
I tried to reduce the capacity of Spring Boot
Create Restapi with Spring Boot ((1) Until Run of App)
How to boot by environment with Spring Boot of Maven
Control the processing flow of Spring Batch with JavaConfig.
File upload with Spring Boot (do not use Multipart File)
The story of making dto, dao-like with java, sqlite
The story that the forced update could not be implemented
The story that led to solving the error because postgres did not start with docker-compose up
The story that the request parameter from the iPhone application could not be obtained successfully with the Servlet
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
The version of Ruby that was installed by default on the Mac was referenced, not from rbenv