欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring依赖注入的模式和类型

程序员文章站 2022-06-13 13:02:03
...

手动模式 - 配置或者编程的方式,提前安排注入规则

  • XML 资源配置元信息
  • Java 注解配置元信息
  • API 配置元信息

自动模式 - 实现方提供依赖自动关联的方式,按照內建的注入规则

  • Autowiring(自动绑定)

实体类Rumenz.java

package com.rumenz;

public class Rumenz {

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

实体类Holder.java

package com.rumenz;

public class Holder {

    private Rumenz rumenz;


    public Rumenz getRumenz() {
        return rumenz;
    }

    public void setRumenz(Rumenz rumenz) {
        this.rumenz = rumenz;
    }
}

XML 资源配置元信息

配置文件Beans.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">


    <bean id="rumenzz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        <property name="name" value="入门小站"/>
    </bean>

    <bean id="holder" class="com.rumenz.Holder">
        <property name="rumenz" ref="rumenzz"/>
    </bean>
</beans>

调用类

package com.rumenz;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class DemoApplication {

    public static void main(String[] args) {
       ClassPathXmlApplicationContext ap=new ClassPathXmlApplicationContext("Beans.xml");
        Holder holder= (Holder) ap.getBean("holder");
        System.out.println(holder.getRumenz().getName());
    }
}

输出

入门小站

Java 注解配置元信息 @Bean

Beans.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">


    <bean id="rumenzz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        <property name="name" value="入门小站"/>
    </bean>
</beans>

调用DemoApplication.java

package com.rumenz;

public class DemoApplication {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext anc=new AnnotationConfigApplicationContext();
        anc.register(DemoApplication.class);
        XmlBeanDefinitionReader xmlBeanDefinitionReader=new XmlBeanDefinitionReader(anc);
        xmlBeanDefinitionReader.loadBeanDefinitions("Beans.xml");
        anc.refresh();
        Holder holder = anc.getBean(Holder.class);
        System.out.println(holder.getRumenz().getName());
        anc.close();
    }

    @Bean
    public Holder  holder(Rumenz rumenz){
        return new Holder(rumenz);
    }

}

输出

入门小站

API 配置元信息

配置文件Beans.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">


    <bean id="rumenzz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        <property name="name" value="入门小站"/>
    </bean>
</beans>

调用DemoApplication.java

package com.rumenz;


public class DemoApplication {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
        // 生成和注册Holder的BeanDefinition
        BeanDefinition beanDefinition=createBeanDefinition();
        ac.registerBeanDefinition("holder",beanDefinition);
        XmlBeanDefinitionReader xmlBeanDefinitionReader=new XmlBeanDefinitionReader(ac);

        xmlBeanDefinitionReader.loadBeanDefinitions("Beans.xml");
        //启用上下文
        ac.refresh();
        Holder bean = ac.getBean(Holder.class);
        System.out.println(bean.getRumenz().getName());
        ac.close();
    }

    private static BeanDefinition createBeanDefinition() {
        BeanDefinitionBuilder beanDefinitionBuilder=BeanDefinitionBuilder.genericBeanDefinition(Holder.class);
        beanDefinitionBuilder.addPropertyReference("rumenz","rumenzz");
        return beanDefinitionBuilder.getBeanDefinition();
    }
}

输出

入门小站

Autowiring(自动绑定)

属性注入Holder.java

package com.rumenz;

import org.springframework.beans.factory.annotation.Autowired;

public class Holder {

    @Autowired //属性注入
    private Rumenz rumenz;
    public Rumenz getRumenz() {
        return rumenz;
    }

    public void setRumenz(Rumenz rumenz) {
        this.rumenz = rumenz;
    }
}

调用

package com.rumenz;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;


public class DemoApplication {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
        ac.register(DemoApplication.class);
        ac.refresh();
        Holder bean = ac.getBean(Holder.class);
        System.out.println(bean.getRumenz().getName());
        ac.close();
    }

    @Bean
    public Rumenz  rumenz(){
        Rumenz r = new Rumenz();
        r.setId(123);
        r.setName("入门小站");
        return r;
    }
    @Bean
    public Holder holder(){
        return new Holder();
    }

}

输出

入门小站

Spring依赖注入的模式和类型

原文: https://rumenz.com/rumenbiji/Spring-dependency-injection.html

相关标签: java spring