依赖注入有三种方式:
1接口注入:
接口注入的意思是通过接口来实现信息的注入,而其它的类要实现该接口时,就可以实现了注入:
下面是代码
- publicinterfaceManager{
- publicvoidmanage(Businessbusiness);
- }
- publicclassxx{
- privateBusinessbusiness;
- publicvoidmanage(Businessbusiness){
- this.business=business;
- }
- }
public interface Manager{
public void manage(Business business);
}
public class xx {
private Business business;
public void manage(Business business){
this.business = business;
}
}
2 Set注入:
通过set方法来注入
- publicclassxx{
- privateManagermanage;
- publicvoidsetManager(Managermanage){
- this.manage=manage;
- }
- }
public class xx {
private Manager manage;
public void setManager(Manager manage){
this.manage= manage;
}
}
3 构造注入:
通过构造函数来注入
- publicclassxx{
- privateManagermanage;
- publicxx(Managermanage){
- this.manage=manage;
- }
- }
@H_230_
301@public class xx {
private Manager manage;
public xx(Manager manage){
this.manage= manage;
}
}
当然通过不同的注入方式,config.xml的配置是不同的。
原文链接:https://www.f2er.com/javaschema/287100.html- publicinterfaceManager{
- publicvoidmanage(Businessbusiness);
- }
- publicclassxx{
- privateBusinessbusiness;
- publicvoidmanage(Businessbusiness){
- this.business=business;
- }
- }
- publicclassxx{
- privateManagermanage;
- publicvoidsetManager(Managermanage){
- this.manage=manage;
- }
- }
- publicclassxx{
- privateManagermanage;
- publicxx(Managermanage){
- this.manage=manage;
- }
- }