首页 > 编程语言 > 详细

Spring框架中<bean>第一种

时间:2020-04-21 23:13:49      阅读:72      评论:0      收藏: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类和之前一样

Spring框架中<bean>第一种

原文:https://www.cnblogs.com/lmff/p/12748056.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!