当我尝试使用Log4j
在我的Dao CLASS中记录somme函数时,我在线程“main”org.springframework.aop.AopInvocationException
中有异常:AOP配置似乎无效:尝试调用方法[public abstract void tp.dao.IDao.addProduit(tp.entity.Produit)]
on target [tp.entity.Produit@27e32fe4];
嵌套异常是java.lang.IllegalArgumentException:
对象不是声明类的实例
我正在使用spring 5,hibernate,MySQL
我的模特:
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配置:
<?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>
我的日志类:
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(此类包含我想使用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();
}
}
道:
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();
}
错误:
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
答案 0 :(得分:1)
免责声明:我不是Spring用户,因此我不确定以下建议是否正确,但我想我可能已经发现了一个问题。
您的compteProxy
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>
实际上我认为根据XML模式和Spring手册中的this example的XML结构应该是:
<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>
即,不应该有<value>...</value>
标签,而应该只有value="..."
属性。也许你还有其他问题,但我认为你应该先解决这个问题。
更新2:好吧,我想除了语法错误之外,你的XML中也有一个逻辑错误:应该
<property name="target" ref="produit"/>
不是
<property name="target" ref="dao"/>
?因为bean dao
实现IDao
,produit
不实现qtbase/mkspecs/features/mac/default_post.prf
。当然Spring不能把一个投入另一个。 ; - )