这一章节我们来讨论一下注解装配的@autowired是怎样通过set方法或者其他方法注入?
1.domain
蛋糕类:(不变)
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_7;
public class Cake {
private String name = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
厨师类:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_7;
import org.springframework.beans.factory.annotation.Autowired;
public class Chief {
private Cake cake = null;
public Cake getCake() {
return cake;
}
@Autowired
public void setCake(Cake cake) {
this.cake = cake;
}
private String name = "";
public String getName() {
return name;
}
public Cake makeOneCake() {
System.out.println(getName() + " make " + cake.getName());
return cake;
}
p