深圳银行网站建设,wordpress 审批流,直播推广渠道,saas WordPress学习材料
尚硅谷Spring零基础入门到进阶#xff0c;一套搞定spring6全套视频教程#xff08;源码级讲解#xff09;
IoC
控制反转。是一种设计思想。
1.获取bean对象的方法
通过id#xff0c;通过class#xff0c;和双重方式。 ApplicationContext context new Cla…学习材料
尚硅谷Spring零基础入门到进阶一套搞定spring6全套视频教程源码级讲解
IoC
控制反转。是一种设计思想。
1.获取bean对象的方法
通过id通过class和双重方式。 ApplicationContext context new ClassPathXmlApplicationContext(bean.xml);User user (User) context.getBean(user);System.out.println(通过id获取bean对象user);User user2 context.getBean(User.class);System.out.println(通过类型获取bean对象user2);User user3 context.getBean(user, User.class);System.out.println(通过id和类型获取bean对象user3);2.依赖注入分set注入和构造器注入其实本质就是xml里面的命令不同。这里注意如果使用set那么定义类里面一定要有set函数构造器注入那么一定要有相应的构造器
普通属性String Interger set和构造器感觉还是set比较方便
bean idstudentOne classcom.atguigu.spring6.bean.Student!-- property标签通过组件类的setXxx()方法给组件对象设置属性 --!-- name属性指定属性名这个属性名是getXxx()、setXxx()方法定义的和成员变量无关 --!-- value属性指定属性值 --property nameid value1001/propertyproperty namename value张三/propertyproperty nameage value23/propertyproperty namesex value男/property
/beanbean idstudentTwo classcom.atguigu.spring6.bean.Studentconstructor-arg value1002/constructor-argconstructor-arg value李四/constructor-argconstructor-arg value33/constructor-argconstructor-arg value女/constructor-arg
/bean特殊属性null特殊的大于小于等xml转义字符cdata
property namenamenull /
/property!-- 小于号在XML文档中用来定义标签的开始不能随便使用 --
!-- 解决方案一使用XML实体来代替 --
property nameexpression valuea lt; b/property nameexpression!-- 解决方案二使用CDATA节 --!-- CDATA中的C代表Character是文本、字符的含义CDATA就表示纯文本数据 --!-- XML解析器看到CDATA节就知道这里是纯文本就不会当作XML标签或属性来解析 --!-- 所以CDATA节中写什么符号都随意 --value![CDATA[a b]]/value
/property对象外部bean内部bean级联通过.来实现
外部bean
bean idclazzOne classcom.atguigu.spring6.bean.Clazzproperty nameclazzId value1111/propertyproperty nameclazzName value财源滚滚班/property
/bean
bean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/property!-- ref属性引用IOC容器中某个bean的id将所对应的bean为属性赋值 --property nameclazz refclazzOne/property
/bean
内部bean
bean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/propertyproperty nameclazz!-- 在一个bean中再声明一个bean就是内部bean --!-- 内部bean只能用于给属性赋值不能在外部通过IOC容器获取因此可以省略id属性 --bean idclazzInner classcom.atguigu.spring6.bean.Clazzproperty nameclazzId value2222/propertyproperty nameclazzName value远大前程班/property/bean/property
/bean
级联通过.来实现bean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/propertyproperty nameclazz refclazzOne/propertyproperty nameclazz.clazzId value3333/propertyproperty nameclazz.clazzName value最强王者班/property
/bean数组 property namehobbiesarrayvalue抽烟/valuevalue喝酒/valuevalue烫头/value/array/propertyList
bean idclazzTwo classcom.atguigu.spring6.bean.Clazzproperty nameclazzId value4444/propertyproperty nameclazzName valueJavaee0222/propertyproperty namestudentslistref beanstudentOne/refref beanstudentTwo/refref beanstudentThree/ref/list/property
/beanMap
bean idteacherOne classcom.atguigu.spring6.bean.Teacherproperty nameteacherId value10010/propertyproperty nameteacherName value大宝/property
/beanbean idteacherTwo classcom.atguigu.spring6.bean.Teacherproperty nameteacherId value10086/propertyproperty nameteacherName value二宝/property
/beanbean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/property!-- ref属性引用IOC容器中某个bean的id将所对应的bean为属性赋值 --property nameclazz refclazzOne/propertyproperty namehobbiesarrayvalue抽烟/valuevalue喝酒/valuevalue烫头/value/array/propertyproperty nameteacherMapmapentrykeyvalue10010/value/keyref beanteacherOne/ref/entryentrykeyvalue10086/value/keyref beanteacherTwo/ref/entry/map/property
/beanutil注意要修改头文件
!--list集合类型的bean--
util:list idstudentsref beanstudentOne/refref beanstudentTwo/refref beanstudentThree/ref
/util:list
!--map集合类型的bean--
util:map idteacherMapentrykeyvalue10010/value/keyref beanteacherOne/ref/entryentrykeyvalue10086/value/keyref beanteacherTwo/ref/entry
/util:map
bean idclazzTwo classcom.atguigu.spring6.bean.Clazzproperty nameclazzId value4444/propertyproperty nameclazzName valueJavaee0222/propertyproperty namestudents refstudents/property
/bean
bean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/property!-- ref属性引用IOC容器中某个bean的id将所对应的bean为属性赋值 --property nameclazz refclazzOne/propertyproperty namehobbiesarrayvalue抽烟/valuevalue喝酒/valuevalue烫头/value/array/propertyproperty nameteacherMap refteacherMap/property
/bean使用p标签注意要修改头文件
bean idstudentSix classcom.atguigu.spring6.bean.Studentp:id1006 p:name小明 p:clazz-refclazzOne p:teacherMap-refteacherMap/bean3.从外部对象中获取配置内容
要修改对应的头文件。 外部文件放在了resource目录下。
!-- 引入外部属性文件 --
context:property-placeholder locationclasspath:jdbc.properties/
bean iddruidDataSource classcom.alibaba.druid.pool.DruidDataSourceproperty nameurl value${jdbc.url}/property namedriverClassName value${jdbc.driver}/property nameusername value${jdbc.user}/property namepassword value${jdbc.password}/
/bean4.作用域 5.生命周期 bean对象创建调用无参构造器 给bean对象设置属性 bean的后置处理器初始化之前 bean对象初始化需在配置bean时指定初始化方法自己设置 bean的后置处理器初始化之后 bean对象就绪可以使用 bean对象销毁需在配置bean时指定销毁方法自己设置 IOC容器关闭
!-- 使用init-method属性指定初始化方法 --
!-- 使用destroy-method属性指定销毁方法 --
bean classcom.atguigu.spring6.bean.User scopeprototype init-methodinitMethod destroy-methoddestroyMethodproperty nameid value1001/propertyproperty nameusername valueadmin/propertyproperty namepassword value123456/propertyproperty nameage value23/property
/bean
!-- bean的后置处理器要放入IOC容器才能生效 --
bean idmyBeanProcessor classcom.zy.spring6.iocxml.livestage.MyBeanProcesser/这里的后置处理器需要实现BeanPostProcessor接口的两个方法分别作用于初始化前和初始化后。 值得注意的是这个类需要在xml里面进行配置且适用于容器管理所有的bean
6.FactoryBean
感觉就是在配置类里面套上了一个盒子。使得在xml里面只要配置FactoryBean就好了。老师说是整合其他框架用的暂时感受不到它的魅力。
7.基于xml的自动装配
老师设置的环境是JavaWeb常见的场景controller-》service-》dao 那么controller里面要创建一个service对象service里面要创建一个dao对象。值得注意这里service和dao分别需要有接口和对应的实现类与JavaWeb里面实现的一样。 IoC可以简化这个过程只要在controller里创建一个service的属性并设置set方法service里面也一样。只要在xml里面配置 好就会自动装配。