Spring——day01

三种注入方式:
1、普通注入

package cn.tedu.docker;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;

@ComponentScan("cn.tedu.docker")
public class Config {

    @Bean
    public DragonBlade blade(){
        return new DragonBlade();
    }

    @Bean("Q")
    @Lazy
    public Hero init(DragonBlade blade){
        Hero h = new Hero();
        h.setName("关羽");
        h.setAge(25);
        h.setDragonBlade(blade);
        return h;
    }
}

package cn.tedu.docker;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("com")
public class DragonBlade {
    private String name = "青龙偃月刀";

    @Override
    public String toString() {
        return name;
    }
}

package cn.tedu.docker;

public class Hero {
    private String name;
    private int age;
    private DragonBlade dragonBlade;

    public DragonBlade getDragonBlade() {
        return dragonBlade;
    }

    public void setDragonBlade(DragonBlade dragonBlade) {
        this.dragonBlade = dragonBlade;
    }

    public Hero() {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "config{" +
                "name='" + name + '\'' +
                ", age=" + age + "," + dragonBlade
                +
                '}';
    }

}

测试:

package cn.tedu.test;

import cn.tedu.docker.Config;
import cn.tedu.docker.DragonBlade;
import cn.tedu.docker.Hero;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestCase {
    AnnotationConfigApplicationContext ctx;

    @Before
    public void init() {
        ctx = new AnnotationConfigApplicationContext(Config.class);
    }

    @After
    public void destory() {
        ctx.close();
    }

    @Test
    public void test() {
        Hero q = ctx.getBean("Q", Hero.class);
        System.out.println(q);

    }
}

2、扫描注入

package cn.tedu.her02;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan("cn.tedu.her02")
public class Config {

}

package cn.tedu.her02;

import org.springframework.stereotype.Component;

import java.io.Serializable;

//@Component
public class DragonBlade implements Wenpon, Serializable {


    private String name = "青龙偃月刀";

    @Override
    public String toString() {
        return name;
    }
}

package cn.tedu.her02;

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

import java.io.Serializable;

@Component
public class GuanYu implements Serializable {

    private String name = "关羽";

    @Autowired
    private Wenpon wenpon;//为了降低程序的耦合性,将原声明的DragonBlade
    //修改为声明接口Weapon
    //private DragonBlade dragonBlade;


    public void fight() {
        System.out.println(name + "使用" + wenpon + "战斗");
    }


}

package cn.tedu.her02;

import org.springframework.stereotype.Component;

import java.io.Serializable;

@Component("FTian")
public class SkyLancer implements Serializable,Wenpon {

    private String name = "方天画戟";

    @Override
    public String toString() {
        return name;
    }
}

package cn.tedu.her02;

//这是一个武器接口
public interface Wenpon {

    //实现武器接口必须重写toString
    public String toString();

    //我们通过接口能够降低程序的耦合性
    //在依赖武器的对象中声明时候,声明接口类对象,而不声明具体类



}

package cn.tedu.her02;

import cn.tedu.her02.Config;
import cn.tedu.her02.GuanYu;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestHero {
    AnnotationConfigApplicationContext ctx;

    @Before
    public void init() {
        ctx = new AnnotationConfigApplicationContext(Config.class);
    }

    @After
    public void destory() {
        ctx.close();
    }

    @Test
    public void test() {
        GuanYu gy = ctx.getBean("guanYu",GuanYu.class);
        gy.fight();

    }
}

3、Set方法注入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ctrl精

面试很多问题,积攒不容易

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

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

打赏作者

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

抵扣说明:

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

余额充值