前言:
什么是IOC Inverse of Control(控制反转)是⼀种设计思想 将原本在程序中⼿动创建对象的流
程,交由Spring框架来管理 核⼼:把创建对象的控制权反转给Spring框架,对象的⽣命周期由
Spring统⼀管理
把spring ioc 当成⼀个容器,⾥⾯存储管理的对象称为Bean,类实例
目录:
步骤一:
创建maven项目即可
步骤二:
在pom.xml中配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.xdclass</groupId>
<artifactId>xd_spring</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
</dependencies>
</project>
步骤三:在applicationContext中配置:
其中ref用到了DI对象依赖,videoorder这个bean对象依赖video对象
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="video" class="net.xdclass.sp.domain.Video">
<property name="id" value="9"/>
<property name="title" value="Spring 5.X课程" />
</bean>
<bean id="videoOrder" class="net.xdclass.sp.domain.VideoOrder" >
<property name="id" value="8" />
<property name="outTradeNo" value="23432fnfwedwefqwef2"/>
<property name="video" ref="video"/>
</bean>
</beans>
附上: