为什么Spring会忽略我的@DependsOn注释? [英] Why did Spring ignore my @DependsOn annotation?

查看:171
本文介绍了为什么Spring会忽略我的@DependsOn注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring 3.1.3进行webapp,使用XML配置和组件扫描。

I'm using Spring 3.1.3 for a webapp, using XML configuration with component scanning.

我意识到其中一个扫描的组件必须先进行初始化其他几个。在所有需要构造后初始化的类上,我在方法上有一个@PostConstruct注释。

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.

为了设置依赖顺序,我将'@Component'更改为'@Component(configData)'在需要在其他人之前构建的类上。然后我在每个类定义之前添加了'@DependsOn(configData)',需要在configDatabean之后进行后构造。

To set up the dependency order, I changed '@Component' to '@Component("configData")' on the class that needs to be post-constructed before the others. I then added '@DependsOn("configData")' just before each class definition that needs to be post-constructed AFTER the "configData" bean.

从我的'我已经阅读了,这就是我需要强制执行依赖顺序。

From what I've read, this is all I need to enforce the dependency order.

然后我构建了所有内容,设置了断点,并启动了应用程序。我希望在任何依赖bean之前点击configDatabean中的断点。这不是发生的事情。第一个断点位于其中一个依赖bean的init方法中。

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. This isn't what happened. The first breakpoint was in the "init" method of one of the dependent beans.

然后我更改了我的log4j.xml以将debug设置为日志记录级别对于org.springframework并重新进行测试。断点行为是相同的,我的日志记录没有显示有关Spring初始化的任何调试信息(我已经调试了log4j初始化本身,所以我确认我为org.springframework设置了DEBUG。)

I then changed my "log4j.xml" to set "debug" as the logging level for "org.springframework" and reran my test. 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").

我可能缺少什么?

更新:

如果重要,这里有几个我在这里做的骨架例子。

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();
        ....
    }
}

重申一下,我在运行时发现的是ClassDependentOnConfigData中的init()方法是由Spring在ConfigData中的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".

另请注意,BaseClass对ConfigData有一个@Autowired。

Note also that "BaseClass" has an "@Autowired" for "ConfigData".

推荐答案

(来自其他人的正确但现在已删除答案)

(From someone else's correct but now deleted answer)

@DependsOn合约仅保证已构造bean并已设置属性。这并不能保证调用任何@PostConstruct方法。

The @DependsOn contract only guarantees that the bean has been constructed and properties have been set. This will not guarantee that any @PostConstruct methods have been called.

让它工作的方法是拥有dependee类(其他人依赖的类) )实现InitializingBean类,这需要实现afterPropertiesSet()方法。我将init()方法的原始主体放入此方法中。我确认现在在依赖于此的任何类之前执行此操作。

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. I put the original body of my "init()" method into this method. I verified that this is now executed before any of the classes that depend on this.

原始答案中提到的另一件事是,如果我定义了我的dependee 在XML中使用bean并使用init-method属性,这个WOULD在任何依赖于它的类之前执行。我没有验证这一点。

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. I didn't verify this.

这篇关于为什么Spring会忽略我的@DependsOn注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆