我可以忽略 BeanCreationException 并注入 null 吗?

发布于 2024-12-27 21:38:21 字数 495 浏览 1 评论 0原文

我们遇到这样一种情况,我们的 Spring 连接了一些包含使用 Java 6 构建的 ActiveMQ 类的 bean。我们的应用程序在客户的服务器上运行,因此我们不能保证他们安装了 Java 6 或更高版本。如果它们碰巧有 Java 5,则应用程序将无法启动,因为依赖于 ActiveMQ 的类会出现 BeanCreationException(根本原因是 UnsupportedClassVersionError)。

所以我的问题是,有没有办法忽略 BeanCreationException 并仍然启动应用程序?我希望能够显示一条错误消息,指出他们需要安装 Java 6 或更高版本,但由于应用程序甚至无法启动,我从来没有机会这样做。

我的预感是没有办法做到这一点,因为 Spring 需要能够保证我的应用程序在初始化后正确构建,但我想我还是会问。关于如何实现我的最终目标的任何其他建议也会有所帮助和赞赏。

我们正在使用 Spring 3.0.6

谢谢!

We have a situation where our Spring wires up some beans that include ActiveMQ classes built with Java 6. Our application runs on customer's servers, so we can't guarantee that they have Java 6 or later installed. If they happen to have Java 5, the application can't start because of BeanCreationExceptions with the classes that depend on ActiveMQ (root cause being a UnsupportedClassVersionError).

So my question is, is there any way to ignore a BeanCreationException and still start the application? I want to be able to display an error message saying that they need to install Java 6 or later, but since the application can't even start, I never have a chance to do this.

My hunch is that there is no way to do this, because Spring needs to be able to guarantee that my application is built properly after initialization, but I thought I would ask anyway. Any other suggestions for how to accomplish my end goal would also be helpful and appreciated.

We are using Spring 3.0.6

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你的手,一向走下去 2025-01-03 21:38:21

如果您可以升级到 Spring 3.1(稳定),请利用 Java 配置:

@Bean
public SomeBean public someBean() {
    if(isEverythingOkWithJavaVersion()) {
        return new CorrectBean();
    } else {
        return null;
    }
}

或者:

@Bean
public SomeBean public someBean() {
    try {
        return new CorrectBean();
    } catch(UnsupportedClassVersionError e) {
        log.warn("", e);
        return null;
    }
}

在旧版本的 Spring 中,FactoryBean 可能用于实现相同的逻辑。您还可以返回一些虚假的实现,您可以稍后发现这些实现,并在应用程序尝试使用它时警告用户,而不是返回 null。

If you can upgrade to Spring 3.1 (stable), take advantage of Java configuration:

@Bean
public SomeBean public someBean() {
    if(isEverythingOkWithJavaVersion()) {
        return new CorrectBean();
    } else {
        return null;
    }
}

or:

@Bean
public SomeBean public someBean() {
    try {
        return new CorrectBean();
    } catch(UnsupportedClassVersionError e) {
        log.warn("", e);
        return null;
    }
}

In older versions of Spring FactoryBean might be used to implement the same logic. Instead of returning null you might also return some fake implementation that you can discover later and warn the user when the application tries to use it.

哎呦我呸! 2025-01-03 21:38:21

首先,任何 java.lang.Error 子类的 throwable 通常被认为是不可恢复的。因此,虽然可以捕获它们,但强烈建议不要这样做:

ErrorThrowable 的子类,它指示合理的应用程序不应尝试捕获的严重问题。大多数此类错误都是异常情况。

但是,如果您要做的只是显示一条错误消息,那么您应该能够摆脱它。

所以回到你的问题,我建议 创建 Spring 的 FactoryBean 接口的实现,它将尝试加载 ActiveMQ 类。如果它有效,那么它可以从FactoryBean.getObject返回适当的对象。如果失败(通过捕获 UnsupportedClassVersionError),则它可以返回 null 或表示该条件的其他对象。

First off, any throwables that are a subclass of java.lang.Error are generally considered to be non-recoverable. So while it's possible to catch them, it's strongly discouraged:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

However, if all you're going to do is display an error message, then you should be able to get away with it.

SO to get back to your question, I suggest creating an implementation of Spring's FactoryBean interface, which would try to load the ActiveMQ classes. If it works, then it can return the appropriate object from FactoryBean.getObject. If it fails (via a caught UnsupportedClassVersionError), then it can return either a null, or some other object representing that condition.

空城仅有旧梦在 2025-01-03 21:38:21

您可以创建一个工厂 bean 并让它创建实际的 ActiveMQ bean。如果无法初始化,工厂可能会返回一个虚拟/模拟实现,这样事情就不会中断。稍后你可以询问工厂是否一切顺利。

http://static.springsource .org/spring/docs/2.5.x/reference/beans.html#beans-factory-extension-factorybean

You could create a factory bean and let it create the actual ActiveMQ bean. If it can't be initialized the factory could return a dummy/mock implementation so things don't break. Later you could ask the factory if everything went alright.

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-extension-factorybean

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文