applicationContext.xml 文件中bean的作用

applicationContext.xml 文件中bean的作用

下面的代码片段展示了当使用applicationContext.xml文件来实例化ChineseImpl实例时,Spring容器做的工作

ChineseImpl chinese = new ChineseImpl () ;
chinese.setName("小明");
chinese.setAge(10);
	<bean id="chinese" class="com.bean.ChineseImpl">
	    
		<property name="name">
			<value>小明value>
		property>
		
		<property name="age">
			<value>10value>
		property>
	bean>

com.bean.ChineseImpl文件

package com.bean;

public class ChineseImpl implements Person{
	private String name;
	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;
	}

	private int age;

	public void Speak() {
		// TODO Auto-generated method stub
		System.out.println("我是中国人,我的名字是" + this.name + "我的年纪是" + this.age);
		
	}

}


Person接口

package com.bean;

public interface Person {
	//接口中包含了一个Speak()方法,表示人都可以说话
	public void Speak();

}

你可能感兴趣的:(applicationContext.xml 文件中bean的作用)