什么是Spring框架:
Spring是一个基于IOC和AOP的结构J2EE系统的框架:
IOC 反转控制 是Spring的基础 ,Inversion Of Control, 简单说就是创建对象由以前的程序员自己 new 构造方法来调用,变成了交由Spring创建对象;
DI 依赖注入 Dependency Inject, 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接 使用即可。
原理分析:
以获取对象的方式来进行比较
传统的方式:
通过new 关键字主动创建一个对象。
IOC方式:
对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。 IOC是
反转
(Inversion Of Control)的缩写,就像控制权从本来在自己手里,交给了Spring。
打个比喻:
传统方式: 相当于你自己去菜市场new 了一只鸡,不过是生鸡,要自己拔毛,去内脏,再上
花椒,酱油,烤制,经过各种工序之后,才可以食用。
用 IOC: 相当于去馆子(Spring)点了一只鸡,交到你手上的时候,已经五味俱全,你就只管
吃就行了。
接下来通过运行TestSpring演示如何用Spring获取一个对象,并打印其name 以及使用IOC的方式,对Product对象,注入一个Category对象
(创建项目以及导包过程已省略, 关于spring项目中需要的jar包介绍 , 请点击此处 )
准备pojo Category 、Product,用来演示IOC和DI:
package com.spring.pojo;
public class 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;
}
private int id;
private String name;
}
package com.spring.pojo;
public class Product {
private int id;
private String name;
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;
}
}
创建applicationContext.xml:
在src目录下新建applicationContext.xml文件;
applicationContext.xml是Spring的核心配置文件, 通过关键字p即可获取
Product 对象(IOC过程) , 该对象获取的时候,即被注入了字符串"product 1“到name属性中 (DI过程) 。
在创建Product的时候注入一个Category对象,注意, 这里要使用 ref 来注入另一个对象 (DI过程)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.spring.pojo.Category">
<property name="name" value="category 1" />
</bean>
<
bean name="p" class="com.how2java.pojo.Product">
<
property
name
=
"name"
value
=
"product1"
/>
<!–使用ref注入category对象–>
<property name="category" ref="c" />
</bean>
</beans>
TestSpring:
测试代码,演示通过spring获取product对象,以及该对象被注入的name和category属性。如图所示,可以打印
出通过Spring拿到的Product对象的name属性和Category对象的name属性。
package com.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.pojo.Product;
public class TestSpring {
public static void main(String[] args) {
//读取配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
//通过关键字“c”,从spring中获取Category对象
Product p = (Product) context.getBean("p");
//p.getName()获取p对象中属性name的值,p.getCategory().getName()获取category的name值
System.out.println(p.getName());
System.out.println(p.getCategory().getName());
}
}
注:读取配置文件:
//方式一:(只能读取一个配置文件)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//方式二:(可读取一个或多个配置文件)
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
原文地址: https://how2j.cn/k/spring/spring-injection/88.html#nowhere