Autowiring Unmanaged Beans Annotated With @Component

后端 未结 3 507
闹比i
闹比i 2020-12-28 12:11

I want to use @AutoWired to inject a non-managed bean configured with @Component into a managed bean. I\'m pretty sure I have the configuration right, but for some reason I

3条回答
  •  隐瞒了意图╮
    2020-12-28 12:27

    Because Bar is configured with xml, it can only be configured with xml. i.e. you can't mix them. So that "@Autowired" annotation on Baz is not getting picked up (none of the annotations would be). It is only when you add the spring annotation at class level that spring will listen to any of the other annotations.

    What you'll need to do is in the xml configure the bean to be autowired by type, add a setter for that type and you'll achieve the desired behaviour.

    
    

    One more thing, when you annotate a bean with @Component it is a spring managed bean. Just because it is not created with xml does not mean it is unmanaged. An unmanaged bean is one you don't get from spring.

    Bar and Baz are both spring managed. It is the mechanism you've chosen to define them that differs.

提交回复
热议问题