学做网站从什么开始,北京网站开发专员,做网站的工资,高端大气网站推荐一、SpringDI#xff08;依赖注入#xff09;
1.DI依赖注入
1.1 Spring DI的理解
关键字:名词解释 DI( Dependecy Inject#xff0c;中文释义:依赖注入#xff09;是对Ioc概念的不同角度的描述#xff0c;是指应用程序在运行时#xff0c;每一个bean对象都依赖IoC 容器…一、SpringDI依赖注入
1.DI依赖注入
1.1 Spring DI的理解
关键字:名词解释 DI( Dependecy Inject中文释义:依赖注入是对Ioc概念的不同角度的描述是指应用程序在运行时每一个bean对象都依赖IoC 容器注入当前bean对象所需要的另外一个bean对象。例如在(MyBatis整合Spring 时SqlSessionFactoryBean依赖IoC容器注入一个Datasource数据源bean ) ;
1.2 作用 将springioc容器所创建的各个组件使用DI的语法进行关联耦合(胶水)
1.3 DI实现方式
①set注入 语法 set方法set配置 property name value ref bean idcontrollerImp classcom.ztt.controller.UserControllerImp
property nameservice refserviceImp/property
/bean②构造注入 语法 构造方法构造配置 constructor-arg name type index value ref bean idstudent2 classcom.ztt.pojo.Student
constructor-arg namestuName value甜甜/constructor-arg
constructor-arg namestuAge value18/constructor-arg
constructor-arg namestuHobby value学习/constructor-arg
/bean
/****************构造注入*******************/
public Student(String stuName, int stuAge, String stuHobby) {this.stuName stuName;this.stuAge stuAge;this.stuHobby stuHobby;
}public Student() {
}/****************set注入*******************/
public void setStuName(String stuName) {
this.stuName stuName;
}public void setStuAge(int stuAge) {this.stuAge stuAge;
}public void setStuHobby(String stuHobby) {
this.stuHobby stuHobby;
}
③注解注入
1.4 DI数据类型 基本类型与String
!--set注入基本类型与String--
bean idstudent classcom.ztt.pojo.Student
property namestuName value甜甜/property
property namestuAge value18/property
property namestuHobby value学习/property
/bean JavaBean
bean idserviceImp classcom.ztt.service.UserServiceImpproperty namedao refdaoImp/property
/bean 复杂类型list set array map properties构造注入不支持
!--set注入复杂类型--
bean idteacher classcom.ztt.pojo.Teacher
property namemyList
list
value苹果/value
value西瓜/value
value桃子/value
/list
/propertyproperty namearray
array
value跑步/value
value跳绳/value
value学习/value
/array
/property
property namemySet
set
value蜜雪冰城/value
value霸王茶姬/value
value茉莉奶绿/value
/set
/propertyproperty namemyMap
map
entry key甜甜 value甜甜不甜/entry
entry key娜娜 value娜娜不辣/entry
entry key的的 value啊的的的/entry
/map
/property
property namemyProp
props
prop key法国巴黎/prop
prop key英国伦敦/prop
prop key美国华盛顿/prop
/props
/property
/bean
④DI使用步骤 思考什么方式什么数据类型给属性提供set(构造)方法编写配置文件 二、SpringIOC容器对Bean管理
1.bean实例化
通过构造方法默认通过工厂方法通过静态工厂方法
!--bean的实例化方式1--
!-- bean idstudent classcom.ztt.pojo.Student/bean--!--bean的实例化方式2--
!-- bean idstudent classcom.ztt.pojo.Student factory-beanfactory factory-methodcreateStu/bean--!-- bean idfactory classcom.ztt.factory.BeansFactory/bean--!--bean的实例化方式3--
!-- bean idstudent classcom.ztt.factory.StaticBeansFactory factory-methodcreateStu/bean--
2.bean作用域
含义spring对于创建javaBean实例的方式语法bean scope属性值/bean属性值
singleton单例默认
prototype多例
request一个请求创建一个
session一个会话创建一个
!--bean的作用域--
!-- bean idteacher classcom.ztt.pojo.Teacher scopeprototype/bean--
3.bean生命周期 实例化 属性赋值(DI) 初始化 接口 DisposableBean 属性 destory-method 操作使用 销毁了 接口 InitializingBean 属性 init-method !--bean的生命周期--
bean iduser classcom.ztt.pojo.User init-methoddoinit destroy-methoddoDestory
property nameuname value杨文琪/property
/bean
public class User implements InitializingBean, DisposableBean {private String uname;public void setUname(String uname) {System.out.println(bean生命周期》属性赋值);this.uname uname;}public User() {System.out.println(bean生命周期》实例化);}//接口Overridepublic void afterPropertiesSet() throws Exception {System.out.println(bean生命周期》初始化接口);}//属性public void doinit() {System.out.println(bean生命周期》初始化属性);}//接口Overridepublic void destroy() throws Exception {System.out.println(bean生命周期》销毁接口);}//属性public void doDestory() {System.out.println(bean生命周期》销毁属性);}
}