(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. hdu1200(来来回回串起来)

    Problem Description Mo and Larry have devised a way of encrypting messages. They first decide secret ...

  2. VB.net总结

    .NET视频差点儿相同用时一周结束,总体感觉就是"走耳不走脑".刚開始看视频理解起来有一点困难,台湾口音以及台湾与大陆计算机术语的差异,让我把前几集相当于直接忽略过了(建议打算看这 ...

  3. gdb 远程qemu-arm调试

    把 c 编译成 arm 指令的可运行文件 /usr/bin/arm-linux-gnueabi-g++ hello.cpp cat hello.cpp #include <stdio.h> ...

  4. sqlserver 存储过程实例

    ALTER PROC [dbo].[SP_mm_NS] (        @ID        NVARCHAR(60),        @ReturnCode    NVARCHAR(30) OUT ...

  5. 基于visual Studio2013解决面试题之0702输出数字

     题目

  6. 程序中非action获得spring容器

    java类: public class MySpringContext implements ApplicationContextAware{ private static ApplicationCo ...

  7. C# LDAP认证登录

    LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根据需要定制.与X ...

  8. 【每日一摩斯】-Troubleshooting: High CPU Utilization (164768.1) - 系列4

    Jobs (CJQ0, Jn, SNPn) Job进程运行用户定义的以及系统定义的类似于batch的任务.检查Job进程占用大量CPU资源的方法,就像检查用户进程一样. 可以根据以下视图检查Job进程 ...

  9. 【cocos2d-js公文】十七、事件分发机制

    简单介绍 游戏开发中一个非常重要的功能就是交互,假设没有与用户的交互.那么游戏将变成动画,而处理用户交互就须要使用事件监听器了. 总概: 事件监听器(cc.EventListener) 封装用户的事件 ...

  10. QNX系统-关于delay函数与sleep函数的区别

    QNX是类unix系统.在c语言编程过程中,往往会用到delay或者sleep延时函数.两者之间在使用上有一定的区别!!! delay()是循环等待,该进程还在运行,占用处理器. sleep()不同, ...