当前位置: 首页 > news >正文

Spring中继承配置的注入方法


(1)两个java类,一个父类一个字类

package com.lc.inherit;

/*
 * 这里是父类
 */
public class Student {
	
	protected String name;
	protected int age;
	public String getName() {
	//get/set方法
}

package com.lc.inherit;
/**
 * 继承Student类
 * @author xuliugen
 */
public class Gradate extends Student {

	//Gradate自由的属性
	private String degree;
	//get、set方法
	
}


(3)Spring注入使用的配置文件

<?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.lc.inherit.Student">
	<property name="name" value="顺平" />
	<property name="age" value="30"/>
</bean>

<!-- 配置Grdate对象 -->
<bean id="grdate" parent="student" class="com.lc.inherit.Gradate">
	<!-- 如果自己在这里配置属性name,age,则会替换从父对象继承的数据  没有配置的话按照父类-->
	<property name="name" value="小明"/>
	<property name="degree" value="学士"/>
</bean>

</beans>

(4)如何使用注入的值

package com.lc.inherit;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App1 {

	
	public static void main(String[] args) {

		ApplicationContext ac=new ClassPathXmlApplicationContext("com/lc/inherit/beans.xml");
	
		Gradate gradate=(Gradate) ac.getBean("grdate");
		System.out.println(gradate.getName()+" "+gradate.getAge()+" "+gradate.getDegree());
	}

}



相关文章:

  • Spring控制反转和依赖注入
  • springMVC框架搭建流程
  • 四舍五入的方法
  • Java代码生成二维码图片
  • 2.Cocos2dx 3.2重力系统Box2D
  • Ubuntu下安装C/C++开发环境【!!!有更新!!!Ubuntu14.10下使用eclipse搭建C语言开发环境】
  • 翻译一篇文章:It's Difficult to Grow a Test Developer(成为测试开发工程师的艰辛)...
  • Spring的核心机制:依赖注入
  • Spring获取ApplicationContext对象工具类
  • linux基础学习8
  • Ubuntu系统更新命令笔记
  • 大龄屌丝自学笔记--Java零基础到菜鸟--038
  • VMWare虚拟机提示:锁定文件失败,打不开磁盘的解决办法
  • HDU2030 汉字统计
  • Windows下删除不需要的服务
  • [译]CSS 居中(Center)方法大合集
  • 〔开发系列〕一次关于小程序开发的深度总结
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • HomeBrew常规使用教程
  • httpie使用详解
  • HTTP中的ETag在移动客户端的应用
  • iOS | NSProxy
  • JavaScript对象详解
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • 阿里研究院入选中国企业智库系统影响力榜
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 关于Java中分层中遇到的一些问题
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 免费小说阅读小程序
  • 面试总结JavaScript篇
  • 区块链将重新定义世界
  • 如何选择开源的机器学习框架?
  • 微信开源mars源码分析1—上层samples分析
  • python最赚钱的4个方向,你最心动的是哪个?
  • Salesforce和SAP Netweaver里数据库表的元数据设计
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #数学建模# 线性规划问题的Matlab求解
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (C语言)逆序输出字符串
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (WSI分类)WSI分类文献小综述 2024
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (转)负载均衡,回话保持,cookie
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .NET NPOI导出Excel详解
  • .NET 分布式技术比较
  • .net 桌面开发 运行一阵子就自动关闭_聊城旋转门家用价格大约是多少,全自动旋转门,期待合作...
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装
  • .Net面试题4
  • .NET正则基础之——正则委托
  • /var/log/cvslog 太大
  • ::
  • @RequestMapping处理请求异常