IOC控制反转(IoC,Inversion of Control), 是一个概念,是一种思想。控制反转就是对对象控制权的转移,从程序代码自己反转到了外部容器。把对象的建立、初始化、 销毁等工做交给spring容器来作。由spring容器控制对象的生命周期。 DI依赖注入:Dependency Injection。 依赖注入DI是指程序运行过程当中,若须要调用另外一个对象协助时,无须在代码中建立被调用者,而是依赖于外部容器,由外部容器建立后传递给程序。依赖注入是目前最优秀的解耦方式。依赖注入让Spring的Bean之间以配置文件的方式组织在一块儿,而不是以硬编码的方式耦合在一块儿的。 IoC与DI的关系 IoC是一个概念,是一种思想,其实现方式多种多样。当前比较流行的实现方式之一是DI。 IOC:控制反转, 将 new 的过程交给spring容器去处理java
<?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">
</beans>
复制代码
3.. 在Spring的配置文件中声明User Bean: spring
<?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">
<!-- 在容器中注册UserBean -->
<bean class="com.dpb.javabean.UserBean" id="userBean"></bean>
</beans>
复制代码
所谓的声明,就是将本身的信息告诉Spring容器,例如id和class,Spring容器根据class,经过反射(默认使用的是无参构造方法)就能够建立一个名为user1的User对象。数组
public static void main(String[] args) {
//初始化Spring容器,当Spring容器初始化时,会自动加载配置文件,而后根据配置文件中的内容初始化Bean
ApplicationContext ac =
new ClassPathXmlApplicationContext("application.xml");
}
复制代码
public static void main(String[] args) {
//初始化Spring容器,当Spring容器初始化时,会自动加载配置文件,而后根据配置文件中的内容初始化Bean
ApplicationContext ac =
new ClassPathXmlApplicationContext("application.xml");
// 去Spring容器中获取一个UserBean对象
UserBean user = ac.getBean("userBean", UserBean.class);
System.out.println(user);
}
复制代码
public static void main(String[] args) {
//初始化Spring容器,当Spring容器初始化时,会自动加载配置文件,而后根据配置文件中的内容初始化Bean
ApplicationContext ac =
new ClassPathXmlApplicationContext("application.xml");
// 去Spring容器中获取一个UserBean对象 经过类型直接获取
UserBean user = ac.getBean( UserBean.class);
System.out.println(user);
}
复制代码
这种方式有潜在的隐患:若是Spring容器中有多个User的实例,此时就会报错 bash
实际开发过程当中咱们能够忽略id和name的区别。能够混合使用。经过getBean()方法均可以获取,这个是个重载的方法。app
id="user1,user2,user3"
表示bean有一个名字,这个名字就是user1,user2,user3
name="user1,user2,user3"
表示bean有多个名字,多个名字分别是user一、user2以及user3
复制代码
在同一配置文件中ID不要出现重复的。框架
/**
* ApplicationContext 方式加载
*/
@Test
public void test1() {
// 建立容器的同时 容器初始化,容器全部的bean建立完毕
ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
// 去Spring容器中获取一个UserBean对象 经过类型直接获取
UserBean user = ac.getBean(UserBean.class);
System.out.println(user);
}
/**
* BeanFactory 方式加载
*/
@Test
public void test2() {
// 建立容器对象,BeanFactory当调用getBean获取响应对象是才建立对象
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("application.xml"));
// 去Spring容器中获取一个UserBean对象 经过类型直接获取
UserBean user = bf.getBean(UserBean.class);
System.out.println(user);
}
复制代码
/**
* User 工厂类
* @author dpb[波波烤鸭]
*
*/
public class UserFactory {
/**
* 必须是static方法
* @return
*/
public static UserBean getInstance(){
return new UserBean();
}
}
复制代码
application.xml文件中注册ide
<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 class="com.dpb.factory.UserFactory" factory-method="getInstance" id="user2"/>
</beans>
复制代码
获取相应的实例 测试
在一些第三方框架使用过程 中,可能不得不使用静态工厂注入或者实例工厂注入。ui
HttpUrlConnection
HttpClient
OkHttp
复制代码
这里以OkHttp为例说明为什么须要静态工厂注入 因为OkHttpClient须要经过Builder进行建立,所以没法直接使用构造方法注入。此时能够经过静态工厂注入。this
/**
* User 工厂类
* @author dpb[波波烤鸭]
*
*/
public class UserFactory {
/**
* 动态工厂方式获取
* 普通方法
* @return
*/
public UserBean getInstance(){
return new UserBean();
}
}
复制代码
<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 -->
<bean class="com.dpb.factory.UserFactory" id="userFactory"/>
<!-- 从工厂中获取UserBean对象 -->
<bean id="user" factory-bean="userFactory" factory-method="getInstance"/>
</beans>
复制代码
属性注入主要是指如何给对象中的属性赋值
<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">
<!-- 注册UserBean -->
<bean class="com.dpb.javabean.UserBean" id="userBean">
<!-- 经过构造注入设置 -->
<constructor-arg name="id" value="1"/>
<constructor-arg name="name" value="波波烤鸭"/>
<constructor-arg name="age" value="18"/>
</bean>
</beans>
复制代码
<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">
<!-- 注册UserBean -->
<bean class="com.dpb.javabean.UserBean" id="userBean">
<!-- 经过构造注入设置 经过index 定位设置 -->
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="波波烤鸭"/>
<constructor-arg index="2" value="18"/>
</bean>
</beans>
复制代码
测试结果
有参构造方法不是必须的了,无参方法是必须的!!! set方法注入就是利用对象属性的set方法给属性赋值,实际上,至关于首先使用无参构造方法建立一个Book对象,而后调用对象中的set方法给各个属性赋值。
/**
*
* @author dpb[波波烤鸭]
*
*/
public class UserBean {
private int id;
private String name;
private int age;
/**
* 无参构造方法
*/
public UserBean() {
System.out.println("无参构造方法");
}
public int getId() {
return id;
}
/**
* 设值注入 必须提供对应的setter方法
* @param id
*/
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
/**
* 设值注入 必须提供对应的setter方法
* @param name
*/
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
/**
* 设值注入 必须提供对应的setter方法
* @param age
*/
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "UserBean [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public void say(){
System.out.println("hello ...");
}
}
复制代码
<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">
<!-- 注册UserBean -->
<bean class="com.dpb.javabean.UserBean" id="userBean">
<!--经过设值注入的方式注入 -->
<property name="id" value="2"/>
<property name="name" value="bobo烤鸭"/>
<property name="age" value="32"/>
</bean>
</beans>
复制代码
p名称空间注入本质上仍是set方法注入,只是写法不一样(注意:p名称空间注入,须要有无参构造方法)。
/**
* p名称空间注入
* @author dpb[波波烤鸭]
*
*/
public class Person {
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", address=" + address + "]";
}
}
复制代码
<!-- 注册 Person -->
<bean id="person" class="com.dpb.javabean.Person" p:id="3" p:name="邓澎波" p:address="深圳"/>
复制代码
这种注入方式了解便可,实际开发中使用较少。
对象能够经过构造方法、set方法或者p名称空间注入,步骤以下:
/**
* 学生
* @author dpb[波波烤鸭]
*
*/
public class Student {
private int id;
private String name;
// 拥有的 cat
private Cat cat;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", cat=" + cat + "]";
}
}
复制代码
/**
* 猫
* @author dpb[波波烤鸭]
*
*/
public class Cat {
private int id;
private String color;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Cat [id=" + id + ", color=" + color + "]";
}
}
复制代码
<!-- 配置 cat -->
<bean id="catId" class="com.dpb.javabean.Cat">
<property name="id" value="1"/>
<property name="color" value="red"></property>
</bean>
<!-- 配置 student对象 -->
<bean class="com.dpb.javabean.Student">
<property name="id" value="10"/>
<property name="name" value="波波烤鸭"/>
<!-- 对象注入 -->
<property name="cat" ref="catId"></property>
</bean>
复制代码
数组和集合的注入方式是一致的,不管是基本数据类型仍是一个引用,注入方式都是同样。 首先声明一个对象,对象中包含集合和数组
<!-- 配置 student对象 -->
<bean class="com.dpb.javabean.Student">
<property name="id" value="10"/>
<property name="name" value="波波烤鸭"/>
<!-- 对象注入 -->
<property name="cat" ref="catId"></property>
<!-- List集合注入 -->
<property name="games">
<list>
<value>LOL</value>
<value>DNF</value>
<value>CS</value>
</list>
</property>
<!-- 数组注入 -->
<property name="books">
<list>
<bean class="com.dpb.javabean.Book">
<property name="id" value="1001"/>
<property name="bookName" value="西游记"/>
</bean>
<bean class="com.dpb.javabean.Book">
<property name="id" value="1002"/>
<property name="bookName" value="红楼梦"/>
</bean>
</list>
</property>
</bean>
复制代码
声明Map属性,注意属性的key和value的数据类型须要提早定义好,而后在xml文件中直接使用(xml文件中配置时,key和属性的值必需要知足声明的要求,不然就会出错)。
<property name="score">
<map>
<entry key="数学" value="99"/>
<entry key="英语" value="78"/>
<entry key="化学" value="84"/>
</map>
</property>
复制代码
properties注入与map注入相似
<property name="props">
<props>
<prop key="userName">admin</prop>
<prop key="password">123</prop>
</props>
</property>
复制代码