Spring实例"IOC"——动物吃水果
Spring学习
1、为什么使用Spring?
以往的编程中,当你需要一个对象时,你会自己new
对象。例如当界面点击提交学生成绩按钮时,学生成绩会提交到java
类中,而java
类是如何存储提交的学生成绩的?我们需要new
一个学生成绩的对象,将界面提交的成绩信息保存到对象的属性中。
Score s = new Score();
s.setTotalscore(67);//成绩
s.setStuno("1234567890");//学号
这种做法你会发现什么问题?问题是你需要对象时就自己new
对象。使得当需求变更导致更好对象时,需要修改源代码,进而导致整个项目需要重新编译、测试、部署。
2、如何改进呢?
使用spring
可以改变这一切,spring
是一个大容器,里面可以装很多java
对象,当程序需要对象时,从spring
容器中获取所需要对象,进而使得程序更换对象时,无需修改源代码,只需要更改配置文件即可。就像你玩游戏前更改游戏设置一样。
一、问题:在控制台打印XX(动物)吃XX(水果)
当然了不可能如下情况:
public class Tests {
public static void main(String[] args) {
System.out.println("猴子吃香蕉。");
}
}
二、解决方法
2.1、常规方法(new对象)
Dog.java
public class Dog {
public void eat(String food) {
System.out.println("狗在吃"+food);
}
}
Cat.java
public class Cat {
public void eat(String food) {
System.out.println("猫在吃"+food);
}
}
Test.java
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat("葡萄");
/*
* Cat cat = new Cat(); cat.eat("葡萄");
*/
}
}
结果展示:
2.2、Spring-ioc(控制反转)
1、搭建开发环境:
(1)、创建maven项目;
(2)、导入jar包(向pow.xml文件中添加jar包);
2、业务开发:
(1)、添加实体类(在src/main/java创建包,在包中创建实体类);
(2)、添加并设置spring配置文件(在src/main/resource中添加);
3、单元测试
创建测试类(在src/main/test创建测试包,在包中创建测试类)。
(一)、搭建开发环境
1.1、创建Maven项目
1.2、导入jar包(pom.xml)
所需要的jar包可以从Maven仓库中获取。
spring
JUnit
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>luas</groupId>
<artifactId>edtFruit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(二)、业务开发
2.1、创建实体类
Dog.java
package cn.edu.model;
public class Dog {
public void eatFuit(String food) {
System.out.println("狗在吃"+food);
}
}
Cat.java
package cn.edu.model;
public class Cat {
public void eatFuit(String food) {
System.out.println("猫在吃"+food);
}
}
2.1、创建spring配置文件
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--
定义一个bean相当于告诉spring创建一个对象
下行代码相当于Dog dog = new Dog();
-->
<bean id="dog" class="cn.edu.model.Dog"></bean>
<bean id="cat" class="cn.edu.model.Cat"></bean>
</beans>
(三)、单元测试
package cn.edu.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.edu.model.Dog;
import cn.edu.model.Cat;
public class Tests {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Dog dog = (Dog) ctx.getBean("dog");
dog.eatFuit("葡萄");
}
@Test
public void eatFruit() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Cat cat = (Cat) ctx.getBean("dog");
cat.eatFuit("葡萄");
}
}
结果展示:
三、Spring运行的思路
Spring运行的思路,见上图。思路是这样的,在程序运行之前,把所有需要new的对象在spring配置文件中定义好。Spring会自动new配置文件中定义的对象,(例如cat对象和dag对象),将new好的对象放到Spring容器中。程序在运行期间如果需要对象,就从spring容器中获取,而不是自己new对象。
介绍一个重要的概念:IoC 控制反转
如前所述,Ioc是Inversion of Control,是控制反转的意思。其中的控制是指谁来创建对象,即谁来new对象。反转的意思是将对象的创建权限转移出去。
例如
Dog dog = new Dog();表示谁用dog对象,谁就自己new出dog对象。
而反转后是这样的
<bean id="dog" class="edu.luas.media.spring.demo1.Dog"></bean>
这个配置是由spring来new出的Dog对象。当程序需要dog对象时,从spring容器中可以获取到dog对象。
也就是说使用对象的程序把创建对象的权利转移到了sping容器中。