`
phpxiaoxin
  • 浏览: 251168 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring proxyTargetClass 动态代理

    博客分类:
  • java
阅读更多

今天做两个项目的集成,主项目单独跑没问题,子项目单独跑没问题,集成到一起就挂了。报错信息为:

PropertyAccessException 1: org.springframework.beans.TypeMismatchException: 
Failed to convert property value of type [$Proxy121] to required type 
[com.**.facilities.group.service.BaseFacilitiesGroupService] 
for property 'baseFacilitiesGroupService'; nested exception is java.lang.IllegalArgumentException: 
Cannot convert value of type [$Proxy121] to required type 
[com.**.facilities.group.service.BaseFacilitiesGroupService] 
for property 'baseFacilitiesGroupService': no matching editors or conversion strategy found

org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'baseFacilitiesGroupForVoService' defined in ServletContext resource [/WE
B-INF/classes/spring/facilities.xml]: Cannot resolve reference to bea
n 'baseFacilitiesService' while setting bean property 'baseFacilitiesService'; n
ested exception is org.springframework.beans.factory.BeanCreationException: Erro
r creating bean with name 'baseFacilitiesService' defined in ServletContext reso
urce [/WEB-INF/classes/spring/facilities.xml]: Cannot resolve referen
ce to bean 'baseFacilitiesServiceTarget' while setting bean property 'target'; n
ested exception is org.springframework.beans.factory.BeanCreationException: Erro
r creating bean with name 'baseFacilitiesServiceTarget' defined in ServletContex
t resource [/WEB-INF/spring/facilities.xml]: Error setting pr
operty values; nested exception is org.springframework.beans.PropertyBatchUpdate
Exception; nested PropertyAccessExceptions (1) are:

 从报错信息来看,是类注入的时候,类型不匹配。再看一下,service是经过了一层事务代理的。看了一下两个项目的配置,完全没有问题。配置文件如下:

 

    <bean id="baseFacilitiesGroupService" parent="baseTransactionProxy">
        <property name="target" ref="baseFacilitiesGroupServiceTarget"/>
    </bean>

    <bean id="baseFacilitiesGroupServiceTarget"
          class="com.**.BaseFacilitiesGroupService">
        <property name="baseFacilitiesGroupDao" ref="baseFacilitiesGroupDao"/>
    </bean>

	<!--事务管理代理-->
	<bean id="baseTransactionProxy"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		abstract="true">
		<property name="transactionManager" ref="transactionManager" />
		<property name="proxyTargetClass">
			<value>true</value>
		</property>
		<property name="transactionAttributes">
			<props>
				<!--<prop key="**">PROPAGATION_REQUIRED,-Exception</prop>
				-->
				<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
			</props>
		</property>
	</bean>

 

从网上搜索错误说proxyTargetClass需要配置为true,我们也配置了。

参考:http://blog.chinaunix.net/u/20045/showart_1019030.html

 

后来听说主项目没有使用我们的配置方法。而是使用了:

 

 

<bean id="chainService" 
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>
        <property name="target">
            <ref bean="chainServiceTarget"/>
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="create*">
                    PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED
                </prop>
        </property>
    </bean>

 根据这种方式修改后,还是不work,后来想到,感觉是不是应该吧proxyTargetClass=true也配置上,加上之后,果然work了。

   <bean id="chainService" 
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="proxyTargetClass">
   <value>true</value>
  </property>
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>
        <property name="target">
            <ref bean="chainServiceTarget"/>
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="create*">
                    PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED
                </prop>
            </props>
        </property>
    </bean>

 

再后来看到了如下的文章:ProxyFactoryBean什么时候需要配置 proxyTargetClass

 

说是spring动态代理使用cglib的时候会通过类所实现的接口动态代理生成类。这下去看那个报错的类,他实现了接口,但是其他地方使用的时候却是直接使用了这个类。所以动态代理生成的类,是会产生类型不匹配的问题。所以问题找到。

 

但是仍然有遗留的问题:

1.为什么子项目单独是可以运行的?

2.为什么我们最后配置完之后,其实就是将parent取掉,然后把代理的配置移到了本身上面,这两种方式我认为是没有区别的。为啥一个可以work,一个不行。

 

有谁可以解答一下,谢谢。

 

分享到:
评论
2 楼 phpxiaoxin 2011-01-23  
z.raul 写道
建议楼主单独运行一下主项目,估计主项目的配置就是错误的。大项目使用代理要小心。

不错,前几天发现问题了,主项目中,有人配置了一个id一摸一样的baseTransactionProxy
而且运行debug发现spring使用的不是我们想想中的那个id。
而那个多出来的配置的事务也有问题。
1 楼 z.raul 2011-01-22  
建议楼主单独运行一下主项目,估计主项目的配置就是错误的。大项目使用代理要小心。

相关推荐

    spring之AOP(动态代理)

    然而,如果需要更精细的控制,可以通过`@EnableAspectJAutoProxy`注解开启基于AspectJ的自动代理支持,或者通过`proxyTargetClass`属性来强制使用CGLIB代理。 总结一下,Spring的AOP机制通过JDK动态代理和CGLIB动态...

    spring 的动态代理详解

    ### Spring的动态代理详解 #### 引言 在软件工程领域,代理模式是一种常见的设计模式,主要用于增强对象的功能,而不改变其原有的结构。Spring框架利用动态代理技术为Bean提供了横切关注点(如事务管理、日志记录...

    spring jdk动态代理

    Spring AOP允许我们通过代理来实现横切关注点,如日志、事务管理等,而JDK动态代理则是Spring AOP实现的一种方式。本文将深入探讨Spring如何利用JDK动态代理技术来实现这一功能,并通过实例解析其底层实现。 首先,...

    springAOP配置动态代理实现

    可以通过配置`proxyTargetClass="true"`来强制使用CGLIB。 4. **代理对象的获取**:通常,Spring容器会自动创建并管理代理对象。在需要调用目标方法的地方,使用Spring注入的bean,实际上就是在操作代理对象。 综...

    Java动态代理(Spring Aop原理)

    Java动态代理是Java提供的一种在运行时创建代理对象的技术,它是通过实现InvocationHandler接口和使用Proxy类来实现的。在Spring框架中,AOP(面向切面编程)就是基于Java动态代理来完成的,用于实现横切关注点,如...

    java Proxy 动态代理

    在Java中,动态代理主要通过`java.lang.reflect.Proxy`类和`java.lang.reflect.InvocationHandler`接口来实现。Proxy类是用于创建一个代理对象,而InvocationHandler接口则定义了代理对象调用方法时的行为。 1. **...

    Spring框架+SpringAOP动态代理

    Spring内部通过`java.lang.reflect.Proxy`类来创建动态代理对象。为了创建一个代理对象,开发者需要实现`InvocationHandler`接口,并重写`invoke`方法。该方法会在代理对象调用被代理方法时被触发。下面是一个简单的...

    spring多个动态代理导致循环依赖报错问题解决

    3. **调整代理模式**:对于CGLIB代理,可以在配置中设置`proxyTargetClass=true`,强制Spring使用CGLIB,即使目标类实现了接口。由于CGLIB创建的是目标类的子类,所以不会导致循环依赖。但是,这可能会引入额外的...

    在spring中获取代理对象代理的目标对象工具类

    - `unwrapIfNecessary(Object proxy, Class&lt;?&gt; requiredType)`:这个方法会检查代理对象是否需要被解包,如果`proxy`的类型与`requiredType`匹配,那么它就直接返回`proxy`;否则,它会调用`getTarget(proxy)`来获取...

    Spring如何基于Proxy及cglib实现动态代理

    Spring支持两种主要的动态代理方式:Java Proxy和cglib。这两种方法都可以用来增强或拦截目标对象的方法调用,实现如AOP(面向切面编程)的功能。 首先,我们来看Java Proxy。Java Proxy是Java内置的动态代理机制,...

    动态代理接口并注册到spring容器

    在本案例中,我们将探讨如何实现一个动态代理接口并将其注册到Spring容器,以便于通过@Autowired注解或其他方式在Spring应用中使用。 首先,我们需要定义一个接口,例如`MyService`,这个接口包含了我们需要代理的...

    spring_aop之JDK的动态代理

    在Spring AOP中,当目标对象实现了至少一个接口时,Spring会使用JDK的动态代理来创建代理对象。这是因为JDK的动态代理只适用于实现了接口的类,它通过生成一个与目标类接口相同的新类来实现代理。这个新类在运行时被...

    Spring_0300_JDKProxy

    标题"Spring_0300_JDKProxy"暗示我们将讨论Spring如何使用JDK的Proxy类来实现动态代理。在Spring中,`org.springframework.aop.framework.ProxyFactoryBean`或`org.springframework.aop.framework.ProxyFactory`可以...

    spring-demo16-JDK动态代理.zip

    总结来说,"spring-demo16-JDK动态代理.zip"可能是一个包含Spring AOP与JDK动态代理结合使用的示例项目,展示了如何通过动态代理在运行时增强对象的行为。这个例子可以帮助开发者理解如何在实际应用中利用JDK动态...

    反射实现 AOP 动态代理模式(Spring AOP 的实现 原理) - Java 例子 -

    Spring框架是Java中实现AOP的一个流行工具,它通过动态代理机制实现了这一功能。本文将深入探讨Spring AOP的实现原理,以及如何使用反射来实现动态代理模式。 首先,我们需要了解AOP的基本概念。AOP的核心思想是切...

    简单模拟spring cglib代理

    我们模拟Spring CGLIB代理的方式,通过Enhancer来创建TargetClass的代理实例。我们定义了一个MethodInterceptor,在该拦截器中,在目标方法调用前后输出了信息,并且调用了目标方法的代理实现。 需要注意的是,上述...

    Spring Boot 使用 ProxyServlet 代理并统一响应

    代理(Proxy)是指客户端通过一个中间服务器间接访问目标服务器,而反向代理(Reverse Proxy)则是指客户端看似直接与服务器交互,但实际上请求被转发给了另一个服务器。在Spring Boot中,我们可以利用`ProxyServlet...

    输出JDK和CGLib动态代理产生的class文件.zip

    - target/classes:编译后的class文件,包括目标接口和实现类的class文件,以及由Proxy生成的代理类class文件 - 测试代码:展示如何使用Proxy创建代理对象并调用方法 2. CGLib代理项目: - src/main/java:包含...

    动态代理的方式注册到spring容器

    在Spring框架中,动态代理是一种常见且强大的技术,它允许我们在运行时创建对象的代理,以便在调用方法时添加额外的功能,如日志、事务管理、缓存等。本篇将深入探讨如何通过动态代理将对象注册到Spring容器,并实现...

    spring-demo20-CGLIB动态代理.zip

    然后,在Spring配置文件中启用CGLIB代理,可以设置`proxy-target-class="true"`属性: ```xml &lt;aop:config proxy-target-class="true"&gt; &lt;!-- 配置切面 --&gt; ... ``` 接下来,创建一个需要代理的目标类,例如`...

Global site tag (gtag.js) - Google Analytics