简体   繁体   English

为什么Spring会忽略我的@DependsOn注释?

[英]Why did Spring ignore my @DependsOn annotation?

I'm using Spring 3.1.3 for a webapp, using XML configuration with component scanning. 我使用Spring 3.1.3作为webapp,使用XML配置和组件扫描。

I realized that one of the scanned components has to be initialized before several others. 我意识到其中一个被扫描的组件必须在其他几个之前进行初始化。 On all the classes that need post-construct initialization, I have a @PostConstruct annotation on a method. 在需要构造后初始化的所有类上,我在方法上有一个@PostConstruct注释。

To set up the dependency order, I changed '@Component' to '@Component("configData")' on the class that needs to be post-constructed before the others. 为了设置依赖顺序,我将'@Component'更改为'@Component(“configData”)'在需要在其他之前构建后的类上。 I then added '@DependsOn("configData")' just before each class definition that needs to be post-constructed AFTER the "configData" bean. 然后我在每个类定义之前添加了'@DependsOn(“configData”)',需要在“configData”bean之后进行后构建。

From what I've read, this is all I need to enforce the dependency order. 从我读过的内容来看,这就是我需要强制执行依赖顺序的全部内容。

I then built everything, set my breakpoints, and started up the app. 然后,我构建了所有内容,设置了断点,并启动了应用程序。 I expected to hit the breakpoint in the "configData" bean before any of the dependent beans. 我希望在任何依赖bean之前点击“configData”bean中的断点。 This isn't what happened. 这不是发生的事情。 The first breakpoint was in the "init" method of one of the dependent beans. 第一个断点位于其中一个依赖bean的“init”方法中。

I then changed my "log4j.xml" to set "debug" as the logging level for "org.springframework" and reran my test. 然后我更改了我的“log4j.xml”以将“debug”设置为“org.springframework”的日志记录级别并重新进行测试。 The breakpoint behavior was the same, and my logging didn't show any debug information about Spring initialization (I have debugging on for log4j initialization itself, so I confirmed that I had DEBUG set for "org.springframework"). 断点行为是相同的,我的日志记录没有显示有关Spring初始化的任何调试信息(我已经调试了log4j初始化本身,所以我确认我为“org.springframework”设置了DEBUG)。

What might I be missing? 我可能会失踪什么?

Update: 更新:

If it matters, here are a couple of skeleton examples of what I'm doing here. 如果重要的话,这里有几个我在这里做的骨架例子。

@Component("configData")
public class ConfigData {
    ....
    @PostConstruct
    public void init() {
        ....
    }
}

@Component
@DependsOn("configData")
public class ClassDependentOnConfigData extends BaseClass {
    ....
    @Override
    @PostConstruct
    public void init() {
        super.init();
        ....
    }
}

To reiterate, what I'm finding at runtime is that the "init()" method in "ClassDependentOnConfigData" is being called by Spring before the "init()" method in "ConfigData". 重申一下,我在运行时发现的是“ClassDependentOnConfigData”中的“init()”方法是由Spring在“ConfigData”中的“init()”方法之前调用的。

Note also that "BaseClass" has an "@Autowired" for "ConfigData". 另请注意,“BaseClass”对于“ConfigData”具有“@Autowired”。

(From someone else's correct but now deleted answer) (来自其他人的正确但现已删除的答案)

The @DependsOn contract only guarantees that the bean has been constructed and properties have been set. @DependsOn合约仅保证已构造bean并已设置属性。 This will not guarantee that any @PostConstruct methods have been called. 这不保证已调用任何@PostConstruct方法。

The way to get this to work is to have the "dependee" class (the class that others depend on) implement the "InitializingBean" class, which requires implementing the "afterPropertiesSet()" method. 让它工作的方法是让“dependee”类(其他人依赖的类)实现“InitializingBean”类,这需要实现“afterPropertiesSet()”方法。 I put the original body of my "init()" method into this method. 我将我的“init()”方法的原始主体放入此方法中。 I verified that this is now executed before any of the classes that depend on this. 我确认现在在依赖于此的任何类之前执行此操作。

Another thing that was mentioned in the original answer is that if I had defined my "dependee" bean in XML and used the "init-method" property, this WOULD have executed before any of the classes that depend on this. 原始答案中提到的另一件事是,如果我在XML中定义了我的“dependee”bean并使用了“init-method”属性,那么这个WOULD已经在任何依赖于它的类之前执行了。 I didn't verify this. 我没有验证这一点。

I also encoutered the same problem, but still not properly resolved. 我也遇到了同样的问题,但仍然没有得到妥善解决。 As a part of a solution Spring documentation says: 作为解决方案的一部分,Spring文档说:

"Using DependsOn at the class level has no effect unless component-scanning is being used." “除非正在使用组件扫描,否则在类级别使用DependsOn无效。”

This is the reason why the @dependsOn annotation has no effect. 这就是@dependsOn注释无效的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM