Spring 依赖注入

先来一个骑士

package root;

/**
 * @author erniu.wzh
 * @date 2021/1/13 8:37 下午
 */
public interface Knight {
    public void embarkOnQuest();
}
package root;

/**
 * @author erniu.wzh
 * @date 2021/1/13 8:21 下午
 */
public class BraveKnight implements Knight{
    private final Quest quest;
    public BraveKnight(Quest quest) {
        this.quest = quest;
    }
    public void embarkOnQuest() {
        quest.embark();
    }
}

再来一个请求

package root;

/**
 * @author erniu.wzh
 * @date 2021/1/13 8:22 下午
 */
public interface Quest {
    public void embark();
}
package root;

import java.io.PrintStream;

/**
 * @author erniu.wzh
 * @date 2021/1/13 8:23 下午
 */
public class SlayDragonQuest implements Quest{
    private final PrintStream stream;

    public SlayDragonQuest(PrintStream stream) {
        this.stream = stream;
    }

    public void embark() {
        stream.println("Embarking on quest to slay the dragon!");
    }
}

如果想让勇敢的骑士去打龙,需要这样

package root;

/**
 * @author erniu.wzh
 * @date 2021/1/13 8:54 下午
 */
public class Main3 {
    public static void main(String[] args) {
        Quest quest = new SlayDragonQuest(System.out);
        Knight knight = new BraveKnight(quest);
        knight.embarkOnQuest();
    }
}

先构造一个quest,再构造一个knight,再打龙

如果用Spring的依赖注入,就不需要这样构造了

使用Spring的依赖注入有两种方法

方法1:使用xml文件配置bean

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="knight" class="root.BraveKnight">
        <constructor-arg ref="quest"/>
    </bean>
    <bean id="quest" class="root.SlayDragonQuest">
        <constructor-arg value="#{T(System).out}"/>
    </bean>
</beans>
package root;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author erniu.wzh
 * @date 2021/1/13 8:25 下午
 */
public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("knights.xml");
        Knight knight = classPathXmlApplicationContext.getBean(Knight.class);
        knight.embarkOnQuest();
        classPathXmlApplicationContext.close();
    }
}

方法2:使用config配置bean

package root;

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

/**
 * @author erniu.wzh
 * @date 2021/1/13 8:42 下午
 */
@Configuration
public class KnightConfig {
    @Bean
    public Knight knight() {
        return new BraveKnight(quest());
    }
    @Bean
    public Quest quest() {
        return new SlayDragonQuest(System.out);
    }
}
package root;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author erniu.wzh
 * @date 2021/1/13 8:46 下午
 */
public class Main2 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("root");
        Knight knight = context.getBean(Knight.class);
        knight.embarkOnQuest();
        context.close();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

牛哥123

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

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

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

打赏作者

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

抵扣说明:

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

余额充值