Spring学习笔记(四、Bean装配(上))

2017-05-26  本文已影响216人  鲁克巴克诗

上一篇:Spring学习笔记(三、IoC)

** Bean常用的配置项**

Bean的作用域

这里global session有可能还是不懂,特此解释:举个例子,一个大型的系统,有多个独立的模块组成,他们有一个统一的登录入口,登录进入后,用户可以在整个系统内操作,不需要在登录其他模块时,重新登录。这就是portlet的global session。

下面用例子证明:

Paste_Image.png Paste_Image.png

上图可见,声明了singleton作用域,即使获取多次实例,也依旧是原来的那一个。

修改spring-ioc.xml:

Paste_Image.png

Junit测试结果变成:

Paste_Image.png

上图可见,声明了prototype作用域,获取多次实例,每一次的hashcode都不同。

余下三个,由于涉及到web,在此就不做测验了。相信工作中,会有很多机会去尝试。

Bean的生命周期

生命周期 —— 初始化

  1. 实现org.springframework.beans.factory.InitializingBean接口,重写afterPropertiesSet方法
  2. 配置init-method
    以上两种方法,可以在Ioc容器初始化实例时执行一些实例内部的初始化工作。
    下面分别举例:

Paste_Image.png Paste_Image.png

为了测试IoC容器启动,我加了一个空的测试方法。

Paste_Image.png

执行后

Paste_Image.png

第一个【实现org.springframework.beans.factory.InitializingBean接口,重写afterPropertiesSet方法】测试初始化成功。


Paste_Image.png Paste_Image.png Paste_Image.png

第二个【配置init-method】测试初始化成功。


**生命周期 —— 销毁 **

  1. 实现org.springframework.beans.factory.DisposableBean接口,重写destroy方法
  2. 配置destroy-method
    这两个销毁方式和初始化测试方式是一样的,我就不做测试了。

还可以配置全局默认初始化、销毁方法,就是为当前IoC容器中所有的bean增加初始化和销毁时执行的方法

Paste_Image.png

既然实现初始化和销毁有三种不同的方法,那么哪种优先级最高呢?下面来测试。

package test4;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * Created by amber on 2017/5/27.
 */
public class TestInitial3 implements InitializingBean,DisposableBean {

    public void initInBean() {
        System.out.println("Bean内自定义init——start——" + hashCode());

    }

    public void destroyInBean(){
        System.out.println("Bean内自定义destroy——stop——" + hashCode());
    }

    public void defaultInit() {
        System.out.println("IoC全局默认defaultInit——start——" + hashCode());
    }


    public void defaultDestroy(){
        System.out.println("IoC全局默认defaultDestroy——stop——" + hashCode());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("重写接口afterPropertiesSet——start——" + hashCode());
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("重写接口DisposableBean——stop" + hashCode());
    }
}
<?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="defaultInit" default-destroy-method="defaultDestroy">

    <bean id="testInitial3" class="test4.TestInitial3" init-method="initInBean" destroy-method="destroyInBean"></bean>

 </beans>

测试后结果:

Paste_Image.png

发现,三种初始化和销毁方法同时执行时,全局默认的是不执行的。其次,最先执行的是接口。然后是bean内自定义的。
也就是三个优先级为:接口>Bean中配置>全局默认
其中全局默认即使配置了,类里面不写也不会报错,其他两个,则会报错。


Aware

Aware相关常用接口

以上接口,都是在bean初始化时候调用。

下面测试接口功能是否如上所述:

package test5;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Created by amber on 2017/5/27.
 */
public class TestAware implements ApplicationContextAware, BeanNameAware {

    private String beanName;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("setApplicationContext执行时间戳:" + System.currentTimeMillis());
        System.out.println("获取当前bean的hashCode:" + applicationContext.getBean(beanName).hashCode());
    }

    @Override
    public void setBeanName(String name) {
        beanName = name;
        System.out.println("setBeanName执行时间戳:" + System.currentTimeMillis());

    }
}

IoC容器中

<bean id="testAware" class="test5.TestAware"></bean>

单元测试中

 @Test
    public void testAware(){
        System.out.println("单元测试获取bean的hashCode:"+getBean("testAware").hashCode());
    }

结果如下。
为什么我要加时间戳呢,主要是为了看哪个回调会先执行,经过多次测试发现BeanNameAware的setBeanName(String name)会先执行。

Paste_Image.png

Bean的自动装配(AutoWiring)
为什么需要自动装配?
其实目的很简单,是偷懒。自动装配可以不用让我们在IoC容器的Bean中声明这些东西啦:

 <property name="testDao" ref="testDao"/>
 <constructor-arg name="testDao" ref="testDao"/>

关于Spring自动装配可以参考:spring的自动装配

本次讲设置在<beans>根节点的全局:default-autowire

在测试之前呢,先把结构抛出来:


4 Paste_Image.png Paste_Image.png Paste_Image.png

自动装配之byName:要保证beanId和调用它的类属性名称一致。

Paste_Image.png Paste_Image.png

自动装配之byType:将IoC容器中AutoWiringDao的id去掉,只留下class。

Paste_Image.png Paste_Image.png

自动装配之constructor:

Paste_Image.png Paste_Image.png

Resource

Resource Loader

Resource Loader接口详情:


Paste_Image.png

可以看到,我们需要传入一个location进去,然后接口会返回一个对应Resource给我们。那么location这个值,我们都可以传哪样的呢?

前缀 样例 说明
classpath: classpath:com/myapp/config.xml 从类路径加载
file: file:///data/config.xml 将其作为 URL 对象,从文件系统加载
http: http://myserver/logo.png 将其作为 URL 对象 加载
(none) /data/config.xml 取决于底层的 ApplicationContext

下面来测试:

使用classpath获取资源

1.首先在resource文件夹下增加一个config.txt文件。

Paste_Image.png

2.新建一个TesrResource类,并通过实现ApplicationContextAware接口获取到ApplicationContext,然后传入classpath:位置的地址得到目标文件信息。

Paste_Image.png

3.IoC容器注册bean


Paste_Image.png

4.测试

Paste_Image.png

5.结果

Paste_Image.png

使用file:获取资源

1.修改TestResource类的resource方法

Paste_Image.png

2.测试结果:

Paste_Image.png

使用http:获取资源

1.修改resource方法

Paste_Image.png

2.测试结果


Paste_Image.png

不使用前缀获取资源

1.修改resource方法

Paste_Image.png

2.测试结果

Paste_Image.png

为什么没有前缀也能获取到我们想要的文件呢?

下一篇:Spring学习笔记(五、Bean装配(下))

上一篇 下一篇

猜你喜欢

热点阅读