装配Bean(三)
来源:互联网 发布:手机噪音检测软件 编辑:程序博客网 时间:2024/06/05 21:37
继承装配:
①继承
②继承配置
③覆盖父Bean配置
④可以设置<bean>的abstract属性为true, Spring不会实例化该Bean
<bean id="gradate" parent="student" class=“...Gradate">
public class Student
public class Gradate extends Student
例子:
package com.hsp.inherit;public class Student {protected String name;protected int age;// *****************************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;}}
package com.hsp.inherit;public class Gradate extends Student {private String degree;//*******************************public String getDegree() {return degree;}public void setDegree(String degree) {this.degree = degree;}}
<?xml version="1.0" encoding="utf-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 配置一个学生对象 --><bean id="student" class="com.hsp.inherit.Student"><property name="name" value="顺平" /><property name="age" value="30" /></bean><!-- 配置Grdate对象 --><bean id="grdate" parent="student" class="com.hsp.inherit.Gradate"><!-- 如果自己配置属性name,age,则会替换从父对象继承的数据 --><property name="name" value="小明" /><property name="degree" value="学士" /></bean></beans>
package com.hsp.inherit;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App1 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/inherit/beans.xml");Gradate gradate=(Gradate) ac.getBean("grdate");System.out.println(gradate.getName()+" "+gradate.getAge()+" "+gradate.getDegree());}}
小明 30 学士
0 0
- 装配Bean(三)
- spring基础-Bean的装配方式(三)自动装配
- Spring框架自动装配 Bean(三)
- 一点一滴学习Spring(三)之bean高级装配
- 【Spring 核心】装配bean(三)XML配置
- Spring学习(四)-Bean的三种装配方式
- Spring入门笔记(三)Bean--Aware、自动装配、Resources
- 装配Bean(一)
- 装配Bean(二)
- 装配Bean(四)
- 装配Bean(五)
- 装配Bean(六)
- Spring装配bean的三种方式
- spring装配bean的三种方式
- Spring之通过XML装配bean(三)
- Bean的装配(笔记)
- Spring(二)--Bean装配
- Spring(4)装配Bean
- 使用Generator生成对象映射关系代码
- hdu1203I NEED A OFFER! 01背包水题
- java类初始化时执行顺序
- linux 服务器 创建vpn
- Activity的四种启动模式
- 装配Bean(三)
- 最近用到的命令
- C语言数组
- 堆--优先队列
- 404_根据uri拿到路径
- c++遍历数组
- 简单实例化解析安卓Thread线程
- 求π近似值
- 蒙特卡洛方法——玩具例子(1)圆周率