日期:2014-05-20  浏览次数:20865 次

Spring aop最基础advise
package aop;

public interface BasicInterface {
public void SayHelo(String str);

}

package aop;

public class BasicImpl implements BasicInterface{

public void SayHelo(String str) {
// TODO Auto-generated method stub
System.out.println("你好:"+str);

}

package aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class AopAdvice implements MethodBeforeAdvice {

public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("执行前。。。。。。");

}
}



package aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
public static void main(String[] args) {
ApplicationContext  context=new ClassPathXmlApplicationContext("applicationContext.xml");
BasicInterface ehello=(BasicInterface)context.getBean("Proxy");
ehello.SayHelo("访问");
}

}


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

<!-- 建立目标对象实例 -->
<bean id="Basic"  class="aop.BasicImpl"/>
<!--建立advice实例  -->
<bean id="myAdvice"   class="aop.AopAdvice"/>
<!--建立代理对象   -->
<bean id="Proxy"  class="org.springframework.aop.framework.ProxyFactoryBean">
    <!--设置代理的接口   -->
    <property name="proxyInterfaces">
        <value>BasicInterface</value>
    </property>
    <!--设定目标对象实例   -->
    <property name="target">
       <ref  bean="Basic"/> 
    </property>
     <!--设定advice实例 -->
     <property name="interceptorNames">
        <list>
           <value>myAdvice</value>
        </list>
     </property>
    </bean>
</beans>



为什么会报错:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'Proxy' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Class[]' for property 'proxyInterfaces'; nested exception is java.lang.IllegalArgumentException: Cannot find class [BasicInterface]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:545)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)at aop.Test.main(Test.java:8)
------解决方案--------------------
proxyInterfaces属性的值得是全路径的:aop.BasicInterface