TheLordOne
TheLordOne

Reputation: 65

Probleme in AOP configuration seems to be invalid

When I try to log somme function in my Dao CLASS using Log4j, I have Exception in thread "main" org.springframework.aop.AopInvocationException: AOP configuration seems to be invalid: tried calling method [public abstract void tp.dao.IDao.addProduit(tp.entity.Produit)] on target [tp.entity.Produit@27e32fe4]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class

I'm using spring 5, hibernate, MySQL

My model:

public class Produit {

            @Id
            @GeneratedValue(strategy=GenerationType.IDENTITY)
            private long idProduit;
            private String designationProduit;
            private double prixProduit;


            public long getIdProduit() {
                return idProduit;
            }
            public void setIdProduit(long idProduit) {
                this.idProduit = idProduit;
            }
            public String getDesignationProduit() {
                return designationProduit;
            }
            public void setDesignationProduit(String designationProduit) {
                this.designationProduit = designationProduit;
            }
            public double getPrixProduit() {
                return prixProduit;
            }
            public void setPrixProduit(double prixProduit) {
                this.prixProduit = prixProduit;
            }
}

Spring configuration:

<?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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


  <bean name="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName"    value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/spring_test"/>
                <property name="username" value="root"/>
                <property name="password" value=""/>
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
                   <property name="dataSource" ref="datasource"/>
                   <property name="annotatedClasses">
                        <list>
                                <value>tp.entity.Produit</value>    
                        </list>
                   </property>
                   <property name="hibernateProperties">
                        <props>
                                        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                                        <prop key="hibernate.show_sql">true</prop>
                                        <prop key="hibernate.hbm2ddl.auto">create</prop>                        
                        </props>
                   </property>
    </bean>
            <tx:annotation-driven transaction-manager="transactionManager"/>

        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory" />
        </bean>

        <bean id="dao" class="tp.dao.Dao">
                    <property name="sessionfactory"   ref="sessionFactory" /> 
        </bean>

        <bean id="produit" class="tp.entity.Produit"></bean>
        <bean id="logAop" class="tp.aop.AnotherLog"></bean>


        <bean  id="compteProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

                                <property name="proxyInterfaces" >
                                            <value>tp.dao.IDao</value>
                                </property>


                                                                <property name="target" ref="produit"></property>

                            <property name="interceptorNames">
                                    <list>
                                            <value>logAop</value>
                                    </list>
                            </property>

        </bean>

</beans>

My Log Class:

package tp.aop;

import java.lang.reflect.Method;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class AnotherLog implements MethodBeforeAdvice,AfterReturningAdvice {

    private final Logger logger = LogManager.getLogger(this.getClass());

    @Override
    public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {

            logger.error("Transaction "+arg1.getName()+"Est Termine");
    }

    @Override
    public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
        logger.error(" Begin Transaction "+arg0.getName());
    }



}

My Dao Class (this class contains function that i want to log using log4J):

package tp.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import tp.entity.Produit;

@Transactional
public class Dao implements IDao {

    private SessionFactory sessionfactory;

    public void setSessionfactory(SessionFactory sessionfactory) {
        this.sessionfactory = sessionfactory;
    }

    @Override
    public void addProduit(Produit p) {
        Session session = sessionfactory.getCurrentSession();
        session.persist(p);

    }

    @Override
    public void deleteProduit(Produit p) {
        Session session = sessionfactory.getCurrentSession();
        session.delete(p);

    }

    @Override
    public Produit getProduit(long idProduit) {
        Session session = sessionfactory.getCurrentSession();
        return (Produit)session.createQuery("select p from Produit p where p.idProduit="+idProduit).uniqueResult();
    }

    @Override
    public List<Produit> getProduits() {
        Session session = sessionfactory.getCurrentSession();
        return session.createQuery("from Produit ").list();
    }

}

Dao :

package tp.dao;

import java.util.List;

import tp.entity.Produit;

public interface IDao {

    public void addProduit(Produit p);
    public void deleteProduit(Produit p);
    public Produit getProduit(long idProduit);
    List<Produit> getProduits();



}

The error:

Exception in thread "main" org.springframework.aop.AopInvocationException: AOP configuration seems to be invalid: tried calling method [public abstract void tp.dao.IDao.addProduit(tp.entity.Produit)] on target [tp.entity.Produit@27e32fe4]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:346)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy32.addProduit(Unknown Source)
    at tp.test.Test.main(Test.java:24)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:338)
    ... 9 more

Upvotes: 1

Views: 5683

Answers (1)

kriegaex
kriegaex

Reputation: 67297

Disclaimer: I am not a Spring user, so I am not sure if my following suggestion is correct, but I think I might have spotted a problem there.

Your compteProxy bean definition looks like this:

<bean id="compteProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces">
    <value>tp.dao.IDao</value>
  </property>
  <property name="target" ref="produit"></property>
  <property name="interceptorNames">
    <list>
      <value>logAop</value>
    </list>
  </property>
</bean>

Actually I guess the XML structure both according to the XML schema and to this example in the Spring manual should be:

<bean id="compteProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces" value="tp.dao.IDao"/>
  <property name="target" ref="produit"/>
  <property name="interceptorNames">
    <list>
      <value>logAop</value>
    </list>
  </property>
</bean>

I.e., there should not be a <value>...</value> tag but just a value="..." property. Maybe you have other problems as well, but this is what I think you should fix first.


Update 2: Well, I guess beside the syntactic error you also have a logical error in your XML: Should

<property name="target" ref="produit"/>

not rather be

<property name="target" ref="dao"/>

? Because because bean dao implements IDao, produit does not. Of course Spring cannot cast one into the other. ;-)

Upvotes: 1

Related Questions