三种注入方式:
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方法注入