有关Bean的 依赖设置有两种方法:
1.常用的Setter()方法,它是在建立好对象后再完全依赖注入.
2.使用Constructor方法,它在构造对象是一并使用.
下面我用第一种方法来写。
1.Student1类:
package com.lzw;
public class Stu1 implements student ... {
private String name;
private String sex;
private int age;
public void printInfo() ...{
// TODO Auto-generated method stub
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("年龄:" + age);
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
}
public class Stu1 implements student ... {
private String name;
private String sex;
private int age;
public void printInfo() ...{
// TODO Auto-generated method stub
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("年龄:" + age);
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
}
2.student2类:
package com.lzw;
public class Stu2 implements student ... {
private String name;
private String sex;
private int age;
public void printInfo() ...{
// TODO Auto-generated method stub
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("年龄:" + age);
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
}
public class Stu2 implements student ... {
private String name;
private String sex;
private int age;
public void printInfo() ...{
// TODO Auto-generated method stub
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("年龄:" + age);
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
}
3.一个student接口:
package com.lzw;
public interface student ... {
public void printInfo();
}
public interface student ... {
public void printInfo();
}
测试类:
package com.lzw;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Info ... {
/**//**
* 有关实例化BeanFactory常用的两种方法
*/
public static void main(String[] args) ...{
/**//**
* 方法一:
*/
//final ClassPathResource rs=new ClassPathResource("applicationContext.xml");
//XmlBeanFactory be=new XmlBeanFactory(rs);
//student stu=(student) be.getBean("stu1");
//stu.printInfo();
//student stu1=(student) be.getBean("stu2");
//stu1.printInfo();
/**//**
* 方法二:
*/
ApplicationContext cont=new ClassPathXmlApplicationContext("applicationContext.xml");
student stu1=(student) cont.getBean("stu1");
stu1.printInfo();
student stu2=(student) cont.getBean("stu2");
stu2.printInfo();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Info ... {
/**//**
* 有关实例化BeanFactory常用的两种方法
*/
public static void main(String[] args) ...{
/**//**
* 方法一:
*/
//final ClassPathResource rs=new ClassPathResource("applicationContext.xml");
//XmlBeanFactory be=new XmlBeanFactory(rs);
//student stu=(student) be.getBean("stu1");
//stu.printInfo();
//student stu1=(student) be.getBean("stu2");
//stu1.printInfo();
/**//**
* 方法二:
*/
ApplicationContext cont=new ClassPathXmlApplicationContext("applicationContext.xml");
student stu1=(student) cont.getBean("stu1");
stu1.printInfo();
student stu2=(student) cont.getBean("stu2");
stu2.printInfo();
}
}
Web配置:
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
< beans
xmlns = " http://www.springframework.org/schema/beans "
xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd " >
< bean id = " stu1 " class = " com.lzw.Stu1 " >
< property name = " name " >
< value > 飞龙 </ value >
</ property >
< property name = " sex " >
< value > 男 </ value >
</ property >
< property name = " age " >
< value > 26 </ value >
</ property >
</ bean >
< bean id = " stu2 " class = " com.lzw.Stu2 " >
< property name = " name " >
< value > 欣欣 </ value >
</ property >
< property name = " sex " >
< value > 男 </ value >
</ property >
< property name = " age " >
< value > 22 </ value >
</ property >
</ bean >
</ beans >
< beans
xmlns = " http://www.springframework.org/schema/beans "
xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd " >
< bean id = " stu1 " class = " com.lzw.Stu1 " >
< property name = " name " >
< value > 飞龙 </ value >
</ property >
< property name = " sex " >
< value > 男 </ value >
</ property >
< property name = " age " >
< value > 26 </ value >
</ property >
</ bean >
< bean id = " stu2 " class = " com.lzw.Stu2 " >
< property name = " name " >
< value > 欣欣 </ value >
</ property >
< property name = " sex " >
< value > 男 </ value >
</ property >
< property name = " age " >
< value > 22 </ value >
</ property >
</ bean >
</ beans >