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中继承配置的注入方法的更多相关文章
- spring中bean配置和注入场景分析
bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean,并 ...
- 关于spring中bean配置的几件小事
一.IOC和DI 1.IOC(Inversion of Control) 其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源:而应用了IOC之后 ...
- getHibernateTemplate()(Spring中常用的hql查询方法)
Spring中常用的hql查询方法(getHibernateTemplate()) --------------------------------- 一.find(String queryStrin ...
- 【坑】Spring中抽象父类属性注入,子类调用父类方法使用父类注入属性
运行环境 idea 2017.1.1 spring 3.2.9.RELEASE 需求背景 需要实现一个功能,该功能有2个场景A.B,大同小异 抽象一个抽象基类Base,实现了基本相同的方法BaseMe ...
- spring中bean配置和bean注入
1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...
- Spring boot将配置属性注入到bean类中
一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...
- Spring中一个类的注入和引用是不一样的
1.在Spring管理下的bean需要以下面这种方式引入(一种注入方式): private MgrService mgrService; public MgrService getMgrService ...
- Spring 中属性配置
1 注册自定义属性编辑器,方法一.使用BeanFactory, 则用户需要手动调用 registerCustomEditor(Class requiredType, PropertyEditor pr ...
- spring中xml配置和autowired混用
1.类的混用: 配置文件中的配置: <bean id="a" class="com.ab.cc.A" /> 类中的配置 @Autowired A a ...
随机推荐
- UVA 10317 - Equating Equations (背包)
Problem F Equating Equations Input: standard input Output: standard output Time Limit: 6 seconds Mem ...
- 复合文档的二进制存储格式研究[ole存储结构](word,xls,ppt...)[转]
复合文档文件格式研究 前 言 复合文档(Compound Document) 是一种不仅包含文本而且包括图形.电子表格数据.声音.视频图象以及其它信息的文档.可以把复合文档想象成一个所有者,它装着 ...
- PHP $_SERVER
$_SERVER 是一个包括了诸如头信息(header).路径(path).以及脚本位置(script locations)等等信息的数组.这个数组中的项目由 Web server创建.不能保证每一个 ...
- HDU5090模拟,hash
/* HDU 5090 算是一道简单模拟题.但当中有非常深的hash思想 这是本人的第一道hash题 更是本人的第一道纸质代码不带编译不带执行提交AC的题 值得纪念 废话讲这么多之后,讲述题中思想 因 ...
- linux 压缩和解压文件
一.压缩:20120715文件下面所有的文件 如下: tar -zcvf 20120715.tar.gz 20120715* 二.解压20120715.tar.gz压缩包 如下: tar -xzvf ...
- Appium 命令行启动配置
Appium 安装过后,会有图形界面,同样也支持命令行参数的启动和配置 Windws: 在windows 安装配置Appium有三种方式: Node install -g appium .exe文件安 ...
- android设置背景图片透明
设置Activiyt为透明可以在Activity中引用系统透明主题android:theme="@android:style/Theme.Translucent" 设置背景图片透明 ...
- Linux下的链接文件
整理自<鸟哥的Linux私房菜>,整理者:华科小涛http://www.cnblogs.com/hust-ghtao/ Linux中的链接文件分为两种:硬链接和软链接.软链接(symbol ...
- Android L动画入门
Android L带来了许多新特性,其中就包括了大量的动画效果,你可以在自己的应用中使用.本文中我将详解这些动画和如何在应用中使用.本文中的所有代码可以在github上找到. 波纹和强调 现在安卓支持 ...
- JMS学习的个人理解笔记
Jms即java消息服务javamessage service,所谓的面向消息编程,主要应用在企业内部各个系统之间做接口,以异步方式传递消息数据. Jms有2种传送模式,先来看第一种,即点对点传送模式 ...