Spring从入门到放弃

我的第一个Spring程序

千辛万苦终于调试好了idea,开始了第一个程序的编写,其中有如下要求:

  • (1)建立Phone接口(call方法),建立PhoneImpl实现类(call方法)。

  • (2)建立Student接口(learn方法),并建立StudentImpl实现类(name和phone属性,learn方法和setPhone方法)。
    注:通过setPhone方法可以为phone属性赋值。

  • (3)使用ApplicationContext接口,实例化spring容器,并初始化(phone和student两个bean)。

  • (4)将phone(bean)通过setter方法注入到student中。

  • (5)建立 TestSpring 测试类,在 main 方法中,进行测试。

首先在 edu.itgeek.setter 包中,创建接口 Phone,在接口中编写一个 call() 方法:

package edu.itgeek.setter;

public interface Phone {
    public void call();
}

之后创建 Phone 接口的实现类 PhoneImpl ,在类中声明 phone 属性,并添加属性的 setter 方法,用于实现依赖注入:

package edu.itgeek.setter;

public class PhoneImpl implements Phone{
    private PhoneImpl phone;

    public void setPhone(PhoneImpl phone) {
        this.phone = phone;
    }
    @Override
    public void call() {
        System.out.println("I will call you");
    }

}

同样方法创建 student 接口,并创建 learn() 方法:

package edu.itgeek.setter;

public interface Student {
    public void learn();
}

之后创建 Student 接口的实现类 StudentImpl ,并将 phone(bean) 通过 sette r方法注入到 student中:

package edu.itgeek.setter;

public class StudentImpl implements Student {

    private Phone phone;

    public void setPhone(Phone phone) {
        this.phone = phone;
    }

    @Override
    public void learn() {
        this.phone.call();
        System.out.println("good good study day day up");
    }
}

之后在配置文件 applicationContext.xml 中,创建 id 为 phone 和 student 的 Bean , 该 Bean 用于实例化 StudentImpl 中的信息 并将 phone 的实例注入到 Phone 中:

<?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="phone" class="edu.itgeek.setter.PhoneImpl"></bean>
    <bean id="student" class="edu.itgeek.setter.StudentImpl">
        <property name="phone" ref="phone"></property>
    </bean>
</beans>

最后创建测试类 Test :

package edu.itgeek.setter;

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

public class Test {
    public static void main(String[] args){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("edu/itgeek/setter/applicationContext.xml");
        Phone phone = (Phone) applicationContext.getBean("phone");
        phone.call();
        Student student = (Student) applicationContext.getBean("student");
        student.learn();

    }
}

控制台输出如下所示:
I will call you
I will call you
good good study day day up

第一个程序就这么运行出来了,期间也遇到了很多弱智一样的错误:比如applicationContext.xml配置文件应该放在src目录下;比如通过容器获取实例时,需要用到 getBean 方法;又比如将phone(bean) 通过 setter 方法注入到 student 中时,需要在实现类 StudentImpl 中创建 setPhone 方法。

所以,所谓的依赖注入(DI),就是在 main() 方法中,不通过new来创建接口的实现类对象,而是通过Spring 容器来获取,控制反转也就是同一含义从另一个角度的描述:控制权由调用者转移到了Spring容器,控制权发生反转。

创建Spring程序时,有很多操作都是系统化的统一操作,比如 jar 包的引入,比如通过ClassPathXmlApplicationContext 创建 ApplicationContext ,比如接口的创建等等。

记录结束!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值