一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第8天,点击查看活动详情。
一.IOC基础
1什么是IOC
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中也就是把对象创建和对象之间的调用过程,交给spring进行管理使用IOC目的:为了耦合度降低
IoC是Spring框架的核心内容,使用多种方式完美的实现了IoC,可以使用XML配置,也可以使用注解, 新版本的Spring也可以零配置实现IoC。
Spring容器在初始化时先读取配置文件,根据配置文件或元数据创建与组织对象存入容器中,程序使用 时再从Ioc容器中取出需要的对象。
采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为 一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现 控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。
2.IOC的底层原理
1.xml解析、工厂模式、反射 步骤:
- xml配置文件,配置创建的对象 <bean id="dao" class="com.spring.UserDo"></bean>
- 用service类和dao类 创建工厂
class UserFactory {
public static UserDao getDao() {
String classValue = class属性值;//xml解析
Class class = Class.forName(classValue);//通过反射创建对象
return (UserDao)class.newInstance();
}
}
3.IOC接口
1.IOC思想基于IOC容器完成,IOC容器底层就是对象工厂 2.Spring提供IOC容器是实现两种方式
- BeanFactory:IOC容器基本实现,是Spring内部的使用接口,不提供开发人员进行使用 特点:加载配置文件的时候不会创建对象,在获取对象才去创建对象
- ApplicationContext:beanFactory接口的子接口,提供更多更强大的功能,一般由开发人员进行使用 特点:加载配置文件的时候就会把在配置文件对象进行创建
3.导包
导入相关的Spring5相关的jar包
- Beans
- Core
- Context
- Expression
- logging--在Spring5的jar包中没有
3.创建IOC代码
1.创建bean配置文件,在配置文件配置中创建对象
1.bean管理
- Spring创建对象
- Spring注入属性
2.bean管理操作
- 基于xml配置文件方式实现
- 基于注解方式实现
一般我们创建普通类是以下创建的
public class User {
public void add() {
System.out.println("add....");
}
}
1. 用IOC基于xml创建 (1)Spring配置文件使用xml格式
在Spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建 配置Bean1.xml中的属性讲解
- id属性:唯一标识
- class属性:类全路径(包类路径)
<?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">
<!--配置User对象创建-->
<bean id="User" class="com.Spring.IOC.User"></bean>
</beans>
在开始前我们最好将@Test包引入以便我们好进行测试
加载配置bean文件
public class TestSpring5 {
@Test
public void testAdd(){
// 加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//获取配置创建对象
User user = context.getBean("User",User.class);
System.out.println(user);
user.add();
}
2.基于xml方式注入属性
1.DI:依赖注入,就是属性注入属性
- 第一张建立set方法注入
public class Book {
private String bname;
private String bauthor;
public void setBname(String bnaem) {
this.bname = bname;
}
public void setBauthor(String bauthor){
this.bauthor = bauthor;
}
}
xml配置
<!--set方法注入属性-->
<bean id="book" class="com.Spring.IOC.Book">
<!--使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="java开发"></property>
<property name="bauthor" value="黑马"></property>
</bean>
- 有参数构造注入
public class Orders {
private String oname;
private String oaddress;
public Orders(String oname, String oaddress) {
this.oname = oname;
this.oaddress = oaddress;
}
}
xml配置
<!--有参数构造注入属性-->
<bean id="order" class="com.Spring.IOC.Orders">
<constructor-arg name="oname" value="腊肠"></constructor-arg>
<constructor-arg name="oaddress" value="中国"></constructor-arg>
</bean>
2.p 名称空间注入 可以简化基于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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="book" class="com.Spring.IOC.Book" p:bname="Spring开发" p:bauthor="黑马">
</bean>
</beans>
java代码
public class Book {
private String bname;
private String bauthor;
@Override
public String toString() {
return "Book{" +
"bname='" + bname + '\'' +
", bauthor='" + bauthor + '\'' +
'}';
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public void setBname(String bname) {
this.bname = bname;
}
}
测试代码
@Test
public void testBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Book book = context.getBean("book",Book.class);
System.out.println(book);
book.toString();
}
3.xml注入特殊值
- 注入空值 将value单独设置为null
<bean id="book" class="com.Spring.IOC.Book">
<!--使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="java开发"></property>
<property name="bauthor">
<null></null>
</property>
</bean>
- 特殊符号的设置 可以使用转义字符或者用]]>这样写
<bean id="book" class="com.Spring.IOC.Book">
<!--使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="<java开发>"></property>
<property name="bauthor">
<value>
<![CDATA[<黑马程序员> ]]>
</value>
</property>
</bean>
4.注入属性-外部bean
- 创建两个类service类和Userdao类 UserDaoImpl类
public interface UserDao {
public void upadte();
}
public class UserDaoImpl implements UserDao {
@Override
public void upadte() {
System.out.println("dao update....");
}
}
- 在service调用dao里面的方法
public class UserService {
//创建UserDao类型属性,生产set方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
System.out.println("add Service....");
userDao.upadte();
}
}
- 配置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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.Spring.IOC.UserService">
<!--注入UserDao对象
name属性值:类里面属性名称
ref属性:创建userDao对象bean标签id值
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.Spring.dao.UserDaoImpl"></bean>
</beans>
- 测试
public void testUserDao(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
UserService userService = context.getBean("userService",UserService.class);
userService.add();
}
5.注入属性-内部bean
- 1.一对多关系:部门和员工 一个部门有多个员工,一个员工属于一个部门
- 2.实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行标识
public class Emp {
private String ename;
private String gender;
private Dept dept;
//员工属于某一个部门,使用对象表示
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public String toString() {
return "Emp{" +
"ename='" + ename + '\'' +
", gender='" + gender + '\'' +
", dept=" + dept +
'}';
}
public void add(){
System.out.println(ename+"11"+gender+".."+dept);
}
}
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"dname='" + dname + '\'' +
'}';
}
}
xml配置
<!--内部bean-->
<bean id="emp" class="com.Spring.dao.Emp">
<!--设置两个普通属性-->
<property name="ename" value="张三"></property>
<property name="gender" value="男"></property>
<!--设置对象类型属性-->
<property name="dept">
<bean id="dept" class="com.Spring.dao.Dept">
<property name="dname" value="技术部门"></property>
</bean>
</property>
</bean>
测试
@Test
public void tetsemp() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Emp emp = context.getBean("emp",Emp.class);
emp.add();
}
6.注入属性-级联赋值 第一张写法
<!--级联赋值-->
<bean id="emp" class="com.Spring.dao.Emp">
<!--设置两个普通属性-->
<property name="ename" value="张三"></property>
<property name="gender" value="男"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.Spring.dao.Dept">
<property name="dname" value="技术部"></property>
</bean>
第二种写法下面这种写法要将dept的get方法创建出来
<!--级联赋值-->
<bean id="emp" class="com.Spring.dao.Emp">
<!--设置两个普通属性-->
<property name="ename" value="张三"></property>
<property name="gender" value="男"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="技术"></property>
</bean>
<bean id="dept" class="com.Spring.dao.Dept">
<!-- <property name="dname" value="技术部"></property>-->
</bean>
3.IOC操作Bean管理,xml注入集合
java类创建
public class Stu {
//数组
private String[] courses;
//list集合
private List<String> list;
//map集合
private Map<String,String> maps;
//set集合
private Set<String> sets;
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void print(){
System.out.println(Arrays.toString(courses));
System.out.println(list);
System.out.println(maps);
System.out.println(sets);
}
}
测试类
public void tetstu() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Stu stu = context.getBean("stu", Stu.class);
stu.print();
}
xml配置各种集合的用法数组、list、Map、set
<?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="stu" class="com.Spring.Collection.Stu">
<!--数组类型注入-->
<property name="courses">
<array>
<value>java课程</value>
<value>javaweb课程</value>
</array>
</property>
<!--list类型注入-->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
</list>
</property>
<property name="maps">
<map>
<entry key="java" value="java"></entry>
<entry key="php" value="php"></entry>
</map>
</property>
<property name="sets">
<set>
<value>入门到放弃</value>
<value>一步一个台阶</value>
</set>
</property>
</bean>
</beans>
当集合为一个对象时进行xml配置,在上面的基础上加个课程类
package com.Spring.Collection;
//课程类
public class Course {
private String cname;//课程名称
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
'}';
}
}
public class Stu {
//数组
private String[] courses;
//list集合
private List<String> list;
//map集合
private Map<String,String> maps;
//set集合
private Set<String> sets;
//学生学习多个课程
private List<Course> coursesList;
public void setCoursesList(List<Course> coursesList) {
this.coursesList = coursesList;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void print(){
System.out.println(Arrays.toString(courses));
System.out.println(list);
System.out.println(maps);
System.out.println(sets);
System.out.println(coursesList);
}
}
测试类
@Test
public void tetstu() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Stu stu = context.getBean("stu", Stu.class);
stu.print();
}
xml里面添加集合对象
<!--集合类型的属性注入-->
<bean id="stu" class="com.Spring.Collection.Stu">
<!--数组类型注入-->
<property name="courses">
<array>
<value>java课程</value>
<value>javaweb课程</value>
</array>
</property>
<!--list类型注入-->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
</list>
</property>
<property name="maps">
<map>
<entry key="java" value="java"></entry>
<entry key="php" value="php"></entry>
</map>
</property>
<property name="sets">
<set>
<value>入门到放弃</value>
<value>一步一个台阶</value>
</set>
</property>
<!--注入List集合对象-->
<property name="coursesList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!--创建多个course对象-->
<bean id="course1" class="com.Spring.Collection.Course">
<property name="cname" value="Spring 框架"></property>
</bean>
<bean id="course2" class="com.Spring.Collection.Course">
<property name="cname" value="MyBatis框架"></property>
</bean>
将集合注入部分提取出来 先创建集合类
public class Book {
private List<String> bname;
@Override
public String toString() {
return "Book{" +
"bname='" + bname + '\'' +
'}';
}
public void setBname(List<String> bname) {
this.bname = bname;
}
}
xml进行集合提取配置使用util标签
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--提取List集合类型属性注入-->
<util:list id="booklist">
<value>java开发</value>
<value>php开发</value>
</util:list>
<!--提取List集合类型属性注入使用-->
<bean id="book" class="com.Spring.IOC.Book">
<property name="bname" ref="booklist"></property>
</bean>
</beans>
测试
@Test
public void testBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Book book = context.getBean("book",Book.class);
System.out.println(book);
book.toString();
}
Properties注入
<property name="info">
<props>
<prop key="学号">20190604</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>