Can I ignore a BeanCreationException and throw null instead?

We have a situation where our Spring connects some beans that include ActiveMQ classes created using Java 6. Our application runs on client servers, so we cannot guarantee that they have Java 6 or later installed. If they have Java 5, the application cannot start due to a BeanCreationException with classes that depend on ActiveMQ (the main reason is UnsupportedClassVersionError ).

So my question is: is there a way to ignore BeanCreationException and still run the application? I want to show an error message saying that they need to install Java 6 or later, but since the application does not even start, I will never have the opportunity to do this.

My guess is that there is no way to do this, because Spring should be able to guarantee that my application builds correctly after initialization, but I thought I would ask anyway. Any other suggestions on how to fulfill my ultimate goal will also be helpful and appreciated.

We are using Spring 3.0.6

Thanks!

+8
java spring dependency-injection java-5 java-6
source share
3 answers

Firstly, any combines that are a subclass of java.lang.Error are generally considered unrecoverable. Therefore, when you can catch them, he is very discouraged :

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

However, if all you are going to do is display an error message, then you should get away from it.

SO, to get back to your question, I suggest creating an implementation of the Spring FactoryBean interface that will try to load ActiveMQ Classes. If it works, it can return the corresponding object from FactoryBean.getObject . If it fails (via the caught UnsupportedClassVersionError ), it can return either null or some other object representing this condition.

0
source share

If you can upgrade to Spring 3.1 (stable), use the 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 can be used to implement the same logic. Instead of returning null you can also return some fake implementation, which you may discover later, and alert the user when the application tries to use it.

+4
source share

You can create a factory bean and create a real ActiveMQ bean. If it cannot be initialized, the factory can return a dummy / layout implementation so that everything does not break. You might later ask the factory if all goes well.

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

0
source share

All Articles