6

I am sorry that this is probably too vague and generel of a question. I am trying to bring up a huge legacy project with tons of spring wirings, and I am getting this error:

The bizzare thing is this error does not tell me on which xml, which bean id, the error occurs.

I certainly don't expect anybody to point me to where to fix.. My question is, how do spring experts even debug this kind of uninformative error message?

Yes, I understand it must be one of my xml bean was set wrongly. But shouldn't the error message make it easier for me to find out which bean?

Any advice would be extremely appreciated.

00:09:00.640 [main] ERROR com.xxxx.yyyy.zzzzzz.AppMain - Failed to initialize BLAH BLAH BLAH
java.lang.IllegalStateException: No bean class specified on bean definition
        at org.springframework.beans.factory.support.AbstractBeanDefinition.getBeanClass(AbstractBeanDefinition.java:381) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:154) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]

It's thrown by following piece of code. The init() methods takes in the spring xml files and do its wiring.

try {
    AppMain.init(args_);
} catch (Exception e) {
    _LOGGER.error("Failed to initialize BLAH BLAH BLAH", e);
    System.exit(-1);
}
CuriousMind
  • 15,168
  • 20
  • 82
  • 120

2 Answers2

1

My question is, how do spring experts even debug this kind of uninformative error message?

I'd argue that the error message is very informative. It states that something has tried to put your application in an illegal state. In this particular example, your context was configured in a way where some bean's class is not specified. This is typically caused by forgetting to put a class attribute in a <bean> declaration.

Spring creates BeanDefinition objects for each bean in your context. These can be from your <bean> declarations or other XML elements or Java configuration elements (from @Configuration, @Component, etc.). Almost everything involved in a Spring ApplicationContext is a bean.

You can be pretty sure that Spring's infrastructure beans aren't to blame for this. Spring is an established framework with tons of work put into it and testing done before every release. The immediately obvious alternative is that somewhere in your code you forgot to specify a class. Did you, for example, try to make an abstract <bean> but forget to add the abstract="true"? Did you register a BeanDefinitionParser and create BeanDefinitions without a class? Did you create BeanDefinitionPostProcessor and remove BeanDefinition class values?

Those are the things I would start with.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Yes, but don't you think there should be an easier way to figure out **which bean**? – CuriousMind Jun 05 '14 at 12:20
  • @CodeNoob Above the logs you've shown, it should have more logs saying: `Error creating bean with name...` if you are configuring your `BeanDefinition`s through `ApplicationContext` implementations. – Sotirios Delimanolis Jun 05 '14 at 17:57
  • I am facing an equally opaque unhelpful message with Grails which is built on Spring. I have used Spring for many years and despite knowing the principles of IoC which will seem very clear for simple examples, I still spend days trying to find out why the container will not instantiate a bean – user1561783 May 27 '20 at 13:56
  • @user1561783 Is there anything I can help with? – Sotirios Delimanolis May 27 '20 at 14:55
1

That's a piece of the sources where it fails You can try to set a breakpoint there in the Exception throwing. Or add the spring code rather than jar file and try to debug there.

public Class<?> getBeanClass() throws IllegalStateException {
        Object beanClassObject = this.beanClass;
        if (beanClassObject == null) {
            throw new IllegalStateException("No bean class specified on bean definition");
        }
        if (!(beanClassObject instanceof Class)) {
            throw new IllegalStateException(
                    "Bean class name [" + beanClassObject + "] has not been resolved into an actual Class");
        }
        return (Class) beanClassObject;
    }
StanislavL
  • 56,971
  • 9
  • 68
  • 98