[英]Spring Step Scoped beans do not respect @Order annotation
When I create 2 beans with @StepScope
and apply @Order
then the order for both beans still gets resolved to Ordered.LOWEST_PRECEDENCE
when trying to autowire them as a List.当我使用@StepScope
创建 2 个 bean 并应用@Order
时,两个 bean 的顺序仍然会在尝试将它们自动装配为列表时解析为Ordered.LOWEST_PRECEDENCE
。
@Bean
@Order(1)
@StepScope
public MyBean bean1(
return new MyBean("1");
}
@Bean
@Order(2)
@StepScope
public MyBean bean2(
return new MyBean("2");
}
Looking in OrderComparator
I see the beans are resolved to a source of ScopedProxyFactoryBean
which returns a null order value.查看OrderComparator
我看到 bean 被解析为ScopedProxyFactoryBean
的源,它返回 null 订单值。 Wondering if Im doing something wrong here as I would expect the ordering to work correctly.想知道我是否在这里做错了什么,因为我希望订购能够正常工作。
So the aim in to autowire an ordered list into another bean eg因此,旨在将有序列表自动装配到另一个 bean 中,例如
@Component
public class OuterBean {
private List<MyBean> beans;
public OuterBean(List<MyBean> beans) {
this.beans = beans;
}
}
And I would expect the list to contain {bean1,bean2}
我希望该列表包含{bean1,bean2}
Step scoped beans need to be used in the scope of a running step.步骤范围的 bean 需要在运行步骤的 scope 中使用。 Here is an example:这是一个例子:
import java.util.List;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
@Configuration
public class MyJob {
@Bean
@Order(1)
@StepScope
public MyBean bean1() {
return new MyBean("1");
}
@Bean
@Order(2)
@StepScope
public MyBean bean2() {
return new MyBean("2");
}
@Bean
public OuterBean outerBean(List<MyBean> beans) {
return new OuterBean(beans);
}
@Bean
public Tasklet tasklet(OuterBean outerBean) {
return (contribution, chunkContext) -> {
outerBean.sayHello();
return RepeatStatus.FINISHED;
};
}
@Bean
public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
return jobs.get("job")
.start(steps.get("step")
.tasklet(tasklet(null))
.build())
.build();
}
}
The complete code can be found here .完整的代码可以在这里找到。 This example prints:此示例打印:
bean = 1
bean = 2
which means step scoped beans have been injected in the right order.这意味着步骤范围的 bean 已以正确的顺序注入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.