什么是Spring 的IOC ?
IOC (Inverse of Control )控制反转的概念就是将原本在java程序中手动创建UserService对象的控制权交给Spring框架进行管理,简单的说,就是将创建UserService对象的控制全被反转到了Spring框架
Spring 的IOC的底层原理实现如下图:
Spring IOC 开发步骤:
idea 创建一个新的项目 File --> new Project
勾选 Create from archetype ----> webapp ,选好后点击 --> next
输入项目名、项目存放路径、包名等信息,点击 --> next
选择本地的maven仓库路径,选好后点击 -->next
创建好的项目目录结构如下:
pom.xml文件<dependencys></dependencys>元素中间添加的依赖包如下:
<!--junit 测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!--Spring 的基础包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- logging-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
创建java 类文件如下:
代码如下:
package com.demo.ioc.service;
public interface UserService {
public void sayHello();
}
package com.demo.ioc.service.impl;
import com.demo.ioc.service.UserService;
public class UserServiceImpl implements UserService {
private String name;
@Override
public void sayHello() {
System.out.println("Hello "+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
创建测试类:
import com.demo.ioc.service.UserService;
import com.demo.ioc.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo1 {
@Test
/**
* 传统开发
*/
public void demo1(){
//UserService userService = new UserServiceImpl();
UserServiceImpl userServiceImpl = new UserServiceImpl();
userServiceImpl.setName("宋阿南");
userServiceImpl.sayHello();
}
@Test
/**
* Spring的方式的实现
*/
public void demo2(){
//创建Spring 的工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类:
UserService userService = (UserService) applicationContext.getBean("userService");
userService.sayHello();
}
}
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">
<!-- UserServiceImpl 的创建源交给了Spring-->
<bean id="userService" class="com.demo.ioc.service.impl.UserServiceImpl">
<!--设置属性-->
<property name="name" value="spring_ioc"></property>
</bean>
</beans>
项目整体的目录结构如下:
demo1测试运行结果如下:
demo2测试结果如下: