通过实例解析spring bean之间的关系
这篇文章主要介绍了通过实例解析springbean之间的关系,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
两种关系:继承、依赖
一、继承关系
Address.java
packagecom.gong.spring.beans.autowire; publicclassAddress{ privateStringcity; privateStringstreet; publicStringgetCity(){ returncity; } publicvoidsetCity(Stringcity){ this.city=city; } publicStringgetStreet(){ returnstreet; } publicvoidsetStreet(Stringstreet){ this.street=street; } @Override publicStringtoString(){ return"Address[city="+city+",street="+street+"]"; } }
beans-relation.xml
Main.java
packagecom.gong.spring.beans.autowire; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassMain{ publicstaticvoidmain(String[]args){ //1.创建spring的IOC容器对象 ApplicationContextctx=newClassPathXmlApplicationContext("beans-relation.xml"); //2.从容器中获取Bean实例 Addressaddress=(Address)ctx.getBean("address"); System.out.println(address.toString()); Addressaddress2=(Address)ctx.getBean("address2"); System.out.println(address2.toString()); } }
输出:
address2继承了address的city配置,因此city=武汉。
当然,我们也可以使用abstract来表明一个Bean是一个抽象bean。抽象bean可以作为一个模板,且不能被实例化。同时,如果一个bean没有声明class,那么该bean也是一个抽象bean,且必须指定abstract="true"。
此时,在进行实例化就会报错
Addressaddress=(Address)ctx.getBean("address");
将抽象bean作为父bean,可以实例化它的子bean:
Addressaddress2=(Address)ctx.getBean("address2"); System.out.println(address2.toString());
二、依赖关系
Car.java
packagecom.gong.spring.beans.autowire; publicclassCar{ publicCar(){ } publicCar(Stringname){ this.name=name; } privateStringname; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } @Override publicStringtoString(){ return"Car[name="+name+"]"; } }
Student.java
packagecom.gong.spring.beans.autowire; importjava.util.List; importjava.util.Map; publicclassStudent{ privateStringname; privateintage; privatedoublescore; privateCarcar; privateAddressaddress; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicdoublegetScore(){ returnscore; } publicvoidsetScore(doublescore){ this.score=score; } publicCargetCar(){ returncar; } publicvoidsetCar(Carcar){ this.car=car; } publicAddressgetAddress(){ returnaddress; } publicvoidsetAddress(Addressaddress){ this.address=address; } @Override publicStringtoString(){ return"Student[name="+name+",age="+age+",score="+score+",car="+car+",address="+address +"]"; } }
beans-relation.xml
spring允许用户通过depends-on属性设定bean前置依赖bean,前置依赖bean会在本Bean实例化之前就创建好。如果前置依赖于多个Bean,则可以通过逗号,空格的方式来配置bean的名称。
Main.java
packagecom.gong.spring.beans.autowire; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassMain{ publicstaticvoidmain(String[]args){ //1.创建spring的IOC容器对象 ApplicationContextctx=newClassPathXmlApplicationContext("beans-relation.xml"); //2.从容器中获取Bean实例 Studentstudent=(Student)ctx.getBean("student"); System.out.println(student.toString()); } }
输出:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。