spring心得
属性注入 autowire
1.xml autowire="byName"
点击查看代码
public class User {
@Value(value = "abc")
private String name;
public void add(){
System.out.println("add....");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private Cat cat;
private Dog dog;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
点击查看代码
public class User {
@Value(value = "abc")
private String name;
public void add() {
System.out.println("add....");
}
@Autowired
private Cat cat;
}
原来我一直用的是属性注入
点击查看代码
@Service
public class Room {
@Autowired
private Computer computer;
}
setter注入
点击查看代码
@Service
public class BService {
AService aService;
@Autowired
public void setaService(AService aService) {
this.aService = aService;
}
}
构造注入
点击查看代码
@Service
public class AService {
BService bService;
@Autowired
public AService(BService bService) {
this.bService = bService;
}
}