DependsOn not working as expect in Spring 4.3.30

For context, I’m working on a Spring 4.3.30 project, not by choice, but because this is a legacy system, and I need help.

I have a configuration class in an external maven dependency (I can alter its code) that looks roughly like this:

@Configuration
@ComponentScan(basePackages = {
        "com.some.stuff"
})
@Import({AnotherModuleConfig.class, })
public class ExternalDependencyConfig{
    @Bean
    @Named("someBean")
    public Stuff abean() {
        //...
        return new Stuff();
    }

    @Bean
    @Named("someOtherBean")
    @DependsOn("someBean") // this should make sure "someBean" is instanciated first
    public Thing bbean(@Named("someBean") Stuff theGoodStuff){
        //...
        return new Thing(theGoodStuff);
    }
}

And I’m trying to import it into my own configuration class:

@Configuration
@ComponentScan(
        basePackages = {
                "com.my.stuff",
        })
@Import( { MyOtherModuleConfig.class, ExternalDependencyConfig.class} )
public class MyConfiguration {
    
}

This configuration class lives inside a module that’s part a large spring project that inherits another template project that I have to use, and is using an xml configuration exclusively. That xml configuration lives in the module we use to handle the war. So I need to add the component-scan the package of my config classes like so in the applicationContext.xml:

<context:component-scan base-package="com.my.stuff.configuration"/>

But whenever I try to load the application context, I get this error:

springframework.beans.factory.BeanCreationException: Error creating bean with name 'bbean' defined in com.some.stuff.configuration.ExternalDependendcyConfig: 'bbean' depends on missing bean 'someBean'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'someBean'

And I have no idea why.

According to the official documentation, @Named can be used to name a bean since Spring 3.0.

According to that same documentation, @DependsOn can be used to influence the bean creation order.

(note: stackoverflow breaks my second link, just search for “If you would like to influence the startup creation order” on that page)

I have tried to replace @Named(“someBean”) with @Bean(“someBean”), but then, I’m getting this error:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.some.stuff.Stuff' available: expected single matching bean but found 2: someBean,abean

The only way to fix this for now is by doing this:

    @Bean
    @Named("someBean")
    public Stuff abean() {
        //...
        return new Stuff();
    }

    @Bean
    @Named("someOtherBean")
    public Thing bbean(@Named("someBean") Stuff theGoodStuff){
        //...
        return new Thing(abean());
    }

Problem is, I will now have two beans, one used exclusively by “someOtherBean”, and another for my applicationContext.

Another way I managed to make this work: if “MyConfiguration” is in a different package as “ExternalDependencyConfig” (they’re in the same packages, as those are sister projects). In in my main application, if I add these, it will work:

<context:component-scan base-package="com.my.stuff.configuration"/>
<context:component-scan base-package="com.some.stuff.configuration"/>

However, if I swap those two, I’m getting the “BeanCreationException” mentioned earlier, and I have idea why, as it really shouldn’t matter.

This a real headhache. I have multiple combination of the different solutions here, but nothing works like I want, which is to define a bean that relies on another bean without having errors being triggered, and use that in my own context. It feels like there is something I just don’t understand about the way spring deals with beans and initialization of the application context.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật