Topic: petclinic中的疑问.

  Print this page

1.petclinic中的疑问. Copy to clipboard
Posted by: wangfeng
Posted on: 2004-09-15 20:25

在spring 1.1中的petclinic中,我对这个有些不理解.

petclinic-servlet.xml中:
  <bean id="findOwnersForm" class="org.springframework.samples.petclinic.web.FindOwnersForm">
    <property name="formView"><value>findOwnersForm</value></property>
    <property name="selectView"><value>selectOwnerView</value></property>
    <property name="successView"><value>ownerRedirect</value></property>
    <property name="clinic"><ref bean="clinic"/></property>
  </bean>


这个里面使用clinic对象,他对clinic属性赋值. 这个属性在FindOwnersForm类中
没有定义,而是从其父类AbstractClinicForm中继承来的.clinic是这样定义的:
  private Clinic clinic;


在petclinic-servlet.xml中不存在clinic bean 的定义. 我只是从
applicationContext-hibernate.xml中找到如下声明:
  <!--
    - A parent bean definition which is a base definition for transaction proxies.
    - It is markes as abstract, since it is never supposed to be instantiated itself.
    - We set shared transaction attributes here, following our naming patterns.
    - The attributes can still be overridden in child bean definitions.
    -->
  <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
      abstract="true">
    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="transactionAttributes">
      <props>
        <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="store*">PROPAGATION_REQUIRED</prop>
      </props>
    </property>
  </bean>

  <!--
   - Petclinic primary business object: Hibernate implementation, as an inner bean
    - wrapped by an outer transactional proxy. The two bean definitions could have been
    - separate, but this is cleaner as ther is no need to ever access the unwrapped object.
-->
  <bean id="clinic" parent="baseTransactionProxy">
    <property name="target">
      <bean class="org.springframework.samples.petclinic.hibernate.HibernateClinic">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
      </bean>
    </property>
  </bean>



可以看出 clinic bean 是从 baseTransactionProxy bean继承来的
(不是类继承,应该是继承属性,方法的说明吧),因此他应该是TransactionProxyFactoryBean
类型的, 这样把clinic传递过去怎么可以呢?? clinic bean 对应的类应该是实现了
Clinic接口才可以啊.. 有搞清楚的吗?

在Spring 1.0 中的 petclinic 中的设定和这个也差不多, 但是问题都一样..

到底是怎么回事呢?

2.Re:petclinic中的疑问. [Re: wangfeng] Copy to clipboard
Posted by: floater
Posted on: 2004-09-16 00:20

the hirachy structure of beans are new to 1.1.

clinic is transaction proxy factory, to build the bean in the target, which is HibernateClinic.

3.Re:petclinic中的疑问. [Re: wangfeng] Copy to clipboard
Posted by: wangfeng
Posted on: 2004-09-16 00:49

我知道这个特性,还专门看了源代码,
您还没有弄明白我说的意思..

clinic bean 到底是什么类型的呢?

根据在applicationContext-hibernate.xml中的定义,
他的parent 是baseTransactionProxy,
而 baseTransactionProxy 是TransactionProxyFactoryBean类型的吧.
那么他就是TransactionProxyFactoryBean 类型的了.
那样的话,就不可以传递给 那个 findOwnerForm bean了...

我的理解什么地方有错误?

4.Re:petclinic中的疑问. [Re: wangfeng] Copy to clipboard
Posted by: floater
Posted on: 2004-09-16 01:29

it's HibernateClinic, in type.

5.Re:petclinic中的疑问. [Re: wangfeng] Copy to clipboard
Posted by: wangfeng
Posted on: 2004-09-16 08:20

在spring 1.0 的 petclinic 中,没有使用这个新特征, 上面的东西是这样写的:
  <!-- Transactional proxy for the Petclinic primary business object -->
<bean id="clinic" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="target"><ref local="clinicTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<!-- Petclinic primary business object: Hibernate implementation -->
<bean id="clinicTarget" class="org.springframework.samples.petclinic.hibernate.HibernateClinic">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>


从类型上讲, clinic应该是TransactionProxyFactoryBean 吧.. 但是怎么还是那样使用呢?

TransactionProxyFactoryBean 是一个FactoryBean ,如果想生成HibernateClinic 类型的东西,在这里不使用构造函数的方法, 但应该声明响应的factory method啊.

一个理解不知道对不对..
这个bean定义里面声明了一个target属性,设置这个后, 就像声明了factory method一样. 返回的就是TransactionProxyFactoryBean 利用CGLIB 对他进行包装的类对应的对象了..

6.Re:petclinic中的疑问. [Re: wangfeng] Copy to clipboard
Posted by: wes109
Posted on: 2004-09-16 08:47

TransactionProxyFactoryBean

看到了吗?Proxy

它对请求进行拦截,加入相应的事物处理后,转发给
org.springframework.samples.petclinic.hibernate.HibernateClinic

HibernateClinic才是真正的clinic

7.Re:petclinic中的疑问. [Re: wangfeng] Copy to clipboard
Posted by: wangfeng
Posted on: 2004-09-16 08:52

呵呵,应该是这样理解..
但是应该给点提示吧,在定义中应该声明一下吧.
你看这里.
<bean id="exampleBean"
class="examples.ExampleBean2"
factory-method="createInstance"/>

可以让人容易的明白,但是例子中的就不那么容易了..

8.Re:petclinic中的疑问. [Re: wangfeng] Copy to clipboard
Posted by: floater
Posted on: 2004-09-16 23:29

Please read the spring doc on transactions, it will help to understand this. Yes, it's a little bit twisted, but you get free transactions, Tounge.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923