Spring框架中<bean>第一种
2020-04-21 21:38 a_mf 阅读(161) 评论(0) 收藏 举报一、通过set、get函数用<property name=" " value=" "></property>来获取
原始方法:
main函数中:
public static void main(String[] args){ student student=new student(); student.setStuNo(1); student.setStuName("李梦凡"); student.setStuAge(20); System.out.println(student); }
student类中:
public class student {
private int stuNo;
private String stuName;
private int stuAge;
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
@Override
public String toString(){
return this.stuNo +","+this.stuName +","+this.stuAge;
}
}
使用springIOC后 \
配置文件applicationContext.xml
<bean id="student" class="org_shitang_entity.student">
<property name="stuNo" value="1"></property>
<property name="stuName" value="李梦凡"></property>
<property name="stuAge" value="20"></property>
</bean>
main函数中:
public static void main(String[] args){ //创建上下文连接 ApplicationContext conext=new ClassPathXmlApplicationContext("applicationContext.xml"); student student=(student)conext.getBean("student"); System.out.println(student); }
student类和之前一样
浙公网安备 33010602011771号