【问题标题】:@Autowired on the method, is the method executed at runtime random?@Autowired 在方法上,该方法是在运行时随机执行的吗?
【发布时间】:2020-06-02 11:17:36
【问题描述】:

我初始化了,发现方法执行是随机的。 但我不知道为什么会这样?

代码如下图,输出顺序有时不一样

@Autowired
public void test() throws Exception {
    System.out.println("test():1");
}
@Autowired
public void test2( MyUserDetialsService userService) throws Exception {
    System.out.println("test2( MyUserDetialsService userService):5");
}
@Autowired
public void a( MyUserDetialsService userService) throws Exception {
    System.out.println("a( MyUserDetialsService userService):2");
}
@Autowired
public void a() throws Exception {
    System.out.println("a():3");
}

【问题讨论】:

  • 最好是代码 sn-p 而不是屏幕截图

标签: spring-boot autowired


【解决方案1】:

执行顺序按字母顺序排列。您可以通过将函数名称从 test 更改为 ztest 来查看它,您会看到输出会发生变化。

我还进行了更多测试,发现有时它不是按字母顺序排列的。这也取决于参数编号。但没有参数它是按字母顺序排列的。你可以在这里看到我的例子https://github.com/ozkanpakdil/spring-examples/tree/master/autowired-which-function-works-first

【讨论】:

    猜你喜欢
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 2018-06-01
    • 2015-04-06
    相关资源
    最近更新 更多