【SSH三大框架】Spring基础第一篇:搭建Spring环境、实例化Bean、管理Bean的作用域以及Bean的生命周期

一、搭建Spring环境:

在lib目录下引入jar包,然后add to path,这就不过多说了。

二、实例化Bean的三种方式:

首先,我们先写两个java类:

接口类:

public interface PersonService {
	public abstract void save();

}

实现类:

public class PersonServiceBean implements PersonService {
	
	@Override
	public void save(){
		System.out.println("我是save()方法");
	}
}

再写一个测试方法:

public class SpringTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		
	}

	@Test
	public void instanceSpring() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService)ctx.getBean("personService");
		personService.save();
	}

}

1、使用类构造器实例化Bean

如果我们使用类构造器实例化Bean,则我们需要配置一下beans.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" xmlns:context="http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
   	
   	<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
	
</beans>
id是这个bean的唯一标识,class是这个bean指向的路径。

我们运行测试类中的instanceSpring方法,可以在控制台打印出:

我是save()方法

2、使用静态工厂实例化Bean

首先,我们建立一个工厂类,其中有个方法是静态的:

public class PersonServiceBeanFactory {
	public static PersonServiceBean createPersonServiceBean(){
		return new PersonServiceBean();
	}	
}
然后,我们需要配置一下beans.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" xmlns:context="http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
   	
   	<bean id="personServiceFactory" class="cn.itcast.service.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBean"></bean>  
	
</beans>
id是这个bean的唯一标识,class表明指向的类,factory-method表明使用工厂方法中的某个方法。

我们运行测试类中的instanceSpring方法,可以在控制台打印出:

我是save()方法

3、使用实例工厂的方法实例化Bean

使用实例工厂需要配置两个bean:

我们配置一下beans.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" xmlns:context="http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
   	
   	<bean id="personServiceFactory" class="cn.itcast.service.impl.PersonServiceBeanFactory"></bean>  
	<bean id="personService" factory-bean="personServiceFactory" factory-method="createPersonServiceBean"></bean>
	
</beans>
可以看到,我们配置了两个bean,第二个bean通过factory-bean引用了第一个bean,并调用factory-method

我们看一下PersonServiceBeanFactory.java:

public class PersonServiceBeanFactory {
	public PersonServiceBean createPersonServiceBean(){  //注意,这里相对于静态工厂少了个static  
	    return new PersonServiceBean();  
	} 
}
有一点需要注意的是,这里被调用的bean所指向的类中要调用的方法不能是静态的,因为这不是静态工厂实例化。

我们运行测试类中的instanceSpring方法,可以在控制台打印出:

我是save()方法

三、管理Bean的作用域

在Spring的Bean中有一个属性scope是用来配置Spring Bean的作用域,它标识Bean的作用域

在Spring 2.0之后默认有五种类型的Bean:

singleton、prototype、request、session、global session(application)

其中,后三种是用于WEB中的Bean对象,这里只介绍前两种。

PS:需要注意的一点就是,如果Bean没有设置作用域的时候,默认是singleton类型

1、 .singleton

当一个Bean的作用域设置为.singleton,Srping IOC容器中仅仅会存在一个共享的bean实例。

在所有对bean的请求中,只要请求的id与bean的id相同,则他们返回的实例都是同一个。

即当设置为singleton的时候,容器只会创建唯一的一个实例。这个单一实例会被存储到单例缓存(singleton cache)中,并且所有针对该bean的后续请求和引用都将返回被缓存的对象实例。

2、.prototype

当bean的作用域设置为prototype的时候,在每次请求bean的时候,Spring IOC容器都会创建一个新的Bean实例。

四、Bean的作用域

1、当Bean的作用域是单例(scope="singleton"或者没有scope属性)的时候

当容器实例化的时候,Bean也同时实例化了。

另外,如果当bean的lazy-init属性设置为true的时候,会延迟初始化,即在调用getBean()方法的时候初始化Bean

2、当Bean的作用域是(scope="prototype")的时候

当调用getBean()方法的时候,Bean才会被实例化。


对于所有的Bean实例有几个属性,init-method方法和destroy-method方法。

init-method方法是在实例化Bean之后,调用此方法(这是通过IOC容器反射技术实现的)

destroy-method方法是在容器关闭之后,调用此方法











发布了149 篇原创文章 · 获赞 25 · 访问量 36万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章