Skip to content
Advertisement

BeanNotOfRequiredTypeException on application start

This is the simplest program I can provide – I removed all the other classes actually just to see if it would still cause the same error. Basically I have 2 classes – Test and TestConf(a configuration class). In TestConf, I create a bean for Test and in Test’s main method, I load the configuration class and then pull from the App context, the bean for Test – But I get this error in the stack trace. Here’s the code.

Test:

package net.draconia.test;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test implements Runnable
{
    public void run()
    { }

    public static void main(String[] args)
    {
        try(final ConfigurableApplicationContext objContext = new AnnotationConfigApplicationContext(net.draconia.test.conf.TestConf.class))
            {
            ((ConfigurableApplicationContext)(objContext)).registerShutdownHook();
    
            Test objBean = ((Test)(objContext.getBean(Test.class)));
    
            new Thread(objBean).start();
            }
    }
}

TestConf: package net.draconia.test.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import net.draconia.test.Test;

@Configuration
public class TestConf
{
    private Test mObjApp = null;

    @Bean
    public Test getApp()
    {
        return(mObjApp);
    }
}

I also have a POM file if you need to see that but probably not. I have a bunch of other dependencies that I didn’t remove out of that but that should’t affect anything as it’s not complaining about any dependencies.

Basically, it’s been a long time (several years) since I last used Hibernate with spring and never with spring boot which we use at work – and I was running into an issue at work with something else so I was trying to get it to build just in a simple app and adding more into it as I was getting successes but I got this error which I’ve never seen before in the over 10 years I’ve been using Spring. Can someone help me figure out why it’s happening so I can move forward and add back my Bean and DAO classes to get this thing working? I’ll check back in an hour or 2 to see results as it seems I’ve gotten responses that fast in the past. I did look on here first before I posted and nothing seemed to fit.

Thanks!

Advertisement

Answer

I don’t know how to explain it correctly. But as I understand if you return null (null is not an Object and don’t have a class) instead of object spring can not to inject it.

So, if you change your code to this everything will work:

@Bean
Test getApp() {
    return new Test();
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement