schneida
schneida

Reputation: 809

Spring xml config equivalent to @Order

Given the following Java class

@Order(12)
@Component
public class MyComponent {

    //....

}

what is the equivalent in the Spring XML configuration? I couldn't find anything matching the @Order annotation for the XML based configuration:

<bean class="MyComponent" />

Upvotes: 1

Views: 2882

Answers (1)

Beri
Beri

Reputation: 11610

In spring you have 2 choices:

  • annotation

  • interface implementation

In your case you will have to go with the second option.

Your class needs to implement Ordered, but this will bind your class with spring API. It's same when using annotation over class.

But if you are using configuration classes, instead of xml config, then you can have plain java beans, and keep all Spring API in configurations. Example:

    @Bean(destroyMethod = "shutdown")
    @Order(12)
    public ScheduledExecutorService scheduledExecutorService() {
        return Executors.newSingleThreadScheduledExecutor();
    }

Configuration classes give you the option to separate Spring API(annotations) from your beans.

Upvotes: 2

Related Questions