开发者

ClassCastException when getting bean from Spring app context, but not when Autowiring

开发者 https://www.devze.com 2023-02-24 17:02 出处:网络
I don\'t get this, I have a typical messageSource <bean id=\"resource\" class=\"org.springframework.context.support.ResourceBundleMessageSo开发者_开发问答urce\">

I don't get this, I have a typical messageSource

<bean id="resource" class="org.springframework.context.support.ResourceBundleMessageSo开发者_开发问答urce">
    <property name="basenames">
        <list>
            <value>content.Language</value> 
        </list>
    </property>
</bean>

and I can get it like

@Autowire protected MessageSource resource;

but when I try to get it from app context with casting to MessageSource I get a cast exception

how is autowire and getting bean from app context different ?

(MessageSource) _applicationContext.getBean(name);

java.lang.ClassCastException: org.springframework.context.support.ResourceBundleMessageSource cannot be cast to org.springframework.context.MessageSource

it implements the interface, why it cannot be cast to it ?


I bet it's a classloader issue, it's very likely that you are using a different classloader for loading that bean. Because there is no other valid reason for this to happen.

0

精彩评论

暂无评论...
验证码 换一张
取 消