厚昌营销网站建设,手机网站建设服务合同,设计好看的企业网站,wordpress 超级管理员目录 一、依赖注入
1.IOC思想
2.什么是依赖注入#xff1f;
3.实例化对象中有pojo类型属性
二、IOC在Spring中的实现方式
1.获取bean的三种方式
1.1根据bean的id获取
1.2根据bean的类型获取#xff08;最常用#xff0c;因为在IOC容器中#xff0c;一个类型的bean只…
目录 一、依赖注入
1.IOC思想
2.什么是依赖注入
3.实例化对象中有pojo类型属性
二、IOC在Spring中的实现方式
1.获取bean的三种方式
1.1根据bean的id获取
1.2根据bean的类型获取最常用因为在IOC容器中一个类型的bean只需要配置一次就够了
1.3根据bean的id和类型进行获取
1.4根据类所实现的类的类型获取通过接口获取 一、依赖注入
1.IOC思想
IOCInversion Of Control 翻译控制反转
举个例子
自己做饭买菜洗菜切菜炒菜~~~~等等步骤和细节都需要自己清楚的知道
点外卖下单等吃不需要关心创建细节 controller-----Service------dao controller依赖于ServiceService依赖于dao 2.什么是依赖注入 依赖注入即程序在运行时希望给某个对象所依赖的对象赋值就称为依赖注入需要什么传递什么传递的任务由Spring完成
简单来说就是对Controller所依赖的对象进行赋值依赖哪个对象spring就对哪个对象进行赋值。 注入赋值方式由三种 setter方法注入构造方法注入注解注入 示例1setter注入通过property标签给属性赋值
基本数据类型的成员属性直接通过value赋值。
对应的bean
bean idperson classcn.edu.xysf.ssm.entity.Personproperty namename value李晶晶/propertyproperty nameage value23/propertyproperty namegender value女/property/bean
测试类 Testpublic void PersonTest(){//1。需要实例化IOC容器,子类ApplicationContext context new ClassPathXmlApplicationContext(spring-config.xml);//2.从IOC容器中拿到想要的对象Person person (Person)context.getBean(person);System.out.println(person);
}
运行结果 示例2构造器注入 假如实体类有五个属性但是只给四个属性赋值此时可以使用namename可以为value指定它所赋值的属性
对应的bean bean idstudentThree classcom.zh.spring.pojo.Studentconstructor-arg value10002/constructor-argconstructor-arg value李四/constructor-argconstructor-arg value男/constructor-argconstructor-arg value22 nameage/constructor-arg/bean Testpublic void testDI02(){//获取IOC容器ApplicationContext ioc new ClassPathXmlApplicationContext(spring-ioc.xml);//获取beanStudent studentThree ioc.getBean(studentThree, Student.class);System.out.println(studentThree);} 注入赋值的数据类型有三种 基本数据类型和String类型普通POJO类型必须在Spring的配置文件中出现过的bean复杂类型集合类型 3.实例化对象中有pojo类型属性
如果Person类中有一个pojo类型的属性假如是idCard则在Person类中添加这个POJO属性然后新建一个IdCard类 添加对应的bean bean nameidCard_1 classcn.edu.xysf.ssm.entity.IDCardproperty nameid value123456789/propertyproperty namepubDep valuexxx公安局/propertyproperty namevailTime value2038-11-09/property/bean
运行结果 二、IOC在Spring中的实现方式
BeanFactory实现基本实现spring内部使用的接口面向spring本身不对开发人员开放ApplicationContext实现BeanFactory的子接口功能更多面向开发人员。
以上只是接口没有具体的实现方法。 ClassPathXMLApplicationContext类路径下的XML配置文件推荐使用。 FileSystemXmlApplicationContext文件系统路径下的XML配置文件不推荐使用因为之后的项目会在不同的电脑上运行配置文件不一定存在于每个电脑上 目前为止spring配置文件的名字可以随便起但是到了ssm整合时会有固定的名字。
配置文件的作用就是获取IOC容器底层通过反射和工厂模式实现
1.获取bean的三种方式 bean idstudentOne classcom.zh.spring.pojo.Student/bean
1.1根据bean的id获取 根据bean的id获取时实例化对象中一定要有无参构造方法否则会报错NoSuchMethodException。 Testpublic void testIOC(){//获取IOC容器ApplicationContext ioc new ClassPathXmlApplicationContext(spring-ioc.xml);//根据id获取bean对象Student studentOne (Student) ioc.getBean(studentOne);System.out.println(student);}
1.2根据bean的类型获取最常用因为在IOC容器中一个类型的bean只需要配置一次就够了 通过类型获取到的对象返回值就是该类型的实例化对象
注意根据类型获取bean时要求IOC容器中有且只有一个bean否则会报错NoUniqueBeanDefinitionException。 bean idstudentOne classcom.zh.spring.pojo.Student/beanbean idstudentTwo classcom.zh.spring.pojo.Student/beanTestpublic void testIOC(){//获取IOC容器ApplicationContext ioc new ClassPathXmlApplicationContext(spring-ioc.xml);//根据类型获取Student student ioc.getBean(Student.class);System.out.println(student);}
1.3根据bean的id和类型进行获取 Testpublic void testIOC(){//获取IOC容器ApplicationContext ioc new ClassPathXmlApplicationContext(spring-ioc.xml);//根据bean的id和类型进行获取Student studentOne ioc.getBean(studentOne, Student.class);System.out.println(studentOne);}
1.4根据类所实现的类的类型获取通过接口获取 Testpublic void testIOC(){//获取IOC容器ApplicationContext ioc new ClassPathXmlApplicationContext(spring-ioc.xml);//根据接口实获取Person person ioc.getBean(Person.class);System.out.println(person);} 结论根据类型来获取bean时在满足bean唯一性的前提下
其实只是看【对象 instanceof 指定的类型 】的返回值
只要返回的时true既可以认定和类型匹配能够获取到
即通过bean的类型、bean所继承的类的类型bean所实现的类的类型都可以获取到bean