笔记:Spring依赖注入的个人理解。

一、Spring依赖注入的使用。

什么是依赖注入?

打个比方:你的学校里有两种吃午餐的方式:

a.是吃现成做好的盒饭。

b.去食堂打饭。

 

现成的盒饭,你买了一盒,觉得里面有个菜不合今天的胃口,就只能再买一盒别的吃,你在买盒饭的过程中需要关心盒饭里装的是什么菜。

这是不用依赖注入生成类的方式。盒饭依赖于里面的菜,如果对菜不满意,那只能把盒饭也换掉。

 

去食堂打饭,你会得到一个餐盘,然后食堂会根据你想要的打给你吃,你想要什么餐盘里就可以装什么。食堂就是一个依赖注入容器,你不用再去关心这个餐盘里面有什么菜,食堂这个依赖注入容器会帮你生成你想要的午餐。

 

写一个程序模拟这个过程

1)新建一个Spring Legacy Project

 

2)写两个类

Lunch类是我们要生成的午餐类,Dish是Lunch依赖的类。

 

Lunch的代码:

public class Lunch {

	private String staplefood;  //主食
	private Dish dish;          //菜
	
	public Lunch() {}           //无参数构造方法
	
	public Lunch(String staplefood, Dish dish) {
		
		this.staplefood = staplefood;
		this.dish = dish;
	}
	public String getStaplefood() {
		return staplefood;
	}
	public void setStaplefood(String staplefood) {
		this.staplefood = staplefood;
	}
	public Dish getDish() {
		return dish;
	}
	public void setDish(Dish dish) {
		this.dish = dish;
	}
	@Override
	public String toString() {  //重写toString 方法会输出午餐里的主食和菜
		return "Lunch : staplefood is "+ staplefood +". "+dish ;
	}

Dish 的代码:

public class Dish {

	private String meatdishes;           //荤菜
	private String vegetablefood;        //素菜
	
	public Dish() {}             //无参数构造方法
	
	public Dish(String meatdishes, String vegetablefood) { //通过构造方法初始化
		
		this.meatdishes = meatdishes;
		this.vegetablefood = vegetablefood;
	}
	public String getMeatdishes() {
		return meatdishes;
	}
	public void setMeatdishes(String meatdishes) {
		this.meatdishes = meatdishes;
	}
	public String getVegetablefood() {
		return vegetablefood;
	}
	public void setVegetablefood(String vegetablefood) {
		this.vegetablefood = vegetablefood;
	}
	
	@Override
 	public String toString() {  //重写toString方法,输出荤菜和素菜
		return "Dish is "+meatdishes+" and "+vegetablefood;
	}	
	
}

3)利用配置文件application-config来对java bean 进行配置

配置文件,生成一个dish1实例

这里采用的是构造器注入,通过对构造器来初始化Dish类的两个成员变量 

name值得是成员变量的名字,value跟着设定的值

dish1是“西红柿炒鸡蛋”

第二个Dish实例,生成dish2实例

这里采用的也是构造器注入,只不过,用的是参数序号的方式,index 指的是 Dish构造方法中参数列表的序号

dish2 “土豆炒肉”

然后配置Lunch,得到一个mylunch实例,通过属性注入,ref 是引用的实例这里引用dish1“西红柿炒鸡蛋”

4)测试类Test

public class Test {

	public static void main(String[] args) {
		//使用ApplicationContext来构建一个上下文容器
		ApplicationContext context=new ClassPathXmlApplicationContext("Lunch/lfc/application-config.xml");
        //用上下文容器来调用 getBean方法获得一个lunch
		Lunch  lunch=context.getBean("myLunch", Lunch.class);
		//输出lunch
        System.out.println(lunch);
        
	}

}

这里注入的是西红柿炒鸡蛋,所以输出结果

如果不想吃 西红柿炒鸡蛋,只需要把配置文件中的 dish1换成dish2,就可以吃到土豆丝炒肉了

输出

5)注意事项

两个类都必须声明一个无参数的构造方法,用于创建bean的实例,没有无参数的构造方法xml会报错。

在xml中的实例所设置的类,需要写全包名,所以两个类都不能是裸体类,需要带着包创建。

在ClassPathXmlApplicationContext 后面跟着的url需要完整路径,否则会报

FileNotFoundException: class path resource [application-config.xml] cannot be opened because it does not exist

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值