spring中bean的作用域与懒加载及初始化方法和销毁方法以及spring容器的正常关闭

时间:2022-07-23 19:43:11

spring中bean的作用域与懒加载及初始化方法和销毁方法以及spring容器的正常关闭


默认为singleton,prototype会在调用getBean()方法时,也就是第一次真正用到bean时才会实例化。

所谓容器启动时指的是:ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");这句代码执行时。


spring中bean的作用域与懒加载及初始化方法和销毁方法以及spring容器的正常关闭


=========

bean.xml

=========

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
           default-lazy-init="false">
           
           <!-- id属于xml所有的属性,是确定bean唯一的属性,不能包含特殊字符   建议第一个字母小写 -->
           <!-- name属性可以包含特殊字符,当不包含特殊字符时应选用id来表示bean,id可以被xml解析 -->
            
            <!-- 使用类构造器方法创建bean -->
          <bean id="personService" class="blog.service.impl.PersonServiceBean" lazy-init="false"
          init-method="init" destroy-method="destroy"></bean>
           
           <!-- 使用静态工场方法创建bean -->
           <bean id="personService2" class="blog.service.impl.PersonServiceBeanFactory" 
           factory-method="createPersonServiceBean" init-method="init" destroy-method="destroy"></bean>
           
           <!-- 使用实例工场方法实例化 -->
           <bean id="PersonServiceBeanFactory" class="blog.service.impl.PersonServiceBeanFactory"></bean>
           <bean id="personService3" factory-bean="PersonServiceBeanFactory" factory-method="createPersonServiceBean2" 
           init-method="init" destroy-method="destroy"></bean>
</beans>

===========

springtest.java

===========

抽象类AbstractApplicationContext可以正常的关闭spring容器

package junit.test;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import blog.service.PersonService;
import blog.service.impl.PersonServiceBean;

public class springtest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}
	
	@Test
	public void instanceSpring(){//实例化spring容器
		//使用:在类路径寻找配置文件来实例化容器
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		ctx.close();
		/*PersonService personService = (PersonService)ctx.getBean("personService");
		personService.save();
		PersonService personService2 = (PersonService)ctx.getBean("personService2");
		personService2.save();
		PersonService personService3 = (PersonService)ctx.getBean("personService3");
		PersonService personService33 = (PersonService)ctx.getBean("personService3");
		System.out.println(personService3==personService33);
		personService3.save();*/
	}
}

=================

PersonServiceBean.java

=================

package blog.service.impl;

import blog.service.PersonService;


public class PersonServiceBean implements PersonService {
	
	public void init(){
		System.out.println("in init method......");
	}
	
	public PersonServiceBean(){
		System.out.println("in PersonServiceBean'constructor method!");
	}
	public void save(){
		System.out.println("in save method!");
	}
	
	public void destroy(){
		System.out.println("in destroy method......");
	}
}