Spring配置继承属性

一:案例截图

二:基本代码

Student.java

<span style="font-size:14px;">package com.cloud.inherit;

public class Student {
	private String name;
	private 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;
	}
}
</span>

Gradate.java

<span style="font-size:14px;">package com.cloud.inherit;

public class Gradate extends Student{
	private String degree;
	public String getDegree() {
		return degree;
	}
	public void setDegree(String degree) {
		this.degree = degree;
	}
}
</span>

三:配置代码

<span style="font-size:14px;"><?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.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置一个学生对象 -->
<bean id="student" class="com.cloud.inherit.Student">
	<property name="name" value="Spring"/>
	<property name="age" value="22"/>
</bean>
<!-- 配置Grdate对象 -->
<bean id="gradate" parent="student" class="com.cloud.inherit.Gradate">
	<property name="name" value="李四"/>
	<property name="degree" value="学士"/>
</bean>
</beans></span>

四:测试代码

<span style="font-size:14px;">package com.cloud.inherit;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("com/cloud/inherit/beans.xml");
		Gradate ge=(Gradate) ac.getBean("gradate");
		System.out.println(ge.getName()+" "+ge.getAge()+" "+ge.getDegree());
	}
}</span>

五:测试结果

李四 22 学士

版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21

时间: 2024-08-29 16:55:42

Spring配置继承属性的相关文章

spring 配置

SpringMVC配置 引入占位符<context:property-placeholder location="classpath:config.properties" /><util:properties id="properties" location="classpath:config.properties"/> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --&

8 -- 深入使用Spring -- 1...4 属性占位符配置器

8.1.4 属性占位符配置器 PropertyPlaceholderConfigurer 是一个容器后处理器,负责读取Properties属性文件里的属性值,并将这些属性值设置成Spring配置文件的数据. 通过使用PropertyPlaceholderConfigurer后处理器,可以将Spring配置文件中的部分数据放在属性文件中设置. XML : <?xml version="1.0" encoding="UTF-8"?> <!-- Spri

spring 配置属性细节

苹果的WWDC ,除了发布了os x 10.10 和IOS8 外,还推出了Swift.详细点击这里 代码总体风格有点像Java,也有点像javascript. 下面给出一些代码段(来自苹果官方手册): println("Hello, world") "var myVariable = 42 myVariable = 50 let myConstant = 42" 摘录来自: Apple Inc. "The Swift Programming Languag

Spring框架笔记(四)——Spring容器的属性配置详解的六个专题

在spring IOC容器的配置文件applicationContext.xml里,有一些配置细节值得一提.我们将一些问题归结为以下几个专题. 专题一:字面值问题 配置的bean节点中的值,我们提出一个概念--字面值. 字面值:可用字符串表示的值. 字面值可以通过 <value> 元素标签或 value 属性进行注入. 基本数据类型及其封装类.String 等类型都可以采取字面值注入的方式 若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来. 例如:(本文

spring配置属性的两种方式

spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location="classpath:jdbctemplate/jdbc.properties" /> 第二种方式通过创建bean,对应类为PropertyPlaceholderConfigurer <bean id="propertyConfigurer" class=

Spring配置中的bean直接引用其它bean的属性值

pring配置中的bean直接引用其它bean的属性值来赋值给当前bean的属性,也可以直接调用其它bean的方法获取返回值来赋值给当前bean的属性,并且可以进行参数传递,这样可以省去在bean中注入需要获取属性值的bean. 以下是一个完整的示例: 1.需要JAVA类: Spring配置中的bean直接引用其它bean的属性值 package com.service.test; public class Bean1 { int v1 = 1; public int getV1() { ret

六 Spring的配置:属性注入

Spring的属性注入: 构造方法的属性注入 set方法的属性注入 构造方法的属性注入: set方法的属性注入: set方法注入对象: 1 package com.itheima.spring.demo4; 2 3 public class Employee { 4 private String name; 5 private Car2 car2; 6 7 public void setName(String name) { 8 this.name = name; 9 } 10 public v

SSH框架系列:Spring配置多个数据源

问题:有开源框架mysql的 ,还有旧系统 sqlserver2000的,解决这些问题总有些成长. 解决sqlserver安装环境:http://qdh68.blog.163.com/blog/static/13756126201261742437357/ 别说sqlserver2000不怎么样,最起码那友好的管理叫你明白数据库. 2.  先说配置jdbc:如果sqlserver 2000以上还好 找到jar包 ,按目录加载到maven仓库,安装一下 http://outofmemory.cn/

[转]@Transactional spring 配置事务 注意事项

@Transactional spring 配置事务 注意事项 [@[email protected]] @Transactional spring 配置事务 注意事项 1. 在需要事务管理的地方加@Transactional 注解.@Transactional 注解可以被应用于接口定义和接口方法.类定义和类的 public 方法上. 2. @Transactional 注解只能应用到 public 可见度的方法上. 如果你在 protected.private 或者 package-visib