spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
55.28k stars 37.62k forks source link

Broken injection mechanism #32716

Closed l3r8yJ closed 2 weeks ago

l3r8yJ commented 2 weeks ago

@jhoeller @sbrannen take a look, please

Who can explain this behavior in Spring: there is an abstract class

abstract class BaseRepo {
    @Autowired
    protected lateinit var dsl: DSLContext
    // ....
}

@Repository
class MyRepo : BaseRepo {
    // ...
}

question: why in case MyRepo is declared as @Repository, the framework does not initialize @Autowired fields – protected lateinit var dsl: DslContext, but if you change @Repository to @Service or @Component, the injection works? This is strange, because @Repository is a subtype of @Component, just at the specification level, but it turns out not, Spring handles them differently.

P.S. I noticed it on Spring Boot 3, I haven't tested it on Spring Boot 2. Constructor injection - do not suggest

snicoll commented 2 weeks ago

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

l3r8yJ commented 2 weeks ago

@snicoll which details I need to add? obviously this issue is a bug, because the unexpected behaviour was described. Also, SO already contains this question. There is no answer around 7 years. I insist to reopen this issue. So, wdyt?

snicoll commented 2 weeks ago

A sample that demonstrates what you've described that we can clone and run.

Here's one that attempts to reproduce what you've described and it works as expected: https://github.com/snicoll-scratches/demo-32716

There is no answer around 7 years. I insist to reopen this issue. So, wdyt?

That pinging team members and gathering your colleagues to thumb up your issue as soon as it's created is a bit obnoxious. If you provide something that fails the way you've described using only the core spring framework, we can obviously reopen and have a look to it.

l3r8yJ commented 2 weeks ago

@snicoll okay, if I'll create runnable example using Spring Boot it would be possible to move this issue to Spring Boot repository?