简体   繁体   中英

BeanNotOfRequiredTypeException: spring factory not being resolved to child type

I'm having an issue with a PropertiesFactoryBean not being resulved to a Properties object in spring. Instead I get the following error:

org.springframework.beans.factory.BeanNotOfRequredTypeException: Bean named 'authProperties' must be of type [java.util.Properties], but was actually of type [org.springframework.beans.factory.config.PropertiesFactoryBean]
    at org.springFramework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:367)
    at org.springFrameowkr.beans.factory.support.AbstractBeanFactory.etBean(AbstractBeanFactory.java:198]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121
    at  *our code here*

This code was written by others who have used it reliably for some time, so the issue is likely configuration issue rather then bad code. However, the failure occurs on the return line of this method:

private static Properties getPropsFromContext(String context) {
    try(AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(context)){
        return ctx.getBeans("authProperties", Properties.class));
    }
}

The relevant lines of the XML file are:

<bean id="authProperties"  class="org.springFramework.beans.factory.config.PropertiesFactoryBean">
   <property name="location" value="classpath:props/auth.properties" />
</bean>

Ultimately the code is being called from within tomcat. We are using our own RealmBase class for the tomcat Relm, which calls this code. The jar containing our RealmBase class is in the tomcat/lib directory.

What should happen is that spring should recognize that PropertiesFactoryBean implements the FactoryBean interface and it should call getObject of the PropertiesFactoryBean as part of the getObjectForBeanInstance called by doGetBean method, which would return the Properties object. However, for some reason this is not happening, and I'm getting the PropertiesFactoryBean object back instead of the Property being returned. I've even looked through the code to verify this.

Our unit tests of this method run fine, which once again suggests the failure is not with the code, but sometime after it gets compiled, likely a configuration issue with tomcat. As far as I can tell there are no duplicate spring jars in the tomcat classpath or other obvious naming collisions.

I'm using Tomcat6. Our spring jars are mostly 3.2.9, a few 3.1.0, but I think the relevant jars are all 3.2.9

Why would it fail to generate the Property value correctly?

I ultimately discovered I was running an outdated version of the realm jar. When I installed the new Realm things worked correctly. I assume my issue was that the old Realm was compiled to a different version of spring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM