spring框架底层逻辑(依赖注入和控制反转)

di(依赖注入)和Ioc(控制反转)

情景1:

新建一个项目并且导入spring的jar包,需要导入的jar包有

定义一个IHelloDao的接口,并且在接口里面写一个方法

package dao;

public interface IHelloDao {

    public String sayHi(String name);
}

新建XML文件,在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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
">
    <bean id="helloDao" class="impl.HelloDaoImpl"></bean>
</beans>

在测试类中测试

package test;

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

public class Test {
    public static void main(String[] args) {
        test1();
    }

    private static void test1() {
        //加载spring配置文件
        ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
        //IHelloDao hd = (IHelloDao) ac.getBean("helloDao");
        IHelloDao hd = ac.getBean("helloDao",IHelloDao.class);
        System.out.println(hd.sayHi("阿伟"));
    }
}

情景2:

新建地址和学生的实体类

package entity;

public class Address {
    private String homeaddress;
    private String schooladdress;

    public String getHomeaddress() {
        return homeaddress;
    }

    public void setHomeaddress(String homeaddress) {
        this.homeaddress = homeaddress;
    }

    public String getSchooladdress() {
        return schooladdress;
    }

    public void setSchooladdress(String schooladdress) {
        this.schooladdress = schooladdress;
    }

    public Address(String homeaddress, String schooladdress) {
        this.homeaddress = homeaddress;
        this.schooladdress = schooladdress;
    }

    public Address() {
    }
}

package entity;

public class StudentInfo {
    private String sid;
    private String name;
    private int age;
    private Address address;

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public StudentInfo(String sid, String name, int age, Address address) {
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public StudentInfo() {
    }

    public StudentInfo(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
}

新建IStudentInfoDao接口

package dao;

import entity.StudentInfo;

public interface IStudentInfoDao {
    public void addStudent(StudentInfo s);
    public void findStudent();
}

新建StudentInfoDaoImpl类实现IStudentInfoDao接口

package impl;

import dao.IStudentInfoDao;
import entity.StudentInfo;

public class StudentInfoDaoImpl implements IStudentInfoDao {
    @Override
    public void addStudent(StudentInfo s) {
        System.out.println("执行添加学生的业务逻辑");
    }

    @Override
    public void findStudent() {
        System.out.println("执行查询学生的业务逻辑");
    }
}

在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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
">
    <bean id="address" class="entity.Address">
        <property name="homeaddress" value="翻斗花园"></property>
        <property name="schooladdress" value="翻斗幼儿园"></property>
    </bean>
    <bean id="studentInfo" class="entity.StudentInfo">
        <property name="sid" value="1"></property>
        <property name="name" value="阿伟"></property>
        <property name="age" value="20"></property>
        <property name="address" ref="address"></property>
    </bean>
    <bean id="studentInfoDao" class="impl.StudentInfoDaoImpl"></bean>
</beans>

进行测试

package test;

import conroller.StudentController;
import dao.IHelloDao;
import entity.StudentInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.StudentService;

public class Test {
    public static void main(String[] args) {
        test2();
    }

    private static void test2() {
        ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
        StudentInfo s = ac.getBean("studentInfo", StudentInfo.class);
        System.out.println("您的地址为:"+s.getAddress().getSchooladdress());
    }

}

结果为

控制

 新建service类

package service;

import dao.IStudentInfoDao;
import entity.StudentInfo;

public class StudentService {
    private IStudentInfoDao iStudentInfoDao;

    public IStudentInfoDao getiStudentInfoDao() {
        return iStudentInfoDao;
    }

    public void setiStudentInfoDao(IStudentInfoDao iStudentInfoDao) {
        this.iStudentInfoDao = iStudentInfoDao;
    }

    public void addStudent(StudentInfo s) {
        iStudentInfoDao.addStudent(s);
    }

    public void findStudent() {
        iStudentInfoDao.findStudent();
    }
}

在XML文件中配置

    <bean id="studentInfoDao" class="impl.StudentInfoDaoImpl"></bean>
    <bean id="studentService" class="service.StudentService" autowire="byType">
        <property name="iStudentInfoDao" ref="studentInfoDao"></property>
    </bean>

测试

package test;

import conroller.StudentController;
import dao.IHelloDao;
import entity.StudentInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.StudentService;

public class Test {
    public static void main(String[] args) {
        //test1();
        //test2();
        test3();
    }

    private static void test3() {
        ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
        StudentService ss = ac.getBean("studentService",StudentService.class);
        ss.findStudent();
    }

    private static void test2() {
        ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
        StudentInfo s = ac.getBean("studentInfo", StudentInfo.class);
        System.out.println("您的地址为:"+s.getAddress().getSchooladdress());
    }

    private static void test1() {
        //加载spring配置文件
        ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
        //IHelloDao hd = (IHelloDao) ac.getBean("helloDao");
        IHelloDao hd = ac.getBean("helloDao",IHelloDao.class);
        System.out.println(hd.sayHi("阿伟"));
    }
}

新建一个StudentController类

package conroller;

import entity.StudentInfo;
import service.StudentService;

public class StudentController {
    private StudentService studentService;

    public StudentService getStudentService() {
        return studentService;
    }

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    public String addStudent(){
        studentService.addStudent(new StudentInfo());
        return null;
    }

    public String findStudent(){
        studentService.findStudent();
        return null;
    }

}

测试

package test;

import conroller.StudentController;
import dao.IHelloDao;
import entity.StudentInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.StudentService;

public class Test {
    public static void main(String[] args) {
        //test1();
        //test2();
        //test3();
        test4();
    }

    private static void test4() {
        ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
        StudentController sc = ac.getBean("studentController",StudentController.class);
        sc.findStudent();
    }

    private static void test3() {
        ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
        StudentService ss = ac.getBean("studentService",StudentService.class);
        ss.findStudent();
    }

    private static void test2() {
        ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
        StudentInfo s = ac.getBean("studentInfo", StudentInfo.class);
        System.out.println("您的地址为:"+s.getAddress().getSchooladdress());
    }

    private static void test1() {
        //加载spring配置文件
        ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
        //IHelloDao hd = (IHelloDao) ac.getBean("helloDao");
        IHelloDao hd = ac.getBean("helloDao",IHelloDao.class);
        System.out.println(hd.sayHi("阿伟"));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月与清酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值