spring中的parent属性我看别人说是继承关系,但我不清楚它是怎么继承的,
比如在写service时候每个service要加入事务管理然后加入parent属性,
但service并没有明显的继承proxy呀,请问是怎么继承的??使用的是什么技术?
如果我的service继承了某个类 如下面代码所示
Test bean = (Test)new ResourceService(); //ResourceService继承Test
Test bean = (Test)a.getBean("resourceService");//这个是经过spring IOC得到的配置文件如下所示
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
value="classpath:hibernate.cfg.xml">
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="basicTxProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="edit*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="disPlay*">PROPAGATION_REQUIRES_NEW</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="steelBasicDaoImpl" class="com.steel.hibernate.dao.impl.SteelBasicDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="resourceService" parent="basicTxProxy">
<property name="target">
<bean class="com.steel.resource.service.ResourceService">
<property name="steelBasicDaoImpl" ref="steelBasicDaoImpl" />
<property name="userService" ref="userService" />
</bean>
</property>
</bean>
结果打印一个语句 System.out.println(bean.getClass().getSuperclass());
new出来的显示正确,是Test,但从spring拿到的却显示转型异常,
百思不得其解,望大家能解惑
spring里的这个继承关系的意思和java类的继承不是一个概念的,
spring里的这个继承关系就指继承那个bean的配置属性,不是类的继承,懂么?
上面的配置文件就相当于
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="edit*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="disPlay*">PROPAGATION_REQUIRES_NEW</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
你用new的方式,那当然是正确的类型,因为你的ResourceService是Test的子类嘛
自然可以向上转型的
但被spring动态代理了之后,就不能这想强制转换类型了
你先去理解一下java的动态代理吧
http://www.cnblogs.com/kongxx/archive/2005/08/08/209749.html
这句话还真是奇怪,难道你的service同时还是个proxy,还能生成一个新的service?
parent的方法不是这样用的,很简单。
如果你定义了一个service,并给它注入了某些东西。
那么如果有个新的serivce继承了刚才的那个service,在配置文件中设置了parent的话,那么这个service就会自己继承父类service中注入的内容