- Bean配置项
- Id :类的唯一标识
- Class : 类型
- Scope :范围
- Constructor arguments :构造方法(注入使用)
- Properties : 属性(注入使用)
- Autowiring mode : 自动装在
- Lazy-initialization mode : 懒加载
- Initialization/destruction method : 初始化和销毁
- Bean的作用域
1、Singleton : 单例,只一个bean在IOC容器中只存在一份
<bean id="beanScope" class="com.base.java.test.BeanScope" scope="singleton"></bean>
2、Prototype : 每次请求或使用都会创建新的实例
<bean id="beanScope" class="com.base.java.test.BeanScope" scope="prototype"></bean>
3、Request : 每个http请求都会创建一个实例,且只在request中有效
4、Session : 每次http请求创建一份,且在session中有效
5、Global session :基于portlet的web有效,如果是web中,效果和session相同。(可用于系统集成,单点登陆共享session)
- Bean生命周期
- 生命周期步骤:
定义:在配置文件xml中定义bean
初始化 :IOC容器在加载中创建bean实例
使用 : 去IOC容器中取实例,进行使用
销毁 : 销毁bean实例
- Bean初始化和销毁
1)单类初始化和销毁
初始化两种方式:
a、XML配置文件
<bean id="beanLifeCycle" class="com.base.java.test.BeanLifeCycle" init-method="beanInit"></bean>
public class BeanLifeCycle {
public void beanInit(){
System.out.println("BeanLifeCycle init。。。");
}
public void say(){
System.out.println("beanLifeCycel say...");
}
}
b、实现org.springframework.beans.factory.InitializingBean里面的afterPropertiesSet方法
public class BeanLifeCycle implements InitializingBean,DisposableBean{
@Override
public void afterPropertiesSet() throws Exception {
}
}
销毁两种方式:
a、XML配置文件
<bean id="beanLifeCycle" class="com.base.java.test.BeanLifeCycle" destroy-method="beanDestory"></bean>
public class BeanLifeCycle {
public void beanDestory(){
System.out.println("BeanLifeCycle destory。。。");
}
public void say(){
System.out.println("beanLifeCycel say...");
}
}
b、实现org.springframework.beans.factory.DisposableBean里面的destroy方法
public class BeanLifeCycle implements InitializingBean,DisposableBean{
@Override
public void destroy() throws Exception {
}
}
说明:不管是初始化还是销毁,如果使用配置文件方式,找不到对应方法会报异常。如果配置文件和实现类两种方式都使用,会先执行实现类的方法在执行配置文件对应的方法。
2)全局初始化和销毁
<?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="start" default-destroy-method="stop">
<bean id="beanLifeCycle" class="com.base.java.test.BeanLifeCycle" ></bean>
</beans>
此种方式是全局初始化和销毁,所有类中都可以出现初始化和销毁方法,如果找不到对应方法不做任何操作。如果既有全局配置也有单类的初始化和销毁时,此全局配置不会执行。
- 实现Aware接口
目的:为了对spring进行简单的扩展,通过这些接口,可获取相应的资源,此处只以两个接口举例,BeanNameAware获取定义号的bean的ID名,ApplicationContextAware 获取上下文
public class BeanAware implements BeanNameAware, ApplicationContextAware {
private String beanName;
@Override
public void setBeanName(String s) {
this.beanName = s;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println(applicationContext.getBean(this.beanName));
}
}
- Resource
针对于资源文件的统一接口。
public class MlhResource implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public void getResource(){
Resource resource1 = applicationContext.getResource("classpath:mlh.txt");
Resource resource2 = applicationContext.getResource("file:C:\\Users\\27128\\IdeaProjects\\spring\\src\\mlh.txt");
Resource resource3 = applicationContext.getResource("mlh.txt");
Resource resource4 = applicationContext.getResource("url:https://www.imooc.com/video/3758/0");
System.out.println(resource1.getFilename());
System.out.println(resource2.getFilename());
System.out.println(resource3.getFilename());
System.out.println(resource4.getFilename());
}
}