Spring-IOC推导(二)

IOC推导

目的:解决企业应用开发的复杂性

功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能

范围:任何Java应用

Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。

分析实现

我们使用原来的方式写一段代码测试:dao--service---前端

思考:现在前端传递或者调用不会变,所有操作都是我们程序猿来实现;

解决方案:前端操作,后台不变;留一个调用的接口

IOC的原型。

IOC的本质

控制反转 (inversion of Control):IOC

他是一种设计思想。 根据这个思想有实现的方式,DI : 依赖注入

本质就是设置接口 , set方法,是一种抽象思维

反转: 从被动到主动 , 程序猿主动控制 到 被动接受 , 由编写代码,编写接口,程序具有高度配置性和动态性;

让程序的耦合性大大降低。方便模块独立:----> 微服务。解耦

3. HelloSpring


项目的整体结构:

1.首先,我们用maven创建一个项目

导入Spring 的jar包

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>

2.创建实体类(pojo)

 1 package org.west.pojo;
 2 
 3 public class Student {
 4     private String name;
 5     private int age;
 6 
 7     public Student() {
 8     }
 9 
10     public Student(String name, int age) {
11         this.name = name;
12         this.age = age;
13     }
14 
15     public String getName() {
16         return name;
17     }
18 
19     public void setName(String name) {
20         this.name = name;
21     }
22 
23     public int getAge() {
24         return age;
25     }
26 
27     public void setAge(int age) {
28         this.age = age;
29     }
30 
31     @Override
32     public String toString() {
33         return "Student{" +
34                 "name='" + name + '\'' +
35                 ", age=" + age +
36                 '}';
37     }
38 }

提供有参无参,get(),set(),toString()方法.

3.编写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">
 <!--bean中id:所指的就是一个实体类的对象我们所起的名字
   class:指的是我们需要实例化的类
   property中的name 指的就是我们在实体类中对应的属性
   value:指的是我们所设的值
   -->

    <bean id="stu" class="org.west.pojo.Student">
        <property name="name" value="猫猫"/>
        <property name="age" value="18"/>
    </bean>
    
</beans>

4.测试类

package org.west.pojo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    @Test
    public void test(){
//ClassPathXmlApplicationContext:参数 对应 Spring的核心配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        //getBean:参数 对应我们bean中 给实例化类所起的名字
        Student stu = (Student) context.getBean("stu");
        System.out.println(stu);

    }

}

5.运行结果:

思考问题?


  • student对象是由谁创建的?

    • student对象是由Spring创建的。

    • AppcationContext.xml ----> 管理bean,Java对象 , Spring就像一个容器,存放了许多的对象;

      • 解析配置文件

      • 通过反射创建对象并且设置值

  • student对象的属性是怎么设置值的?

    • student对象的属性是由Spring容器创建的。

这个过程就叫控制反转:IOC

控制:谁来控制对象的创建? 原来 :程序猿来控制, 使用Spring后,Spring来创建对象的

反转:程序本身不创建对象了,变成被动接受对象。

依赖注入:DI ,本质就是利用set方式来注入的。

IOC是一种编程思想。由主动的编程到被动的接受;

 对象由Spring 来创建 , 管理 , 装配 !

 

 

猜你喜欢

转载自www.cnblogs.com/xiaoqiqistudy/p/11271369.html