日期:2014-05-18  浏览次数:20782 次

关于Spring 基于借口的AOP报错
@Before("execution(* com.spring.Master.*(..))")
Master是一个类,实现了MyInterface这个借口,按照上面这样写不报错,一切正常
但是把Master换成MyInterface就报错了,,求各位大神看下问题在哪

主要代码如下
public interface WorkInterface {
public void dowork();
}


@Component("master")
public class Master implements WorkInterface{

@Override
public void dowork() {
// TODO Auto-generated method stub
System.out.println("Working after meal...");
}

}


public class DynamicProxyFactory{
@Resource
private Servant servant;

// @Before("execution(* com.spring.WorkInterface.*(..))")
@Before("execution(* com.spring.Master.*(..))")
public void before(){
servant.dowork();
}
}


public class Test {
public static void main(String[] args) {
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
WorkInterface master=(WorkInterface)factory.getBean("master");
master.dowork();
}
}


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" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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/cache 
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


    <!-- 启用组件扫描 -->
<context:component-scan base-package="com.spring"/>
<!-- 启用注解注入 -->
<context:annotation-config/>
<!-- 启用AspectJ的-->
<aop:aspectj-autoproxy />
</beans>

------解决方案--------------------
看错误日志是: 注入 Servant 的时候生成的代理对象和Servant类型不匹配造成的错误。用servant的接口试试
------解决方案--------------------
将这里的
public?class?DynamicProxyFactory{
????@Resource
????private?Servant?servant;

Servant servant; 改成 WorkInterface servant;

其他有 @Resource 及@Autowired&nbs