ioc的实现
ioc的实现
一,用配置文件xml实现
首先用maven导入spring的包
new 一个 file 在xml configuration file 里面选择spring config
<?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">
<!-- 一般情况下 类型 变量名 =new 类型()
id是变量名 class 包名.类名
在<property> 给 name(变量名)未 str 的 变量赋值为 wwk
ref 是给引用类型 比如在此之前已经实例化好的对象
value 是基本类型
-->
<bean id="hellow" class="HelloSpring">
<property name="str" value="wwk"></property>
</bean> //其实就相当于新建了一个类
</beans>
使用
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");//配置文件的名字
HelloSpring he=(HelloSpring)context.getBean("hellow"); //返回值是Object 要强转
System.out.println(he.str); //直接使用就好了
1,通过无参构造注入
public class HelloSpring {
public String str;
public String str1;
public void setStr(String str) {
this.str = str;
}
public void setStr1(String str1) {
this.str1 = str1;
}
}
<bean id="hellow" class="kevindemo.HellowSpring">
<property name="str" value="wwk"></property>
</bean>
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");//配置文件的名字
HellowSpring he=(HellowSpring)context.getBean("hellow"); //返回值是Object 要强转
System.out.println(he.str); //直
默认的构造器
2,通过构造器注入,就是不用setter了
public class hellow {
public String name;
public hellow(String name){
this.name=name;
}
}
<bean id="h" class="kevindemo.hellow">
<constructor-arg name="name" value="kk"> //这里的 不同
</constructor-arg>
</bean>
ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");//配置文件的名字
hellow he1=(hellow)context.getBean("h"); //返回值是Object 要强转
System.out.println(he1.name); //直
在配置文件执行的时候(程序刚开始)就已经初始化了,以后用的时候,用到什么对象就从容器里拿哪个对象 (容器就是那个配置文件)
二 DI注入环境
介绍
依赖:bean对象 的创建依赖于容器
注入:bean对象的所有属性由容器注入
1,构造器注入
前面已经讲过了
2,set注入(重点) 全都是有set方法注如的
对于普通的对象直接注入即可,和上面相同
public class Address {
public String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
// 通过setter注入
<bean id="add" class="Address">
<property name="address" value="qufu"></property>
</bean>
**对于复杂对象注入**
public class Student{
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;//一个类继承了hashtable
}
<?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">
<bean id="add" class="Address">
<property name="address" value="qufu"></property>
</bean>
<bean id="student" class="Student" >
<!-- 普通类型注入-->
<property name="name" value="wwk"></property>
<!--bean注入-->
<property name="address" ref="add"></property>
<!--array注入-->
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
</array>
</property>
<!--list注入-->
<property name="hobbys">
<list>
<value>睡觉</value>
<value>吃饭</value>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="123" value="123"></entry>
<entry key="123" value="3124"></entry>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>123</value>
<value>231</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null></null>
</property>
<!--Properties类的注入-->
<property name="info">
<props>
<prop key="info">20205362</prop>
<prop key="sex">男</prop>
<prop key="username">2121</prop>
</props>
</property>
</bean>
</beans>
3 p命名空间注入
对应的是set注入一般只会用于简单类型与bean注入
xmlns:p="http://www.springframework.org/schema/p"
<bean id="add" class="Address"></bean>
<bean id="user" class="User" p:sex="男" p:name="王文凯" p:add-ref="add"></bean> //ref表示引用
就是set注入的语法糖 先导入一个p的命名空间,然后直接写 与下面比较简单了不少
<!-- 普通类型注入-->
<property name="name" value="wwk"></property>
<!--bean注入-->
<property name="address" ref="add"></property>
4 c命名空间注入
对应的是构造器注入
public class Person {
public String name;
public String sex;
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
}
xmlns:c="http://www.springframework.org/schema/c"
<bean id="people" class="Person" c:name="wwk" c:sex="男"></bean>
西导入c的约束 与下面对比简单了不少
<bean id="h" class="kevindemo.hellow">
<constructor-arg name="name" value="kk"> //这里的 不同
</constructor-arg>
</bean>
5.注意
p和c命名不能直接使用 要导入约束