随笔-193  评论-715  文章-1  trackbacks-0
本Blog所有内容不得随意转载,版权属于作者所有。如需转载请与作者联系( fastzch@163.com )。
未经许可的转载,本人保留一切法律权益。


我曾经在我的文章《搭建SSH时的思考和遇到的几个问题》中写过,我为何要使用AutowiringRequestProcessor来作为Struts与Spring集成时的解决方案,如果不知道这个Processor的作用和不了解用意的朋友,可以再看看那篇文章。

今天要说的问题是在使用这个Processor中的一个问题。在那篇文章中,我曾提到过,支持byName和byType两种方式,默认是byType方式,但有时候使用byType可能不行,比如:
我想使用Spring自带的声明式事务,这东东很是不错,不用我操心事务的处理,相信大家也都比较喜欢,比如我的事务定义 如下:
    <bean id="TransactionAttributeSource"
        class
="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
        
<property name="properties">
            
<props>
                
<prop key="add*">PROPAGATION_NESTED</prop>
                
<prop key="delete*">PROPAGATION_NESTED</prop>
            
</props>
        
</property>
    
</bean>
那么我的Service类就应该定义成这样:
<bean id="groupInfoService"
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        
<property name="proxyInterfaces">
            
<list>
                
<value>
                    com.eric.ocgweb.biz.vpmn.IGroupInfoService
                
</value>
            
</list>
        
</property>
        
<property name="target">
            
<ref bean="groupInfoServiceImpl">
        
</property>
        
<property name="transactionManager">
            
<ref bean="TransactionManager" />
        
</property>
        
<property name="transactionAttributeSource">
            
<ref bean="TransactionAttributeSource" />
        
</property>
    
</bean>
然后我还得定义一个groupInfoServiceImpl的Bean,在此省略。

根据Spring的DOC所说,如果想使用byName的方式来自动装载,通过如下配置来实现,修改web.xml中对于Struts所用的ActionServlet的定义:
<servlet>
        
<servlet-name>action</servlet-name>
        
<servlet-class>
            org.apache.struts.action.ActionServlet
        
</servlet-class>
        
<init-param>
            
<param-name>config</param-name>
            
<param-value>/WEB-INF/struts-config.xml</param-value>
        
</init-param>
        
<init-param>
            
<param-name>autowire</param-name>
            
<param-value>byName</param-value>
        
</init-param>
        
<init-param>
            
<param-name>dependencyCheck</param-name>
            
<param-value>true</param-value>
        
</init-param>
        
<init-param>
            
<param-name>debug</param-name>
            
<param-value>3</param-value>
        
</init-param>
        
<init-param>
            
<param-name>detail</param-name>
            
<param-value>3</param-value>
        
</init-param>
        
<load-on-startup>0</load-on-startup>
    
</servlet>

但是在我这样修改之后,当应用程序在运行的时候,会提示说找到了两个Bean,一个是groupInfoService,另一个是groupInfoServiceImpl,似乎并没有byName。

所以我不得不采用下面这种形式来定义Spring中的Bean:
<bean id="groupInfoService"
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        
<property name="proxyInterfaces">
            
<list>
                
<value>
                    com.eric.ocgweb.biz.vpmn.IGroupInfoService
                
</value>
            
</list>
        
</property>
        
<property name="target">
            
<bean class="com.eric.ocgweb.biz.vpmn.GroupInfoService">
                
<property name="groupInfoLogic">
                    
<ref bean="GroupInfoLogic" />
                
</property>
            
</bean>
        
</property>
        
<property name="transactionManager">
            
<ref bean="TransactionManager" />
        
</property>
        
<property name="transactionAttributeSource">
            
<ref bean="TransactionAttributeSource" />
        
</property>
    
</bean>
这样,自动装载时就只能找到一个Bean的定义了,就不会报不惟一的错了。
posted on 2008-02-24 00:20 Robin's Programming World 阅读(2332) 评论(1)  编辑  收藏 所属分类: Java

评论:
# re: Struts与Spring集成时使用AutowiringRequestProcessor产生的问题及解法 2009-02-05 13:45 | ss
似乎是spring.autowire来声明byName方式  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: