Announcement Announcement Module
Collapse
No announcement yet.
Root of BeanFactory hierarchy Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Root of BeanFactory hierarchy

    I want to use spring to manage DAOs,The spring configuration file:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE beans PUBLIC
              "-//SPRING//DTD BEAN//EN"
              "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
            <bean id="txProxyTemplate" lazy-init="true"
                  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
                  <property name="transactionManager"><ref bean="transactionManager"/></property>
                  <property name="transactionAttributes">
                      <props>
                        <prop key="save*">PROPAGATION_REQUIRED</prop>
                        <prop key="remove*">PROPAGATION_REQUIRED</prop>
                        <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
                      </props>
            </property>
            </bean>
            <bean id="departManager"
    	      parent="xtProxyTemplate">
                    <property name="name"><value>override</value></property>
    		<property name="target">
    			<bean class="com.sales.service.impl.DepartManagerImpl">
    				<property name="departDAO">
    					<ref bean="departDAO"></ref>
    				</property>
    			</bean>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="save*">PROPAGATION_REQUIRED</prop>
    				<prop key="update*">PROPAGATION_REQUIRED</prop>
    				<prop key="remove*">PROPAGATION_REQUIRED</prop>
    				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    			</props>
    		</property>
    	</bean>
            <bean id="roleManager"
    	      parent="xtProxyTemplate">
    		<property name="target">
    			<bean class="com.sales.service.impl.RoleManagerImpl">
    				<property name="roleDAO">
    					<ref bean="roleDAO"></ref>
    				</property>
    			</bean>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="save*">PROPAGATION_REQUIRED</prop>
    				<prop key="update*">PROPAGATION_REQUIRED</prop>
    				<prop key="remove*">PROPAGATION_REQUIRED</prop>
    				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    			</props>
    		</property>
    	</bean>
            <bean id="userManager"
    	      parent="xtProxyTemplate">
    		<property name="target">
    			<bean class="com.sales.service.impl.UserManagerImpl">
    				<property name="userDAO">
    					<ref bean="userDAO"></ref>
    				</property>
    			</bean>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="save*">PROPAGATION_REQUIRED</prop>
    				<prop key="update*">PROPAGATION_REQUIRED</prop>
    				<prop key="remove*">PROPAGATION_REQUIRED</prop>
    				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    			</props>
    		</property>
    	</bean>
    	<bean id="menuManager"
    	      parent="xtProxyTemplate">
    		<property name="target">
    			<bean class="com.sales.service.impl.MenuManagerImpl">
    				<property name="menuDAO">
    					<ref bean="menuDAO"></ref>
    				</property>
    				<property name="permissionDAO">
    					<ref bean="permissionDAO"></ref>
    				</property>
    			</bean>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="save*">PROPAGATION_REQUIRED</prop>
    				<prop key="update*">PROPAGATION_REQUIRED</prop>
    				<prop key="remove*">PROPAGATION_REQUIRED</prop>
    				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    			</props>
    		</property>
    	</bean>
    </beans>
    my test class:

    Code:
    package test.sales.service.impl;
    
    import junit.framework.*;
    import com.sales.service.*;
    import com.sales.service.impl.*;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.beans.factory.BeanFactory;
    
    public class _TestMenuManagerImpl
        extends TestCase &#123;
      private MenuManager menuService = null;
      private ApplicationContext ctx = null;
      private BeanFactory app;
    
      protected void setUp&#40;&#41; throws Exception &#123;
        super.setUp&#40;&#41;;
        String&#91;&#93; paths = &#123;
            "/ApplicationContext-hibernate.xml", "/ApplicationContext-service.xml"&#125;;
        ctx = new ClassPathXmlApplicationContext&#40;paths&#41;;
        app = &#40;BeanFactory&#41;ctx;
        menuService = &#40;MenuManager&#41;app.getBean&#40;"menuManager"&#41;;
      &#125;
    
      protected void tearDown&#40;&#41; throws Exception &#123;
        menuService = null;
        super.tearDown&#40;&#41;;
      &#125;
    
      public void testSaveMenu&#40;&#41; &#123;
        long parentid = 0;
        String menuName = "menu2";
        String menuLink = "http&#58;//localhost&#58;8080";
        boolean flag = menuService.saveMenu&#40;parentid, menuName,
            menuLink&#41;;
        if&#40;flag&#41;
          System.out.println&#40;"save success"&#41;;
        else
          System.out.println&#40;"save failure"&#41;;
      &#125;
    
    &#125;
    Running this demo will output to the console profing the expected result:

    Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionException&#58; No bean named 'xtProxyTemplate' is defined&#58; org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans &#91;dataSource,sessionFactory,transactionManager,menuDAO,departDAO,roleDAO,permissionDAO,userDAO,txProxyTemplate,departManager,roleManager,userManager,menuManager&#93;; Root of BeanFactory hierarchy
    
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition&#40;DefaultListableBeanFactory.java&#58;291&#41;
    
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition&#40;AbstractBeanFactory.java&#58;554&#41;
    
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition&#40;AbstractBeanFactory.java&#58;579&#41;
    
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition&#40;AbstractBeanFactory.java&#58;554&#41;
    
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.isBeanDefinitionTypeMatch&#40;DefaultListableBeanFactory.java&#58;124&#41;
    
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinitionNames&#40;DefaultListableBeanFactory.java&#58;106&#41;
    
    	at org.springframework.context.support.AbstractApplicationContext.getBeanDefinitionNames&#40;AbstractApplicationContext.java&#58;464&#41;
    
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors&#40;AbstractApplicationContext.java&#58;307&#41;
    
    	at org.springframework.context.support.AbstractApplicationContext.refresh&#40;AbstractApplicationContext.java&#58;266&#41;
    
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>&#40;ClassPathXmlApplicationContext.java&#58;80&#41;
    
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>&#40;ClassPathXmlApplicationContext.java&#58;65&#41;
    
    	at test.sales.service.impl._TestMenuManagerImpl.setUp&#40;_TestMenuManagerImpl.java&#58;20&#41;
    
    	at junit.framework.TestCase.runBare&#40;TestCase.java&#58;125&#41;
    
    	at junit.framework.TestResult$1.protect&#40;TestResult.java&#58;106&#41;
    
    	at junit.framework.TestResult.runProtected&#40;TestResult.java&#58;124&#41;
    
    	at junit.framework.TestResult.run&#40;TestResult.java&#58;109&#41;
    
    	at junit.framework.TestCase.run&#40;TestCase.java&#58;118&#41;
    
    	at junit.framework.TestSuite.runTest&#40;TestSuite.java&#58;208&#41;
    
    	at junit.framework.TestSuite.run&#40;TestSuite.java&#58;203&#41;
    
    	at com.borland.jbuilder.unittest.JBTestRunner.a&#40;Unknown Source&#41;
    
    	at com.borland.jbuilder.unittest.JBTestRunner.initiateTest&#40;Unknown Source&#41;
    
    	at com.borland.jbuilder.unittest.JBTestRunner.main&#40;Unknown Source&#41;
    please help me!

  • #2
    You have a simple typo. The parent values should actually be "txProxyTemplate", like the parent definition.

    The error message is completely correct. Note that it shows a list of defined beans, that includes the correctly spelled version of the parent bean.

    Comment


    • #3
      thanks

      thank you for your help!
      but i have a new problem:
      my daos can not rollback when one of them insert failure.
      Is spring can manage daos on service layer?

      Comment


      • #4
        my table struct:
        Code:
        create table menu&#40;
          id bigint not null auto_increment,
          parentid bigint not null,
          menuname varchar&#40;50&#41; unique,
          menulink varchar&#40;100&#41;,
          primary key&#40;id&#41;
        &#41;type=innodb;
        my dao method:
        Code:
        public boolean saveMenu&#40;Menu menu&#41; &#123;
            try&#123;
              &#91;b&#93;getHibernateTemplate&#40;&#41;.save&#40;menu&#41;;&#91;/b&#93;
              return true;
            &#125;
            catch &#123;
              return false;
            &#125;
          &#125;
        but method getHibernateTemplate().save() can't capture insert exception?
        why?

        Comment

        Working...
        X