BeanNotOfRequiredTypeException with Spring AOP

I tried my hands in spring aop and under spring config file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean id="eddie" class="com.springinaction.Instrumentalist">
        <property name="instrument" ref="violin"></property>
        <property name="song" value="Samarame"></property>

    </bean>


    <bean id="kenny" class="com.springinaction.Instrumentalist">
        <property name="song" value="SAMARAME "></property>
        <property name="instrument" ref="saxopone"></property>
    </bean>

    <bean id="hank" class="com.springinaction.OneManBand">
        <property name="instruments">
            <props>
                <prop key="GUITAR">STRUM STRUM STRUM</prop>
                <prop key="CYMBAL">CRASH CRASH CRASH CRASH</prop>
                <prop key="HARMONICA">HUM HUM HUM</prop>
            </props>
        </property>
    </bean>

    <bean id="guitar" class="com.springinaction.Guitar">
    </bean>

    <bean id="violin" class="com.springinaction.Violin">
    </bean>

    <bean id="tabala" class="com.springinaction.Tabala">
    </bean>

    <bean id="saxopone" class="com.springinaction.Saxophone">
    </bean>

    <bean id="audience" class="com.springinaction.Audience"></bean>

    <aop:config>

        <aop:aspect ref="audience">

            <aop:before pointcut="execution(* com.springinaction.Performer.perform(..))" method="takeSeats()"/>
        </aop:aspect>
    </aop:config>

</beans>

when I run the code, I get an error:

An exception in the stream "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: A bean named 'eddie' must be of type [Com.springinaction.Instrumentalist], but was actually of type [$ Proxy4] in org.springframework.beans.factory. support.AbstractBeanFactory.doGetBean (AbstractBeanFactory.javahaps48) in org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:193) in org.springframework.context.support.AbstractAonstractAonstract 1008) at com.springinaction.Main.main (Main.java:12)

<aop:config> spring, .

?

+3
1

Spring AOP -. - . "", , . Javadoc - .

, beans , Spring . . Spring bean eddie -, . , bean eddie, - .

Main, stacktrace , . , Main , -

Instrumentalist eddie = (Instrumentalist) appContext.getBean("eddie", Instrumentalist.class);

getBean(String, Class) Spring , bean , , . , . - Instrumentalist, , $Proxy4. ( - Instrumentalist, - java.lang.reflect.Proxy).

- , . Spring , Instrumentalist Performer, - Performer.

Performer eddie = (Performer) appContext.getBean("eddie", Performer.class);

, perform() eddie, .

+7

Source: https://habr.com/ru/post/1762050/


All Articles