spring中关于IOC的学习说明
ioc的解释说明和理解
IOC(inversion of control): 控制反转,反转的是对象的创建权,用于削减程序之间的耦合关系,底层使用反射技术实现
传统方式创建对象: new 对象(); (主动创建)
IOC方式创建对象: 找容器(被动接收),本质上就是一个Map集合
作用:解耦
1、通过标记(标记就是配置文件中的key)找工厂,工厂帮我们创建对应的类对象,并返回给我们使用
2、当前类可以选择主动出击(new的方式)创建对象,但是此时耦合度高。
3、 把主动式改成被动接收,由工厂对象为当前类生产所必须的关联对象,此时降低了两个类的依赖关系。
代码实现spring中的DI【依赖注入】
Spring中的依赖注入:
注入的方式有三种:
第一种:使用构造方法注入
要求:必须有对应参数列表的构造函数
第二种:使用set方法注入
要求:提供被注入对象的set方法(不需要get方法)
第三种:使用注解注入
注入的数据类型有三类:
第一类:基本类型和String
第二类:其他bean类型
要求:其他bean指的是在spring的配置文件中定义过的bean,或者是用注解注释过的类。
第三类:复杂类型(集合类型)
Array: 数组
List:
Map:
Properties:
<!-- 配置文件中的数据-->
<?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">
<!-- 当采用有参构造进行注入时,class会报错,因为会优先找构造方法,需要给变量赋值即可-->
<!--<bean id="Applocation" class="it.heima.services.impl.ApplocationImpl4">-->
<!-- <constructor-arg name="name" value="杨"></constructor-arg>-->
<!-- <constructor-arg name="id" value="1"></constructor-arg>-->
<!-- <constructor-arg name="age" value="12"></constructor-arg>-->
<!-- <constructor-arg index="1" value="杨"></constructor-arg>-->
<!-- <constructor-arg index="0" value="1"></constructor-arg>-->
<!-- <constructor-arg index="2" value="12"></constructor-arg>-->
<!--</bean>-->
<!-- 通过setter的方式进行依赖注入-->
<!-- <bean id="Applocation" class="it.heima.services.impl.ApplocationImpl4">-->
<!-- <property name="name" value="yag"></property>-->
<!-- <property name="id" value="12"></property>-->
<!-- <property name="age" value="12"></property>-->
<!-- ref 代表引用对象。引用的内容需要来自于ioc容器中-->
<!-- <property name="user" ref="user"></property>-->
<!-- </bean>-->
<!-- 创建ioc容器,保存数据内容-->
<!-- <bean id="user" class="it.heima.pojo.User">-->
<!-- <property name="password" value="123"></property>-->
<!-- <property name="money" value="12"></property>-->
<!-- </bean>-->
<!-- 复杂数据类型-->
<bean id="Applocation" class="it.heima.services.impl.ApplocationImpl4">
<property name="names">
<array>
<value>小李子</value>
<value>千玺</value>
</array>
</property>
<property name="list">
<list>
<value>柚子</value>
<value>草莓</value>
</list>
</property>
<property name="map">
<map>
<entry key="狗" value="拉布拉多"></entry>
<entry key="猫" value="布偶"></entry>
</map>
</property>
<property name="pro">
<props>
<prop key="dagname"> 小七</prop>
<prop key="catname"> 十一</prop>
</props>
</property>
</bean>
</beans>
package it.test.servlet;
public class ApplocationTest4 {
@Test
public void test1(){
/*
di依赖注入的三种数据类型
基本数据类型
其他bean数据类型
复杂数据类型
*/
ApplicationContext context =
new ClassPathXmlApplicationContext("applocationcontact4.xml");
Applocation applocation =(Applocation) context.getBean("Applocation");
System.out.println("applocation = " + applocation);
applocation.add();
}
}
package it.test.services.impl;
//services中的数据处理--只有setter方法
public class ApplocationImpl4 implements Applocation {
private int id;
private String name;
private String age;
private User user;
private String[] names;
private List<String> list;
private Map<String,String> map;
private Properties pro;
@Override
public void add() {
System.out.println("DI-----");
System.out.println(id+" : "+name+" : "+age);
System.out.println(user);
System.out.println(names);
System.out.println(map);
System.out.println(list);
System.out.println(pro);
}
// public ApplocationImpl4(int id, String name, String age) {
// this.id = id;
// this.name = name;
// this.age = age;
// }
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setUser(User user) {
this.user = user;
}
public void setNames(String[] names) {
this.names = names;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setPro(Properties pro) {
this.pro = pro;
}
}
package it.test.pojo;
//当前特殊类型中的数据
public class User {
private String password;
private float money;
public void setPassword(String password) {
this.password = password;
}
public void setMoney(float money) {
this.money = money;
}
public User(String password, float money) {
this.password = password;
this.money = money;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"password='" + password + '\'' +
", money=" + money +
'}';
}
}
//面向接口式开发
public interface Applocation {
void add();
}
Spring IOC与DI详解
1130

被折叠的 条评论
为什么被折叠?



