spring入门学习-3、Bean装配(XML)

  • Bean配置项
  1. Id :类的唯一标识
  2. Class : 类型
  3. Scope :范围
  4. Constructor arguments :构造方法(注入使用)
  5. Properties : 属性(注入使用)
  6. Autowiring mode : 自动装在
  7. Lazy-initialization mode : 懒加载
  8. 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生命周期
  1. 生命周期步骤:

定义:在配置文件xml中定义bean

初始化 :IOC容器在加载中创建bean实例

使用 : 去IOC容器中取实例,进行使用

销毁 : 销毁bean实例

  1. 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());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值