0

I am running a third party application that calls my class whenever a new record is inserted in a database. My classname is in a property file that this third party program is using. So this program is calling my class using reflection.

My class needs to access a SpringComponent called titleService. I can get the bean the following away.

 ApplicationContext ctx = SpringContext.getApplicationContext();
 Object titleServiceObject = ctx.getBean("titleService");

This works fine, but I have to use reflection to call the method I want since it is an Object and not a concrete class.

I have tried to cast it to a TitleService object but I get the error:

    org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
Bean named 'titleService' is expected to be of type 'com.title.TitleService' but was actually of type 'com.title.TitleService'

I don't understand the error message since the class name for what is expected is the same as what it actually is.

I have also tried getting the bean this way:

TitleService titleService = ctx.getBean(TitleService.class)

but I get the same error.

The TitleService object has several autowired components so I cannot use "new" to create this object.

How can I get a TitleObject object?

  • is your spring configuration via java class configuration or via xml file configuration? – zawarudo Mar 10 '22 at 14:00
  • It is via java class configuration. There is a main class that it is @SpringBootApplication and it scans the other components. – Dave Trower Mar 10 '22 at 15:08
  • 1
    Class equality is based on classname **and** the classloader they are in. So the same class in different classloaders isn't equal. Judging from the error you get you have classloading issues. – M. Deinum Mar 10 '22 at 15:36

1 Answers1

0

The problem seems to be caused by using spring-dev-tools . I stopped using Spring-dev-tools and the problem has gone away. I am doing more research as to why this is the case. I found this link here that describes how using spring-dev-tools can cause classloader issues: enter link description here