admin管理员组

文章数量:1530361

使用aop的时候报一个这样的错误:

org.springframework.beans.factory.support.DefaultListableBeanFactory@17d5d2a: defining beans [aopMethod,personService,personServiceImpl,youBean]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'youBean' defined in class path resource [interceptor_part.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0 implementing aop.test.interceptor.MyBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [aop.test.interceptor.MyBeanImpl] for property 'myBean'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0 implementing aop.test.interceptor.MyBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [aop.test.interceptor.MyBeanImpl] for property 'myBean': no matching editors or conversion strategy found
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at aop.test.interceptor.TestMain.main(TestMain.java:8)
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0 implementing aop.test.interceptor.MyBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [aop.test.interceptor.MyBeanImpl] for property 'myBean'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0 implementing aop.test.interceptor.MyBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [aop.test.interceptor.MyBeanImpl] for property 'myBean': no matching editors or conversion strategy found
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:391)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
	... 14 more
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0 implementing aop.test.interceptor.MyBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [aop.test.interceptor.MyBeanImpl] for property 'myBean': no matching editors or conversion strategy found
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
	... 18 more
代码贴出来:

MyBean代码:

package aop.test.interceptor;

public interface MyBean {
	public void testHello(String dd);
}
MyBeanImpl代码:

package aop.test.interceptor;

public class MyBeanImpl implements MyBean {

	public void testHello(String dd) {
		System.out.println("dddddd");
	}

}
YouBean代码

package aop.test.interceptor;

public interface YouBean {

	public void testFF();
}
YouBeanImpl代码

package aop.test.interceptor;


public class YouBeanImpl implements YouBean {

	protected MyBeanImpl myBean = null;

	public MyBeanImpl getMyBean() {
		return myBean;
	}

	public void setMyBean(MyBeanImpl myBean) {
		this.myBean = myBean;
	}

	public void testFF() {
		myBean.testHello("ddd");
	}

}
配置文件interceptor.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework/dtd/spring-beans.dtd">

<beans >
   
    
    <bean id="aopMethod" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
      <property name="advice">
         <bean class="aop.test.interceptor.MethodBefore"></bean>
      </property>
      <property name="mappedName">
          <value>*</value>
      </property>
    </bean>

    <bean id="personService"
   class="org.springframework.aop.framework.ProxyFactoryBean">
   <property name="target">
    <ref bean="personServiceImpl" />
   </property>
   <property name="interceptorNames">
    <list>
     <value>aopMethod</value>
    </list>
   </property>
	</bean>
     <bean autowire="default"   lazy-init="default" id="personServiceImpl" class="aop.test.interceptor.MyBeanImpl"></bean>
    <!--  <import resource="classpath:interceptor_part.xml"/>-->
     <bean id="youBean" class="aop.test.interceptor.YouBeanImpl">
        <property name="myBean" ref="personService"></property>
     </bean>
   
</beans>  
测试类:TestMain
package aop.test.interceptor;

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

public class TestMain {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"interceptor.xml");
		// 1.ClassA使用事务控制,ClassB不使用事务控制 如果B调用A B发生异常,之前调用A的事务仍然提交
		YouBean services = (YouBean) context.getBean("youBean");
		services.testFF();
	}

}


实际上这行提示已经非常明显:

[$Proxy0 implementing aop.test.interceptor.MyBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] 

to required type [aop.test.interceptor.MyBeanImpl] for property 'myBean': no matching editors or conversion strategy found

 

spring实例化youBean属性myBean的时候,声明的类型是一个实现类MyBeanImpl ,由于spring是按照接口去找对应的实现的,而MyBeanImpl 类
整好实现了MyBean接口,所以找到了类型为MyBean;这样就导致youBean的属性myBean类型MyBeanImpl与找到的接口类型MyBean对不上,所以就报了no matching editors or conversion strategy found
    原因找到了,解决的方法也很简单:
 方法一、就是将youBean中的属性myBean类型MyBeanImpl改为MyBean,因为这样也是符合面向接口的规范的。或者我就是要在youBean中的属性myBean类型MyBeanImpl就是将youBean中的属性myBean类型MyBeanImpl改为MyBean,因为这样也是符合面向接口的规范的。
方法二、你就是要在youBean中声明属性myBean类型为MyBeanImpl,那么就不要MyBeanImpl去实现MyBean接口了

后记:有人说在配置文件中加入<aop:config proxy-target-class="true"></aop:config>的方法,我没试成功,
感觉那样做也不妥帖,因为你的代码结构本来就是不合理的,而你还要在错误的代码上走下去。




本文标签: 拦截器typePropertyaoprequired