Spring AOP 问题

xiaoweiweixiaomoumou 2010-10-29 07:45:16
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="BasehiberNateDao" abstract="true"
class="com.aptech.jb.epet.dao.hibimpl.BasehiberNateDao" />
<bean id="petDiaryDao"
class="com.aptech.jb.epet.dao.hibimpl.PetDiaryDaoHibImpl"
parent="BasehiberNateDao" />
<bean id="petInfoDao"
class="com.aptech.jb.epet.dao.hibimpl.PetInfoDaoHibImpl"
parent="BasehiberNateDao" />
<bean id="petDiaryBiz"
class="com.aptech.jb.epet.biz.impl.PetDiaryBizImpl">
<property name="petDiaryDao" ref="petDiaryDao"></property>
</bean>
<bean id="petInfoBiz"
class="com.aptech.jb.epet.biz.impl.PetInfoBizImpl">
<property name="petInfoDao" ref="petInfoDao"></property>
</bean>

<!-- aop -->

<bean id="petInfoBizTarget"
class="com.aptech.jb.epet.biz.impl.PetInfoBizImpl">
<property name="petInfoDao" ref="petInfoDao"></property>
</bean>
<bean id="LotteryAdvice"
class="com.aptech.jb.epet.aop.LotteryAdvice">
<property name="petInfoDao" ref="petInfoDao"></property>
</bean>

<bean id="petInfoBizProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.aptech.jb.epet.dao.PetInfoDao</value>
</property>
<property name="interceptorNames">
<list>
<value>LotteryAdvice</value>
</list>
</property>
<property name="target" ref="petInfoBizTarget"></property>
</bean>
</beans>



PetInfoDao接口

package com.aptech.jb.epet.dao;

import java.util.List;
import com.aptech.jb.epet.entity.PetInfo;

public interface PetInfoDao {
// 根据编号查找单个宠物信息
public PetInfo load(Integer id);

// 更新宠物信息
public void update(PetInfo petInfo);

// 查找所有宠物信息
public List selectAll();

// 增加一条宠物信息
public void insertPetInfo(PetInfo petInfo);

// 验证信息
public PetInfo userValidate(String petId, String petPassword);

// 喂养宠物
public void doTraining(Integer petId, String Training);

// 根据条件查找宠物信息
public List search(PetInfo condition, String orderBy);
}



...全文
356 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
panhaichun 2010-10-29
  • 打赏
  • 举报
回复
忘了说,如果改成后一种的话,你的action里面得改成
PetInfoBizImpl的接口 petInfoBizProxy = (PetInfoBizImpl的接口) SpringUtil.getBean("petInfoBizProxy");
panhaichun 2010-10-29
  • 打赏
  • 举报
回复
把你的
<property name="target" ref="petInfoBizTarget"></property>
改成
<property name="target" ref="petInfoDao"></property>

或者
<property name="proxyInterfaces">
<value>com.aptech.jb.epet.dao.PetInfoDao</value>
</property>
改成
<property name="proxyInterfaces">
<value>PetInfoBizImpl的接口</value>
</property>
panhaichun 2010-10-29
  • 打赏
  • 举报
回复
<property name="target" ref="petInfoBizTarget"></property>

必须是

<property name="proxyInterfaces">
<value>com.aptech.jb.epet.dao.PetInfoDao</value>
</property>

的实现类才行

  • 打赏
  • 举报
回复
内容有点长 麻烦大家帮我看一下。。我弄了4个多小时了 一直找不到问题所在。。
  • 打赏
  • 举报
回复
当在ACTION中调用方法

public ActionForward doFeed(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
PetInfoDao petInfoBizProxy = (PetInfoDao) SpringUtil.getBean("petInfoBizProxy");
PetInfoForm dynaForm = (PetInfoForm) form;
petInfoBizProxy.doTraining(dynaForm.getPetInfo().getPetId(), "doFeed"); //这句报错
try {
response.sendRedirect("/epet/pet.do?operate=" + "doPetInfo");
return null;
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return null;
}


错误信息

javax.servlet.ServletException: AOP configuration seems to be invalid: tried calling method [public abstract void com.aptech.jb.epet.dao.PetInfoDao.doTraining(java.lang.Integer,java.lang.String)] on target [com.aptech.jb.epet.biz.impl.PetInfoBizImpl@145d1ef]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.aptech.jb.epet.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:30)


root cause

org.springframework.aop.AopInvocationException: AOP configuration seems to be invalid: tried calling method [public abstract void com.aptech.jb.epet.dao.PetInfoDao.doTraining(java.lang.Integer,java.lang.String)] on target [com.aptech.jb.epet.biz.impl.PetInfoBizImpl@145d1ef]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:312)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy0.doTraining(Unknown Source)
com.aptech.jb.epet.struts.action.PetAction.doFeed(PetAction.java:110)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.aptech.jb.epet.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:30)


root cause

java.lang.IllegalArgumentException: object is not an instance of declaring class
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy0.doTraining(Unknown Source)
com.aptech.jb.epet.struts.action.PetAction.doFeed(PetAction.java:110)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.aptech.jb.epet.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:30)
  • 打赏
  • 举报
回复
实现类

public class PetInfoDaoHibImpl extends BasehiberNateDao implements PetInfoDao {

public void doTraining(Integer petId, String Training) {
PetInfo petInfo = this.load(petId);
if (Training.equals("doFeed")) {
petInfo.setPetStrength(new Integer(Integer.parseInt(petInfo
.getPetStrength().toString()) + 30));
} else if (Training.equals("doStroy")) {
petInfo.setPetCute(new Integer(Integer.parseInt(petInfo
.getPetCute().toString()) + 1));
petInfo.setPetLove(new Integer(Integer.parseInt(petInfo
.getPetLove().toString()) + 3));
petInfo.setPetStrength(new Integer(Integer.parseInt(petInfo
.getPetStrength().toString()) - 5));
} else if (Training.equals("doGame")) {
petInfo.setPetCute(new Integer(Integer.parseInt(petInfo
.getPetCute().toString()) + 3));
petInfo.setPetLove(new Integer(Integer.parseInt(petInfo
.getPetLove().toString()) + 1));
petInfo.setPetStrength(new Integer(Integer.parseInt(petInfo
.getPetStrength().toString()) - 5));
}
super.update(petInfo);
}



LotteryAdvice 类

public class LotteryAdvice implements AfterReturningAdvice {

private PetInfoDao petInfoDao = null;

public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {

if (method.getName().equals("doTraining")
&& args[1].toString().equals("doFeed")) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);

Integer petId = new Integer(args[0].toString());
PetInfo petInfo = petInfoDao.load(petId);
if (Integer.parseInt(petInfo.getPetStrength().toString()) < 200) {
petInfo.setPetStrength(new Integer(200));
petInfoDao.update(petInfo);
} else {
petInfo.setPetStrength(new Integer(Integer.parseInt(petInfo
.getPetStrength().toString()) + 50));
petInfoDao.update(petInfo);
}
}
}
}

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧