This is specifically related to the interaction between classes in separate java libraries.
We have a class in a core library that is not annotated with anything spring - POJO - and say it has a field foo
We then create a bean in another library with a dependency on the core library and this bean is marked as @RequestScope
. When using this bean from within a controller foo
would be null - I believe this is expected because of proxying or something?
This is also the same behavior if this class was defined in the same library where it was used.
Now if in the core library we annotate the class with @Component
and @RequestScope
and we do not import or do any auto-detection with spring of the core library and create a bean marked as @RequestScope
just as above we now see it working fine in the controller - foo
is now not null. This I do not fully understand as annotating a class should not be doing anything - especially if spring is not scanning it for anything?
Furthermore everywhere in the documentation I read that @Component
is just another @Bean
(beans just create components) - however even in the case of classes in the same library @RequestScope
and @Components
annotated classes result in fields that are not null, however defining an @Configuration
results in null fields.
Could anyone explain this?
Comment From: bclozel
Can you share a minimal sample application that shows the problem?
Comment From: dragonbone81
@bclozel yes:
Defined within the core package
@Component
@RequestScope
class CoreA {
val foo = "foo"
}
class CoreB {
val foo = "foo"
}
Defined within the service package
@Component
@RequestScope
class ServiceA {
val foo = "foo"
}
class ServiceB {
val foo = "foo"
}
@Configuration
class Config {
@Bean
@RequestScope
fun coreA() = CoreA()
@Bean
@RequestScope
fun coreB() = CoreB()
@Bean
@RequestScope
fun serviceB() = ServiceB()
}
@RestController
class Controller(
val coreA: CoreA,
val coreB: CoreB,
val serviceA: ServiceA,
val serviceB: ServiceB,
) {
@PostMapping("/api")
fun api() {
coreA.foo // works - non null
coreB.foo // does not work - results in null
serviceA.foo // works - non null
serviceB.foo // does not work - results in null
}
}
Hopefully this makes is some what clear what I'm talking about. And just to emphasize - the classes CoreA
, and CoreB
are not scanned in any way by spring (or at least I'm not telling them to be scanned in any way)
Comment From: snicoll
Can you please move this code in text into an actual project that we can run? We'll have to do that anyway to better understand what you're describing.
Comment From: dragonbone81
@snicoll sorry not exactly sure what you mean by an actual project that we can run
- do you mean just create a github repo with the code?
Comment From: sbrannen
do you mean just create a github repo with the code?
We ask that you provide a minimal example application (or complete integration test) that we can download and run -- for example, a ZIP file or a public Git repository.
Comment From: snicoll
just create a github repo with the code?
From experience, it isn't "just create a repo". Often there are details that the code snippet doesn't show and that we'd miss if we did that ourselves. Besides, I believe our time is better spent helping the community rather than trying to extract a sample project from a code snippet. Thanks!
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.