Spring . IOC
1. IOC理论推导
1.UserDao接口
public interface UserDao {
void getUser();
}
2.UserDaoImpl接口实现类
public class UserDaoImpl implements UserDao{
@Override
public void getUser() {
System.out.println("用户获取数据...");
}
}
public class UserDaoMysqlImpl implements UserDao{
@Override
public void getUser() {
System.out.println("Mysql");
}
}
3.UserServices业务接口
public interface UserServices {
void getUser();
}
4.UserServicesImpl业务接口实现类
- 程序猿掌握主动权,程序主动创建对象(需要根据用户的需求修改底层代码,十分麻烦!!)
public class UserServicesImpl implements UserServices{
//根据需要使用的业务创建对象
private UserDao userDao = new UserDaoImpl();
public void getUser() {
userDao.getUser();
}
}
- 将控制权交给用户,根据用户需求,程序被动的创建对象(使用set结构实现)
public class UserServicesImpl implements UserServices{
private UserDao userDao;
//使用set方法动态的实现值的注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void getUser() {
userDao.getUser();
}
}
- 使用set方法的好处就是程序猿不再需要去管理对象的创建,全部交给用户端的需求,被动创建。
- 系统的耦合性大大的降低,程序猿可以更加卖力与业务动能的开发(真是头秃呀~)。
- 这种思想是IOC的原型。
2. IOC的本质
用结构图理解下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bsZUxrSX-1631806811423)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210909230645668.png)]
**控制反转(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IOC的一种方法。**没有IOC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬解码在程序中,对象的创建由程序控制,控制反转后对象的创建移交于第三方,即获得依赖对象的方式反转了。
采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式把二者合二为一。Bean的定义信息直接以注解的形式定义在实现类中,实现零配置的目的。
控制反转是一种通过描述(xml或者注解)并通过第三方去生产或者获取特定对象的方式。在Spring中实现控制反转的是IOC容器,其实现方法是依赖注入(Denpendency injection,DI)。
3. HelloSpring
1.pojo的Hello类
package pojo;
public class Hello {
private String name;
public String getName() {
return name;
}
//依赖注入使用set的实现方式
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Hello{" +
"name='" + name + '\'' +
'}';
}
}
2.配置文件beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring来创建对象,在Spring中对象称为bean
类型 对象名 = new 类型();
id = 对象名;
class = new的类型
property:给对象中的属性赋值
-->
<bean id="hello" class="pojo.Hello">
<property name="name" value="spring"/>
</bean>
</beans>
3.测试文件TestMain
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Hello;
public class TestMain {
public static void main(String[] args) {
//获取ApplicationContext,获取Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//从Spring容器获取对象(容器在手,天下我有!!)
Hello hello = context.getBean("hello", Hello.class);
System.out.println(hello.getName());
}
}
上面这整个过程就叫做控制反转(还有点蒙圈中o(╥﹏╥)o…)
- 控制:反转前程序的对象创建由程序控制,使用Spring容器后,对象变为由Spring创建。
- 反转:程序本身不再创建对象,而变为被动的接受对象(狂神老师解释的很到胃啊~)
- 依赖注入:使用set的方式实现注入。
- IOC是一种编程思想,由主动的编程变成被动的接收。
- 总结下来IOC可以说解放了程序猿的双手,不再去进行底层代码的修改,要实现不同的操作,只需要进配置文件中进行修改
- 一句话概括:对象由Spring来创建、管理、装配!!
4.IOC创建对象的方式
1.无参构造方法,默认!
2.有参构造方法,有以下三种方式:
-
<!-- 第一种创建对象的方式,通过下标赋值 --> <!--<bean id="user" class="pojo.User"> <constructor-arg index="0" value="my love tc"/> </bean>-->
-
<!-- 第二种方式,通过相对应的类型,这种方式不推荐,若构造器有两个相同类型参数需要赋值就会出现问题 --> <!--<bean id="user" class="pojo.User"> <constructor-arg type="java.lang.String" value="kuang shen teach java"/> </bean>-->
-
<!-- 第三种方式,直接通过参数赋值,最常用 --> <bean id="user" class="pojo.User"> <constructor-arg name="name" value="tc love"/> </bean>
总结:在配置文件加载的时候,容器中的对象已经初始化完成!!
5. Spring的配置方式
5.1 bean的配置方式
<!--
id:bean的唯一标识符,类似对象的名字
class:bean对象对应的权限定名:包名 + 类名,类似对象的类型
name:也是别名,而且name可以同时取多个别名
-->
<bean id="userT" class="pojo.UserT" name="userT2 u2,asdf;sjkda">
<constructor-arg name="name" value="hzl"/>
</bean>
5.2 import的使用
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
将多个配置文件整合到一个配置文件中方便调用。
6. 依赖注入
6.1 构造器注入:略
6.2 Set注入
- 依赖注入:
- 依赖:bean对象的创建依赖于容器。
- 注入:bean对象的所有属性,由容器来注入。
1.复杂类型
package pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
2.真实测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> L1;
private Map<String,String> M1;
private Set<String> S1;
private String wife;
private Properties info;
3.beans.xml
<?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>
4.测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Student;
public class TestMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
5.配置文件中注入全部信息
<?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="address" class="pojo.Address">
<property name="address" value="贵阳"/>
</bean>
<bean id="student" class="pojo.Student">
<property name="name" value="hzl"/>
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>回到过去</value>
<value>钢铁是怎样炼成的</value>
<value>简爱</value>
</array>
</property>
<!--list注入-->
<property name="l1">
<array>
<value>唱歌</value>
<value>吉他</value>
<value>打炮</value>
</array>
</property>
<!--map注入-->
<property name="m1">
<map>
<entry key="身份证" value="350322164815482456"/>
<entry key="银行卡" value="6503221648153482456"/>
</map>
</property>
<!--set注入-->
<property name="s1">
<set>
<value>LOL</value>
<value>DNF</value>
<value>王者荣耀</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null></null>
</property>
<!--prop注入-->
<property name="info">
<props>
<prop key="driver">2018022099</prop>
<prop key="url">贵阳</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
6.3 其他注入方式
p注入与c注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p注入,可以直接给属性赋值-->
<bean id="user" class="pojo.User" p:name="hzl" p:age="27"/>
<!--c注入,通过有参构造器赋值-->
<bean id="user2" class="pojo.User" p:name="tc" p:age="24"/>
</beans>
- 注意,p注入与c注入需要导入约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
6.4 bean的作用域
1.单例模式(Spring默认机制)
<bean id="user" class="pojo.User" p:name="hzl" p:age="27" scope="singleton"/>
2.原始模式(每次从容器中get的时候,都会产生新的对象)
<bean id="user2" class="pojo.User" p:name="tc" p:age="24" scope="prototype"/>
3.其余的request、session、application只能在web开发中使用!
7. Bean的自动装配
1.Byname自动装配
<bean id="dog" class="pojo.Dog"/>
<bean id="cat" class="pojo.Cat"/>
<bean id="personal" class="Personal" autowire="byname">
<property name="name" value="小灿灿"/>
</bean>
byname:会自动在容器的上下文中寻找,和自对象set方法后面的值相对应的beanid!
- byname的时候,需要保证所有bean的id唯一,并且这个bean要和自动注入的属性的set方法的值一致!
2.Bytape自动装配
<bean class="pojo.Dog"/>
<bean class="pojo.Cat"/>
<bean id="personal" class="Personal" autowire="byType">
<property name="name" value="小灿灿"/>
</bean>
bytape:会自动在容器的上下文寻找,和自己对象属性类型相对应的bean!
- bytape的时候,需要保证所有的bean的class唯一,并且这个bean要和自动注入的属性的类型一致!
ame=“name” value=“小灿灿”/>
byname:会自动在容器的上下文中寻找,和自对象set方法后面的值相对应的beanid!
- byname的时候,需要保证所有bean的id唯一,并且这个bean要和自动注入的属性的set方法的值一致!
2.Bytape自动装配
```xml
<bean class="pojo.Dog"/>
<bean class="pojo.Cat"/>
<bean id="personal" class="Personal" autowire="byType">
<property name="name" value="小灿灿"/>
</bean>
bytape:会自动在容器的上下文寻找,和自己对象属性类型相对应的bean!
- bytape的时候,需要保证所有的bean的class唯一,并且这个bean要和自动注入的属性的类型一致!