Spring IOC&DI
1、什么是IOC与DI?
IOC:(Inversion Of Control)即控制反转。不是什么技术而是一种思想,在java开发中,IOC意味将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。对于使用对象而言,不是主动请求而是被动接受。
DI:(Dependency Injection)即依赖注入。组件之间的依赖关系由容器运行期间决定,形象的说,即容器动态的将某个依赖关系动态注入到组件中。
2、IOC与DI有什么用?
例:我们,美团外卖,商家
首先,我们要到美团外卖定餐与商家要做生意的话,就必须要到美团外卖注册一个账号(相当于Bean的注入),这样我们才能通过美团外卖吃到饭与商家才能做生意。
传统的吃饭方式是,我们必须亲自,主动到店里面点餐吃饭,如果你不去的话就吃不到饭(相当于传统的new对象)。
而你通过美团外卖点餐,只要你在规定的送餐范围内,选择自己喜欢的商家(建立依赖),提交订单(依赖注入),商家就会给你送来,然后你吃饭。(IOC与DI的体现)
这样的好处在于:一是可以对我们和商家统一管理,实现资源的可配置和易管理。比如:在美团外卖我们能知道哪些商家在休息,哪些在营业,有什么优惠活动等等,让我们可以吃到满意的饭菜,商家能生意兴隆。
二是使我们和商家的直接联系降低了,也符合软件设计高内聚低耦合的原则。要是不用美团外卖,你直接去吃饭的话,有可能商家今天休息了呢?岂不是白跑了。(没有new就直接使用会出现NULL异常咯)
3、怎么用DI?
注:有很多种注入方法,这里只是演示比较常用的两种
A:构造器注入->实际上是在配置文件中动态的给类的属性赋值
B:setter注入->调用set方法进行动态赋值
A:构造器注入
a:建立实体类
public class Student {
private String stu_name;
private int id;
private int stu_age;
//有参构造
public Student(String stu_name, int id, int stu_age) {
super();
this.stu_name = stu_name;
this.id = id;
this.stu_age = stu_age;
}
//打印信息方法
public void print(){
System.out.println(this.getStu_age());
System.out.println(this.getId());
System.out.println(this.getStu_name());
}
//无参构造
public Student() {
super();
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStu_age() {
return stu_age;
}
public void setStu_age(int stu_age) {
this.stu_age = stu_age;
}
}
b:配置applicationContext.xml文件
c:测试
public class Test {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = context.getBean("student",Student.class);
stu.print();
}
}
d:测试结果
B:setter注入
a:setter注入与构造器注入其他地方没变,主要是applicationContext.xml配置文件中的配置不同。Bean类必须有要注入属性的set方法
4、注入类型
A:配置文件applicationContext.xml配置
1
2
3
4
5
6
7
9
10
8
11
12
com.mysql.driver
root
root
B:实体类
a:学生实体类
/**
* 学生实体类
*
* @author Administrator
*
*/
public class Student {
private final String HELLO = null;
private String stuName;
private int stuAge;
private String stu_gender = "1";
private List
list;
private Set
set;
private int array[];
private int arrays[][];
private Map
map;
private Properties prop;
private Teacher teacher;
public void print() {
System.out.println(this.stuName);
System.out.println(this.stuAge);
for (String str : list) {
System.out.print(str + "\t");
}
System.out.println();
for (String str1 : set) {
System.out.print(str1 + "\t");
}
System.out.println();
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "\t");
}
System.out.println();
for (int j = 0; j < arrays.length; j++) {
for (int k = 0; k < arrays[j].length; k++) {
System.out.print(arrays[j][k] + "\t");
}
}
System.out.println();
System.out.println(map);
System.out.println();
System.out.println(prop.getProperty("drivers"));
System.out.println(prop.getProperty("user"));
System.out.println(prop.getProperty("password"));
System.out.println();
System.out.println(HELLO);
teacher.sayHello();
System.out.println(this.stu_gender);
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public void setStu_gender(String stu_gender) {
this.stu_gender = stu_gender;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public void setProp(Properties prop) {
this.prop = prop;
}
public void setMap(Map
map) {
this.map = map;
}
public void setArrays(int[][] arrays) {
this.arrays = arrays;
}
public void setArray(int[] array) {
this.array = array;
}
public void setSet(Set
set) { this.set = set; } public void setList(List
list) { this.list = list; } public Student(String stuName, int stuAge) { super(); this.stuName = stuName; this.stuAge = stuAge; } public Student(Teacher teacher) { this.teacher = teacher; } public Student() { super(); } }
b:教师实体类
/**
* 教师类
* @author Administrator
*
*/
public class Teacher {
private String teaName;
public void sayHello(){
System.out.println("hello!");
}
public String getTeaName() {
return teaName;
}
public void setTeaName(String teaName) {
this.teaName = teaName;
}
public Teacher(String teaName) {
super();
this.teaName = teaName;
}
public Teacher() {
super();
}
}
D:测试类
public class Test {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = context.getBean("student",Student.class);
stu.print();
}
}
E:测试结果
F:整体测试结构(jdbc.properties文件中的内容为:drivers=com.mysql.driver user=root password=root)
5、使用注解注入
A:/**
* hello接口
* @author Administrator
*
*/
public interface Hello {
void say();
}
B:/**
* Hello接口子类
* @author Administrator
*
*/
@Component("hello1")
public class Hello1 implements Hello{
@Override
public void say() {
System.out.println("hello spring...");
}
}
C:/**
* 实体类
* @author Administrator
*
*/
@Component
public class World {
//表示自动注入,如果要指定注入,下面两个注解必须一起使用
@Autowired
@Qualifier("hello1")
private Hello hello;
public void sayHello(){
hello.say();
}
}
D:/**
* 测试类
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
World world = context.getBean(World.class);
world.sayHello();
}
}
E:配置文件需加入自动扫描
<context:annotation-config />
<context:component-scan base-package="cn.gson.*" />
F:测试结果