Close

Spring Framework - @Order Examples

Spring Framework 

Using @Order annotation on class level, along with @Component annotation. This is the component scanning approach where the container register the beans with @Component on class level. It's alternative to approach where the beans are registered by calling factory methods annotated with @Bean.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

@PropertySource("classpath:app-props.properties")
@Configuration
@ComponentScan(basePackageClasses = ListInjectionScanRefOrderExample.class,
useDefaultFilters = false,
includeFilters = @ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = {ListInjectionScanRefOrderExample.class}))

public class ListInjectionScanRefOrderExample {

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
ListInjectionScanRefOrderExample.class);

TestBean bean = context.getBean(TestBean.class);
System.out.println(bean.getRefBeanServices());
}

@Component
public static class TestBean {

private List<RefBeanService> refBeanServices;

@Autowired
public void setRefBeanServices (List<RefBeanService> refBeanServices) {
this.refBeanServices = refBeanServices;
}

public List<RefBeanService> getRefBeanServices () {
return refBeanServices;
}
}

public static interface RefBeanService {
String getStr ();
}

@Component
@Order(3)
public static class RefBean implements RefBeanService {
private String str;

public String getStr () {
return str;
}

@Value("${some-prop1:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{str='" + str + "'}";
}
}

@Component
@Order(1)
public static class RefBean2 implements RefBeanService {
private String str;

@Override
public String getStr () {
return str;
}

@Value("${some-prop2:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{str='" + str + "'}";
}
}

@Component
@Order(2)
public static class RefBean3 implements RefBeanService {
private String str;

@Override
public String getStr () {
return str;
}

@Value("${some-prop3:defaultStr}")
public void setStr (String str) {
this.str = str;
}

@Override
public String toString () {
return "RefBean{ str='" + str + "'}";
}
}
}
Original Post




org.springframework.core.annotation.Order annotation is used to arrange elements of arrays or lists in the provided ordinal number (defined as 'value' attribute). Alternatively we can use Ordered interface.

This example demonstrate how to order the elements of the injected array using @Order annotation.
The annotation is used on configuration method, along with @Bean annotation.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.Arrays;

@Configuration
public class ArrayInjectOrderExample {

@Bean
public TestBean testBean () {
return new TestBean();
}

@Bean
@Order(3)
public String refString () {
return "my string 1";
}

@Bean
@Order(1)
public String refString2 () {
return "my string 2";
}

@Bean
@Order(2)
public String refString3 () {
return "my string 3";
}

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
ArrayInjectOrderExample.class);

TestBean bean = context.getBean(TestBean.class);
System.out.println(Arrays.toString(bean.getStringArray()));
}

private static class TestBean {
private String[] stringArray;

@Autowired
public void setStringArray (String[] stringArray) {
this.stringArray = stringArray;
}

public String[] getStringArray () {
return stringArray;
}
}
}
Original Post




See Also