黑马程序员技术交流社区

标题: 关于Spring框架 [打印本页]

作者: 肖亚光    时间: 2013-1-15 16:24
标题: 关于Spring框架
本帖最后由 可乐咖啡厅 于 2013-1-15 16:37 编辑

这是xml文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.            http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.            <bean id="personService" class="cn.itheima.service.impl.PersonServiceBean"/>
  7.            <bean id="personService2" class="cn.itheima.service.impl.PersonServiceBeanFactory"
  8.            factory-method="PersonServiceBean"/>
  9. </beans>
复制代码
一开始只有一个<bean>的时候运行是正确的
但是现在多加了一个 就运行不过去了
  1. import org.junit.BeforeClass;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;

  5. import cn.itheima.service.PersonService;

  6. public class SpringTest {

  7.         @BeforeClass
  8.         public static void setUpBeforeClass() throws Exception {
  9.         }
  10.         @Test public void instanceSprint(){
  11.                 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  12. //                ItHeimaClassPathXmlApplicationContext ctx = new ItHeimaClassPathXmlApplicationContext("beans.xml");
  13.                 PersonService personService = (PersonService)ctx.getBean("personService");
  14. //                PersonService personService2 = (PersonService)ctx.getBean("personService2");
  15.                 personService.save();
  16. //                personService2.save();
  17.         }
  18. }
复制代码
就这样即使我注掉要实例第二个。照样不能运行。
也就是我的xml文件多加了一个<bean>后就不能运行了
报错是这样的
  1. 2013-1-15 16:21:09 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
  2. 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@5973ea]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1556d12
  3. 2013-1-15 16:21:09 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  4. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1556d12: defining beans [personService,personService2]; root of factory hierarchy
  5. 2013-1-15 16:21:09 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
  6. 信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1556d12: defining beans [personService,personService2]; root of factory hierarchy
复制代码
请问这是什么原因呢



作者: yong230    时间: 2013-1-15 16:34
把xml文件中这句去了试试: factory-method="PersonServiceBean"
作者: 肖亚光    时间: 2013-1-15 16:37
yong230 发表于 2013-1-15 16:34
把xml文件中这句去了试试: factory-method="PersonServiceBean"

嗯  我问的就是去了是成功的 加上就不成功了 为什么
呵呵
不过我已经解决了   因为方法名字我写错了  谢谢




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2