sping27: bean装配-基于xml

  bean的作用域:  scope=""

<bean id="someService" class="com.atChina2.service.SomeServiceImpl" scope="singleton"/>

   主要掌握 singleton, prototype即可

  bean的声明周期

   

 init-method,destroy-method配置声明周期方法

配置

<!-- 定义bean的生命始末方法,自定义方法参与到spring创建和销毁对象的过程中。
	      1>. 在java类中定义方法,方法的原形: public void 方法名(无参数){...} 
	      2>. 在定义bean的时候,告诉spring两个方法的存在
	      <bean id="xx" class="yy" 
	         init-method="" destroy-method="" />
	      -->
	<bean id="someService" class="com.atChina2.service.SomeServiceImpl" init-method="startUp" destroy-method="endDown" scope="singleton"/>

测试 

@Test
	public void testClassPathService(){
		String configLocation = "applicationContext.xml"; // 类路径的根目录
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
		com.atChina2.service.SomeService ss = (com.atChina2.service.SomeService)ctx.getBean("someService");
		ss.doSome();	
		/*
		 * 1.容器方法必须close,  2. 必须是单例  满足这两个条件,销毁方法才会执行
		 */
		((ClassPathXmlApplicationContext)ctx).close();
	}

猜你喜欢

转载自blog.csdn.net/m0_37564426/article/details/90105985