Spring依赖注入的模式和类型

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

  • 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





    
        
        
    

    
        
    

调用类

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





    
        
        
    

调用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





    
        
        
    

调用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();
    }

}

输出

入门小站

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

你可能感兴趣的:(java,spring)