Spring1.0——spring的IoC控制反转和DI依赖注入简单案例

发布时间:2024-01-27 10:00

JAVAWEB框架学习文章索引点这里
核心api:
BeanFactory:用于生成任意bean,采取延迟加载,第一次getBean的时候才会初始化Bean
ApplicationContext:是BeanFactory的子接口,功能更强大。当配置文件被加载时,就进行对象实例化。
ClassPathXmlApplicationContext :用于加载classpath(类路径、src)下的xml
FileSystemXmlApplicationContext:用于加载指定盘符下的xml

spring loC(控制反转)简单使用
1,导入jar包(beans,context,core,expression,logging)
2,编写配置文件

配置文件


<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="Person" class="com.IocDemo.Person">bean>
beans>

测试

    @Test
    public void test1() {
        //1,获得spring容器
        String xmlPath = "com/IocDemo/beans.xml";
        ApplicationContext appCont = new ClassPathXmlApplicationContext(xmlPath);
        //2,获得内容
        Person person = (Person) appCont.getBean("Person");
        person.setId(20);
        System.out.println(person.getId());
    }

DI 依赖注入
分别创建StudentDao、StudentService接口
分别创建StudentDaoImpl、StudentServiceImpl实现类
在StudentServiceImpl中设置属性StudentDao并设置对于的setStudentDao方法(配置文件配置后自动调用进行赋值),由于StudentServiceImpl中并不知道StudentDao的实现类的内容(实际上调用setStudentDao方法的时候配置传入的是它的实现类),这是一种解耦。

几个接口,实现类

package com.b_DI;

public interface StudentDao {
    void info();
}
package com.b_DI;

public class StudentDaoImpl implements StudentDao {
    @Override
    public void info() {
        System.out.println("我是学生");
    }
}
package com.b_DI;

public interface StudentService {
    void study();
}
package com.b_DI;

public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;

    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
    @Override
    public void study() {
        System.out.println("学生学习");
        studentDao.info();
    }
}

配置文件:


<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="StudentService" class="com.b_DI.StudentServiceImpl">
        <property name="studentDao" ref="StudentDao">property>
    bean>
    <bean id="StudentDao" class="com.b_DI.StudentDaoImpl">bean>
beans>

测试类

package com.b_DI;

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

import com.a_IocDemo.Person;

public class MyTest {
    @Test
    public void test1() {
        //1,获得spring容器
        String xmlPath = "com/b_DI/DITest.xml";
        ApplicationContext appCont = new ClassPathXmlApplicationContext(xmlPath);
        //依赖注入
        StudentService studentService = (StudentService) appCont.getBean("StudentService");
        studentService.study();
    }
}

结果:

学生学习
我是学生

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号