Spring是一个基于IOC和AOP的结构JAVAEE系统的框架
IOC 反转控制 是Spring的基础,Inversion Of Control简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象
DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。
练习代码
maven依赖(建立maven工程,向pom里倒入下列依赖)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
package cn.why.pojo;
/**
* Title: Category
* Description:
* Company: AceGear
* Author: henrywang
* Date: 2018/8/2
* JDK: 8
* Encoding: UTF-8
*/
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package cn.why.pojo;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Title: Product
* Description:
* Company: AceGear
* Author: henrywang
* Date: 2018/8/2
* JDK: 8
* Encoding: UTF-8
*/
public class Product {
private int id;
private String name;
@Autowired
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
package cn.why.test;
import cn.why.pojo.Category;
import cn.why.pojo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Title: TestSpring
* Description:
* Company: AceGear
* Author: henrywang
* Date: 2018/8/2
* JDK: 8
* Encoding: UTF-8
*/
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext1.xml"});
Category c = (Category) context.getBean("c");
System.out.println(c.getId()+" :: "+c.getName());
System.out.println("=============");
Product p = (Product) context.getBean("p");
System.out.println(p.getId()+" "+p.getName()+" "+p.getCategory().getId()+" "+p.getCategory().getName());
}
}
applicationContext1.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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean name="c" class="cn.why.pojo.Category">
<property name="name" value="category 1"/>
<property name="id" value="10"/>
</bean>
<bean name="p" class="cn.why.pojo.Product">
<property name="name" value="product1"/>
<!--<property name="category" ref="c"/>-->
</bean>
</beans>
整体结构图(隐藏部分为暂时用不到的部分)
先无脑运行上述代码,输出为
10 :: category 1
=============
0 product1 10 category 1
问题一:IOC与传统创建对象的方式有何区别
传统方式 Category c = new Category();
IOC Category c = (Spring产生)
打个比喻:
传统方式:相当于你自己去菜市场new 了一只鸡,不过是生鸡,要自己拔毛,去内脏,再上花椒,酱油,烤制,经过各种工序之后,才可以食用。
用 IOC:相当于去馆子(Spring)点了一只鸡,交到你手上的时候,已经五味俱全,你就只管吃就行了。
问题二:DI与传统赋值取值有何区别
传统方式 Category c = new Category();
c.setXX();
c.getXX();
DI 赋值方式在applicationContext里键入
取值方式 Product p = (Product) context.getBean("p"
);
System.out.println(p.getName());