springDI应用

手动装配

通过构造方法

1.实体类需要带参的构造方法

    private String brand;
    private String name;
    private Float price;

    public Computer(){

    }

    public Computer(String brand, String name, Float price) {
        this.brand = brand;
        this.name = name;
        this.price = price;
    }

2.配置文件如下

    <bean name="room" class="domain.Room">
        <constructor-arg name="name" value="room1"></constructor-arg>
        <constructor-arg name="computer" >
            <bean class="domain.Computer">
                <constructor-arg name="brand" value="xiaomi"></constructor-arg>
                <constructor-arg name="name" value="pro16"></constructor-arg>
                <constructor-arg name="price" value="7888"></constructor-arg>
            </bean>
        </constructor-arg>
    </bean>

通过set方法

1.需要一个无参构造方法

2. 配置文件如下:
    <bean name="room" class="domain.Room">
        <property name="name" value="room1"></property>
        <property name="computer">
            <bean class="domain.Computer">
                <property name="brand" value="xiaomi"></property>
                <property name="name" value="pro16"></property>
                <property name="price" value="7888"></property>
            </bean>
        </property>
    </bean>

自动装配

  1. 构造方法
  • 新建类StudentController、StudentService、StudentDao(构造方法中打印写东西区分构造方法 or set方法)
  • 配置文件如下
    <bean name="studentController" class="controller.StudentController"  autowire="constructor"></bean>
    <bean name="studentService" class="service.StudentService" autowire="constructor"></bean>
    <bean name="studentDao" class="dao.StudentDao"></bean>
  • 新建一个测试类测试
        BeanFactory factory1 = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        StudentController controller = (StudentController) factory1.getBean("studentController");
        controller.login();
  • 运行结果


    image.png
  1. set方法
  • 禁用有参构造,新建无参构造
  • 配置文件如下byType
    <bean name="studentController" class="controller.StudentController" autowire="byType"></bean>
    <bean name="studentService" class="service.StudentService" autowire="byType"></bean>
    <bean name="studentDao" class="dao.StudentDao"></bean>
  • 配置文件如下byName(注意:byName要求类的属性名称必须和配置文件bean的name属性一致,否则会找不到bean*,例子上面截图中的studentService属性和配置文件中bean的name属性是一致的)
    <bean name="studentController" class="controller.StudentController" autowire="byType"></bean>
    <bean name="studentService" class="service.StudentService" autowire="byType"></bean>
    <bean name="studentDao" class="dao.StudentDao"></bean>
  • 运行结果


    image.png

自动装配之接口

1.配置如下


image.png
  • 接口类中配置(一个方法目的为了区分接口实现类,B类接口一样,输出的是B)


    image.png

2.1通过构造方法

  • 有带参构造方法,构造方法有输出
  • 配置文件如下
    <bean name="ti" class="interfaceTest.impl.InterfaceTestA" ></bean>
    <bean name="ti2" class="interfaceTest.impl.InterfaceTestB" ></bean>

    <bean name="testInterface" class="interfaceTest.TestInterface" autowire="constructor"></bean>
  • 使用类配置
public class TestInterface {

    private InterfaceTest ti;

    public InterfaceTest getTi() {
        return ti;
    }

    public TestInterface(InterfaceTest ti) {
        System.out.println("TestInterface 有参构造");
        this.ti = ti;
    }

    public TestInterface() {
        System.out.println("TestInterface 无参构造");
    }

    public void setTi(InterfaceTest ti) {
        System.out.println("set方法");
        this.ti = ti;
    }
}
  • 运行结果
    A


    image.png

    B


    image.png

    C
    image.png

我们可以发现通过构造方法进行自动装配,只有一个接口的情况下正常运行,如果有2个接口,她会先根据类型找(情况A),如果找不到(找到就是情况B),然后根据名称去寻找(情况C)
2.2通过set方法
2.2.1 byName(bean的name属性要与类中属性名一致,否则报错找不到bean)


image.png

2.2.2 byType(byType只能有一个实现接口,否则根据类型不知道找那个实现的接口类)


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容