Spring--2.Spring之IOC--IOC容器的23个实验(1) 中的所有实验我都是在同一个工程中进行的,从第十个实验开始,我将新建一个新的工程开始实验。
目前导包还是跟第一个项目一致,bean包直接复制过来,新建test类,新建源文件夹,新建spring配置文件applicationContext.xml
实验10.创建带有生命周期的bean
1.Book中新建自定义初始化方法和销毁方法
ioc容器注册单实例bean时:
ioc容器注册多实例bean时:
实验11.测试bean的后置处置器
1.新建后置处理器(实现BeanPostProcessor接口)
2.配置后置处理器
3.测试
实验12.引用外部属性文件(Spring管理连接池)
配置:
新建properties文件保存数据库连接信息:
配置外部属性文件(依赖context名称空间)
注意username是Spring的key中的一个关键字;为了防止配置文件中的key和spring自己的关键字冲突,我在properties中加上前缀jdbc
注意property中name要看提示,别盲目敲,容易出错,value中不要有多空格
补充Spring的username到底是什么?--系统用户名(例如我的lhl)
实验13.基于xml的自动配置(自定义类型自动赋值)
javaBean(基本类型)没有自动赋值;
自定义类型的属性是一个对象,这个对象在容器中可能存在
实验14.[SpEl测试]
在SpEl中使用字面量
引用其他bean
引用其他bean的某个属性值
调用非静态方法
调用静态方法
使用运算符