(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中继承配置的注入方法的更多相关文章

  1. spring中bean配置和注入场景分析

    bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean,并 ...

  2. 关于spring中bean配置的几件小事

    一.IOC和DI 1.IOC(Inversion of Control) 其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源:而应用了IOC之后 ...

  3. getHibernateTemplate()(Spring中常用的hql查询方法)

    Spring中常用的hql查询方法(getHibernateTemplate()) --------------------------------- 一.find(String queryStrin ...

  4. 【坑】Spring中抽象父类属性注入,子类调用父类方法使用父类注入属性

    运行环境 idea 2017.1.1 spring 3.2.9.RELEASE 需求背景 需要实现一个功能,该功能有2个场景A.B,大同小异 抽象一个抽象基类Base,实现了基本相同的方法BaseMe ...

  5. spring中bean配置和bean注入

    1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...

  6. Spring boot将配置属性注入到bean类中

    一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...

  7. Spring中一个类的注入和引用是不一样的

    1.在Spring管理下的bean需要以下面这种方式引入(一种注入方式): private MgrService mgrService; public MgrService getMgrService ...

  8. Spring 中属性配置

    1 注册自定义属性编辑器,方法一.使用BeanFactory, 则用户需要手动调用 registerCustomEditor(Class requiredType, PropertyEditor pr ...

  9. spring中xml配置和autowired混用

    1.类的混用: 配置文件中的配置: <bean id="a" class="com.ab.cc.A" /> 类中的配置 @Autowired A a ...

随机推荐

  1. 教师简介 (Alma Del Tango的小站)

    教师简介 (Alma Del Tango的小站) Esteban Peng (TT) & Emilia Jia (Amy) TT和Amy是北京极具影响力的专业舞者,他们从07年开始推广阿根廷探 ...

  2. 基于W5500+Yeelink的远程灯光控制设计

    概述 工具:物联网云平台Yeelink  DHT11温湿度传感器   W5500EVB 编译环境:Keil4 目的:通过以太网实时监控远程某个位置的温度和湿度 在W5500EVB端连接LED灯.通过W ...

  3. hdu 1421 搬寝室 (dp)

    思路分析: dp[i][j] 表示选取到第 i 个   组成了 j 对的最优答案. 当然排序之后 选取相邻两个是更优的. if(i==j*2) dp[i][j] = dp[i-2][j-1] + w[ ...

  4. ASP.NET中IsPostBack详解(转载)

    1.IsPostBack介绍Page.IsPostBack是一个标志:当前请求是否第一次打开. 调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者 ...

  5. Java面试题精选(三) JSP/Servlet Java面试逻辑题

    --   JSP/Servlet  Java面试逻辑题   --     很显然,Servlet/JSP的WEB前端动态制作的重要性比HTML/CSS/JS的价值高很多,但我们都知道他们都是建立在HT ...

  6. Python中列表的常用操作

    只整理重要常用的操作: append():尾部追加元素,参数只能为一个. extend():用列表扩展列表,参数为列表. insert():在指定位置插入元素,第一个参数为插入位置,第二个为参数为插入 ...

  7. Apriori算法实现

    Apriori算法原理:http://blog.csdn.net/kingzone_2008/article/details/8183768 import java.util.HashMap; imp ...

  8. ajax基础入门

    补充一下Ajax的使用方法 //可以复制下面两种方法在百度上实验 //jquery的使用方法 $.ajax({ url:"index.php", success:function( ...

  9. hdu1114

    完全背包的水题,不过今天才学动态规划,就这样啦……hahahah!!! 完全背包跟普通背包的区别是普通背包从后往前循环,以防止被替换 完全背包是从前往后循环,后面的状态会跟着之前状态的改变而改变…… ...

  10. hbulider mui框架

    1.webview http://www.dcloud.io/docs/api/zh_cn/webview.shtml#plus.webview.WebviewStyle http://www.dcl ...