kotlin, spring boot and data classes for configuration - spring-boot

I've implemented several projects with spring boot and java. Now I'm evaluating, if I could do it with kotlin.
I am struggling with #ConfigurationProperties and data classes.
Simple example:
api:
clientId: client123
url: https://api.url.com
key: api-access-key
Data class:
#ConfigurationProperties("api")
#ConstructorBinding
data class ApiConfiguration(
val clientId: String,
val url: String,
val key: String
)
Starter class:
#EnableConfigurationProperties(ApiConfiguration::class)
#SpringBootApplication
class SpringdemoApplication
fun main(args: Array<String>) {
runApplication<SpringdemoApplication>(*args)
}
If I'm running a test, I get the following message:
s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'apiConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
...
Description:
ApiConfiguration is annotated with #ConstructorBinding but it is defined as a regular bean which caused dependency injection to fail.
Action:
Update your configuration so that ApiConfiguration is defined via #ConfigurationPropertiesScan or #EnableConfigurationProperties.
The following post doesn't help me: Kotlin & Spring Boot #ConfigurationProperties
Environment:
spring boot 2.7.8
kotlin 1.6.21
Can someone help me to understand and solve the problem?

Related

#ConditionalOnBean not work for spring boot test

Hy everyone! I'm trying to solve the problem for a very long time.
There is very simple spring boot test
public class ApplicationTest {
#Test
void testContext() {
SpringApplication.run(Application.class);
}
}
And several beans...
#Service
#ConditionalOnBean(CommonService.class)
#RequiredArgsConstructor
public class SimpleHelper {
...
#Service
#RequiredArgsConstructor
#ConditionalOnBean(CommonFeignClient.class)
public class CommonService {
...
#FeignClient(
name = "CommonClient",
url = "localhost:8080"
)
public interface CommonFeignClient {
And the main class look as
#SpringBootApplication
#EnableFeignClients(clients = AnotherFeignClient.class)
public class Application {
When the spring application starts everything works ok. SimpleHelper does not created.
But in the spring boot test throw the exception:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'simpleHelper' defined in URL [...]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'foo.bar.CommonService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Please help, I don't understand what's going on anymore =)
Spring boot version is 2.3.0.RELEASE.
To use #ConditionalOnBean correctly, CommonService needs to be an auto-configuration class or defined as a bean by an auto-configured class rather than a service that's found by component scanning. This ensures that the bean on which CommonService is conditional has been defined before the condition is evaluated. The need for this is described in the annotation's javadoc:
The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after.

Spring Data JDBC UnsatisfiedDependencyException

I wanted to move from JdbcTemplate to Spring Data JDBC. However I seem to have some misconfiguration but I cannot figure out where. The errors are "expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}" and "Parameter 0 of constructor ... required a bean ... that could not be found."
I put #Repository on the public repository interfaces extending PagingAndSortingRepository (as I did with the DAO classes extending from JdbcDaoSupport) without success. Then I added #EnableJdbcRepositories with and without package name to the database config class, also no success. I also tried the database config to inherit from AbstractJdbcConfiguration, still the same errors ...
Unfortunately I couldn't find a working example and I now gave up after some trial and error. I still would love to get this working, the version I used is spring-boot-starter-data-jdbc:2.4.0
Code fragments:
DatabaseConfiguration.java
#Configuration
#EnableJdbcRepositories("<basepackage>.repository.jdbc")
#EnableJdbcAuditing(auditorAwareRef = "springSecurityAuditorAware")
#EnableJpaRepositories("<basepackage>.repository")
#EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
#EnableTransactionManagement
#EnableElasticsearchRepositories("<basepackage>.repository.search")
public class DatabaseConfiguration extends AbstractJdbcConfiguration {
}
UserRepository.java
#Repository
public interface UserRepository extends PagingAndSortingRepository<User, String> {
}
QualityResource.java (REST Controller)
public QualityResource(UserRepository userRepository) {
this.userRepository = userRepository;
}
Error messages:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'qualityResource' defined in file [.../backend/build/classes/java/main/.../web/rest/QualityResource.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '....repository.jdbc.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Application failed to start: Description: Parameter 0 of constructor in <basepackage>.web.rest.QualityResource required a bean of type '<basepackage>.repository.jdbc.UserRepository' that could not be found.

Kotlin application.yml data class looking for beans

I have a Kotlin data class that I want to read properties from application.yml file. Here is my data class:
#ConstructorBinding
#ConfigurationProperties("meanwhile.in.hell.myapp")
data class MyAppProperties(val serverId: String, val locationId: String)
I then added it to my configuration class:
#Configuration
#EnableConfigurationProperties(MyAppProperties::class)
open class MyAppConfiguration(private val properties: MyAppProperties) {
where I access the values using just properties.serverId and pass the object into the constructor of other beans being created, such as this one:
open class MyAppClient(
private val webClient: WebClient,
private val properties: MyAppProperties
) : IMyAppClient {
However, when I start up my application I get an error that instead of trying to load the properties from application.yml, it is trying to find beans for the constructor params:
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'meanwhile.in.hell.myapp-meanwhile.in.hell.myapp.MyAppProperties': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Parameter 0 of constructor in meanwhile.in.hell.myapp.MyAppProperties required a bean of type 'java.lang.String' that could not be found.
How do I stop my app thinking that these params are Autowired? I know that in Kotlin, this is how a constructor Autowired bean looks like (ie, not requiring the annotation), but all example I have seen online on how to read application.yml properties looks the same as my data class.
Spring-Boot v2.3.0.RELEASE
Kotlin v1.3.72
Turns out I was missing the dependency
org.jetbrains.kotlin:kotlin-reflect
Docs:
https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/htmlsingle/#boot-features-kotlin-requirements
Issue:
https://github.com/spring-projects/spring-boot/issues/19582

Spring #Service defined in Kotlin file results in 'UnsatisfiedDependencyException: Error creating bean' when referenced

I have an existing Springboot/Java application and I wanted to start migrating the application to Kotlin. I create a #Service in Kotlin, and my unit test fails at runtime because Spring can't find my service.
My unit test:
#ExtendWith(SpringExtension::class)
#SpringBootTest(classes = arrayOf(Application::class))
class Junit5SpringKotlinTests {
#Autowired // Springboot ConfigurationProperties (application.yaml) mapped to Java bean. This works.
lateinit var applicationConfigurationJavaBean: EnvironmentProperties
fun appProperties(): EnvironmentProperties.Environment? {
return applicationConfigurationJavaBean.environments[applicationConfigurationJavaBean.activeEnvironment]
}
#Autowired
lateinit var testServiceKotlinBean :TestService // My Kotlin #Service which can not be found at runtime.
#Test
fun `ApplicationConfiguration`() {
println(appProperties()?.baseClientId)
testServiceKotlinBean.hello()
}
}
My Kotlin service: TestService.kt
import org.springframework.stereotype.Service
#Service
class TestService {
fun hello() {
println("hello from service")
}
}
My error at runtime executing the unit test:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Junit5SpringKotlinTests': Unsatisfied dependency expressed through field 'testServiceKotlinBean'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'api.model.TestService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'api.model.TestService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
FWW, I am able to write JUnit5 SpringBootTests in Kotlin test code and exercise all my Java components, so this has something to do with Kotlin components being discovered at runtime by Spring. Java components work.
I suspected this might be a build problem and have seen similar issues. I have the following included in my pom.xml:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
so that wasn't the problem.
The problem was the package name I used for the Kotlin service. Kotlin is nice in that I don't have put my files in the same package path like java. But my Spring classpath scanning was relying on a package: A.B.C and below and my Kotlin path only had C. I changed the package name in my Kotlin file to be A.B.C.TestService and all is fine. Yay.

Spring beans GeoModule bean injection to RepositoryRestMvcConfiguration

I'm currently testing spring data rest, and I want to expose the primary keys (ids) of my entities through the REST interface.
I have found that the proper (?) way to do this is:
public class IdExporterConfiguration extends RepositoryRestMvcConfiguration {
#Override
protected void configureRepositoryRestConfiguration(
RepositoryRestConfiguration config) {
super.configureRepositoryRestConfiguration(config);
config.exposeIdsFor(User.class);
}
}
The problem is, that if I change my bean definition to this:
<bean class="test.project.util.IdExporterConfiguration"/>
From this:
<bean class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/>
my application fails to start...
The error is:
Could not autowire field: org.springframework.data.geo.GeoModule org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.geoModule;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.geo.GeoModule] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
Basically it says that it does not find a GeoModule bean, so it can't autowire it for the RepositoryRestMvcConfiguration base...
Now the fun part is, that is I define the bean:
<bean class="org.springframework.data.geo.GeoModule"/>
The error changes to:
Could not autowire field: org.springframework.data.geo.GeoModule org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.geoModule;
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.data.geo.GeoModule] is defined:
expected single matching bean but found 2: jacksonGeoModule,org.springframework.data.geo.GeoModule#0
So if I don't define a bean, there is 0, but if I define one, there is 2?
I still don't know why, but if I use the #Configuration annotation, then it works...
No GeoModule bean needed, but how can it be, that with the original config as XML bean definition it works, but with the subclass, it does not?

Resources