argh! Is there an example somewhere of using the k...
# spring
d
argh! Is there an example somewhere of using the kotlin-noarg plugin? The docs say “Then specify the list of no-arg annotations”. Does this mean that I have to write my own annotation class to be able to use it? I see some references in stackoverflow to people just adding
@NoArg
to their data class, but no information on what other setup is needed to get that to work
c
That depends. What do you need no-arg plugin for?
d
I’m just trying to get a silly @ConfigurationProperties class to work. 😞
c
@ConfigurationProperties does not need no-arg
d
If I define this
Copy code
@ConfigurationProperties("scanner")
data class Scanner(var backendSourceRoot: String)
c
But since you're asking I assume you're trying to use data class with val params to the constructor, this won't work in Spring yet (although feature is planned and at some point will be available, you can check their tracker).
d
and I try to use it as an autowired argument to a component:
Copy code
@Component
class Platform(model: Model, personas: Personas, scanner: Scanner) : ViewProvider {
It says “No beans of scanner found”
c
I see, what you need in this case is:
Copy code
@ConfigurationProperties("scanner")
class Scanner {
    lateinit var backendSourceRoot: String
}
d
not a data class
okay..
So that doesn’t make the no bean error go away..
c
Do you have kotlin spring plugin applied?
and which version of spring you're using by the way?
d
id "org.jetbrains.kotlin.plugin.spring" version "1.2.60"
and
compile "org.springframework.boot:spring-boot-starter:2.0.3.RELEASE"
This is all based off of the project https://github.com/Catalysts/cat-boot-structurizr-sample/ . As an example, I just want to do something simple like replace the string “some shell tool” on line 62 of https://github.com/Catalysts/cat-boot-structurizr-sample/blob/master/src/main/kotlin/cc.catalysts.structurizr.kotlin/KotlinArchitecture.kt with a configuration parameter.
c
Hm... should work then. Can you please check that import for
Scanner
in
Platform
is correct? Because there are some library classes called Scanner
d
I didn’t like that name anyway, so I’ll change it to ScannerConfig. No import, it is defined in the same single file KotlinArchitecture.kt
Hmm..
c
Let me download the project and try for myself I'll get back to you in several minutes
d
Oops, I looked at some pure Java code I wrote and noticed that it used two annotations. @Configuration and @ConfigurationProperties.
When I add the @Configuration parameter, that makes the bean error go away. Let me test and see if it is actually reading the prop. I’d still really appreciate it if you might be able to take a look at what should be done to add a config property to that project as I described above. 🙂
c
That's wrong thing to do
But it points as to the problem
Your ScannerConfig class is not picked up by component scan
d
🙂 I’ll just take a moment to sincerely thank you for helping me this morning. I really appreciate it.
c
@Component
is what you're looking fore. Sure it is my pleasure 🙂
I wonder why docs do not mention the needed
@Component
...
d
Going back to those docs though, I don’t know how many additional properties I’m going to want. Maybe I should just try starting with a single
@Value
for the moment.
My experience is that using
@Value
leads to all kinds of bad habits and maintainability of the software suffers.
For example one project I'm working on has a huge code base and
@Value
sprinkled all over it, it's very hard to find and manage all those properties and switches, whereas if they were properly grouped in @ConfigurationProperties classes it would've been much cleaner and easier to deal with.
d
Very true. If we can provably get config props working, I’m fine with using it. 🙂
c
Sure, good luck 🙂 this approach is awesome. One architectural suggestion: in real project do not add @Component/@Configuration annotations to @ConfigurationProperties, instead declare them on respective module's
@Configuration
class with this:
@EnableConfigurationProperties(ModuleConfig::class)
This will make it explicit and a breeze to manage and look up anything you want 🙂
In the sample we're working with it would look like this:
Copy code
@SpringBootApplication
@EnableConfigurationProperties(ScannerConfig::class)
class KotlinArchitecture

@ConfigurationProperties("scanner")
class ScannerConfig {
	lateinit var backendSourceRoot: String
}
d
okay, lemme try that
joy! I got my value!
And a ton of other errors, but that is beside the point! 🙂
c
🙂 Cheers