Bean的作用域
。singleton:单列,指一个Bean容器中只存在一份
。prototype:每次请求创建新的实例,destroy方式不生效
。request:每次http请求创建一个实例且仅在当前request内有效
。session:同上,每次http请求创建,仅在当前session内有效
。global session:基于protlet的web中有效(protlet定义了global session),如果在web中,同session
Bean的生命周期
定义->初始化->使用->销毁
。初始化
- 实现org.springframework.beans.factory.InitializingBean接口,覆盖afterPropertiesSet()方法
public class ExampleInitBean implements InitializingBean{
@Override
public void afterPropertiesSet(){
}
}
- 配置init-method
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean{
public void init(){
//当ioc容器加载的时候,就会根据init-method指定的方法来调用指定类的init方法
}
}
。销毁
- 实现org.springframework.beans.factory.DisposableBean接口,覆盖destroy方法
- 配置destroy-method
代码同上
。配置全局默认初始化、销毁方法
<?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.xsd"
default-init-method="init" default-destroy-method="destroy">
</beans>
Aware
. Spring中提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化之后,可以获取相应资源
. 通过Aware接口,可以对Spring相应资源进行操作(一定要慎重)
. 为对Spring进行简单的扩展提供了方便的入口
ApplicationConextAware:
当一个类实现了ApplicationContextAware这个接口之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有引用到的bean对象
作用:它会向实现了这个接口的bean提供ApplicationContext(IOC容器的上下文)的信息。实现了这个接口的bean必须配置到同样的bean配置文件中去,并且由spring的bean容器去加载
BeanNameAware:
作用:他会提供一个关于beanName的定义的内容
bean的自动装配default-wire
No:不做任何操作,是个默认的选项
byName:根据属性名自动装配,此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配
<?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.xsd"
default-wire="byName">
<bean id="autoWiringService" class="com.mooc.autoWriingService"></bean>
<bean id="autoWiringDAO" class="com.mooc.autoWriingDAO"></bean>
</beans>
byType:如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配,如果存在多个该类型的bean,那么抛出异常,并指出不能使用byType方式进行自动装配;如果没有找到相匹配的属性,则什么事情都不发生;
Constructor:与byType方式相类似,不同之处是它应用于构造器参数。如果容器中没有找到与构造器参数类型一致的bean,那么抛出异常。
Resources
. 针对资源文件的统一接口
- UrlResource:URL对应的资源,根据一个URL地址即可构建
- ClassPathResource:获取类路径下的资源文件
- FileSystemResource: 获取文件系统里面的资源
- ServletContextResource: ServletContext封装的资源,用于访问ServletContext环境下的资源
- InputStreamResource: 针对于输入流封装的资源
- ByteArrayResource: 针对字节数组封装的资源
ResourcesLoader
- 对Resources加载的一个类,在Spring的IOC容器中所有的applicationContext都实现了ResourceLoader这个接口。所有的applicationContext接口都可以获取Resource实例。
public interface ResourceLoader{
Resource getResource(String location);
//输入一个文件的配置
}
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");
//默认的文件的路径
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");
//相对于classpath的资源文件
Resource template = ctx.getResource("file:some/resource/path/myTemplate.txt");
//从文件系统中获取一个资源文件的路径