@Slf4j(topic = "e")
@Order(1)
@Component
public class OrderTestService1 {
public OrderTestService1(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
}
@Slf4j(topic = "e")
@Order(2)
@Component
public class OrderTestService2 {
public OrderTestService2(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
}
@Slf4j(topic = "e")
@Order(3)
@Component
public class OrderTestService3 {
public OrderTestService3(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
}
打印顺序如下: OrderTestService1.java 行数=13 10:56:20.756 [main] DEBUG e - order-1 OrderTestService2.java 行数=13 10:56:20.760 [main] DEBUG e - order-2 OrderTestService3.java 行数=13 10:56:20.761 [main] DEBUG e - order-3
例子二
改变OrderTestService三个类的注解序值
@Slf4j(topic = "e")
@Order(3)
@Component
public class OrderTestService1 {
public OrderTestService1(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
}
@Slf4j(topic = "e")
@Order(2)
@Component
public class OrderTestService2 {
public OrderTestService2(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
}
@Slf4j(topic = "e")
@Order(1)
@Component
public class OrderTestService3 {
public OrderTestService3(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
}
@Slf4j(topic = "e")
public class E {
public void orderList(){
int orderValue = 0;
if(this.getClass().isAnnotationPresent(Order.class)){
Order order = this.getClass().getAnnotation(Order.class);
orderValue = order.value();
}
log.debug("List Order postProcessBeanFactory {} order={}",this.getClass().getSimpleName(),orderValue);
}
}
@Slf4j(topic = "e")
@Order(3)
@Component
public class OrderTestService1 extends E{
public OrderTestService1(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
}
@Slf4j(topic = "e")
@Order(2)
@Component
public class OrderTestService2 extends E{
public OrderTestService2(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
}
@Slf4j(topic = "e")
@Order(1)
@Component
public class OrderTestService3 extends E{
public OrderTestService3(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
}
@Component
public class OrderTestService {
List beanFactoryPostProcessor;
public List getBeanFactoryPostProcessor() {
return beanFactoryPostProcessor;
}
@Autowired
public void setBeanFactoryPostProcessor(List beanFactoryPostProcessor) {
this.beanFactoryPostProcessor = beanFactoryPostProcessor;
}
}
打印顺序如下: E.java 行数=15 11:01:47.756 [main] DEBUG e - List Order postProcessBeanFactory OrderTestService3 order=1 E.java 行数=15 11:01:47.756 [main] DEBUG e - List Order postProcessBeanFactory OrderTestService2 order=2 E.java 行数=15 11:01:47.756 [main] DEBUG e - List Order postProcessBeanFactory OrderTestService1 order=3
@Slf4j(topic = "e")
@Order(3)
@Component
public class OrderTestService1 extends E implements BeanFactoryPostProcessor {
public OrderTestService1(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
int orderValue = 0;
if(this.getClass().isAnnotationPresent(Order.class)){
Order order = this.getClass().getAnnotation(Order.class);
orderValue = order.value();
}
log.debug("execute postProcessBeanFactory a order={}",orderValue);
}
}
@Slf4j(topic = "e")
@Order(2)
@Component
public class OrderTestService2 extends E implements BeanFactoryPostProcessor {
public OrderTestService2(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
int orderValue = 0;
if(this.getClass().isAnnotationPresent(Order.class)){
Order order = this.getClass().getAnnotation(Order.class);
orderValue = order.value();
}
log.debug("execute postProcessBeanFactory a order={}",orderValue);
}
}
@Slf4j(topic = "e")
@Order(1)
@Component
public class OrderTestService3 extends E implements BeanFactoryPostProcessor {
public OrderTestService3(){
log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
int orderValue = 0;
if(this.getClass().isAnnotationPresent(Order.class)){
Order order = this.getClass().getAnnotation(Order.class);
orderValue = order.value();
}
log.debug("execute postProcessBeanFactory a order={}",orderValue);
}
}
This article is from an interview with Zuhaib Siddique, a production engineer at HipChat, makers of group chat and IM for teams.
HipChat started in an unusual space, one you might not