I want to use spring to manage DAOs,The spring configuration file:
my test class:Code:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://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>
Running this demo will output to the console profing the expected result: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 { private MenuManager menuService = null; private ApplicationContext ctx = null; private BeanFactory app; protected void setUp() throws Exception { super.setUp(); String[] paths = { "/ApplicationContext-hibernate.xml", "/ApplicationContext-service.xml"}; ctx = new ClassPathXmlApplicationContext(paths); app = (BeanFactory)ctx; menuService = (MenuManager)app.getBean("menuManager"); } protected void tearDown() throws Exception { menuService = null; super.tearDown(); } public void testSaveMenu() { long parentid = 0; String menuName = "menu2"; String menuLink = "http://localhost:8080"; boolean flag = menuService.saveMenu(parentid, menuName, menuLink); if(flag) System.out.println("save success"); else System.out.println("save failure"); } }
please help me!Code:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xtProxyTemplate' is defined: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,sessionFactory,transactionManager,menuDAO,departDAO,roleDAO,permissionDAO,userDAO,txProxyTemplate,departManager,roleManager,userManager,menuManager]; Root of BeanFactory hierarchy at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:554) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:579) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:554) at org.springframework.beans.factory.support.DefaultListableBeanFactory.isBeanDefinitionTypeMatch(DefaultListableBeanFactory.java:124) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinitionNames(DefaultListableBeanFactory.java:106) at org.springframework.context.support.AbstractApplicationContext.getBeanDefinitionNames(AbstractApplicationContext.java:464) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:307) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:266) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:80) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65) at test.sales.service.impl._TestMenuManagerImpl.setUp(_TestMenuManagerImpl.java:20) at junit.framework.TestCase.runBare(TestCase.java:125) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at com.borland.jbuilder.unittest.JBTestRunner.a(Unknown Source) at com.borland.jbuilder.unittest.JBTestRunner.initiateTest(Unknown Source) at com.borland.jbuilder.unittest.JBTestRunner.main(Unknown Source)


Reply With Quote