Spring 定义:abstract="true"
bean定义的继承
在bean定义中包含了大量的配置信息,其中包括容器相关的信息(比如初始化方法、静态工厂方法名等等)以及构造器参数和属性值。子bean定义就是从父bean定义继承配置数据的bean定义。它可以覆盖父bean的一些值,或者添加一些它需要的值。使用父/子bean定义的形式可以节省很多的输入工作。实际上,这就是一种模板形式。
当以编程的方式使用BeanFactory
时,子bean定义用ChildBeanDefinition
类表示。大多数用户从来不需要以这个方式使用它们,而是以类似XmlBeanFactory
中的声明方式去配置bean定义。当使用基于XML的配置元数据时,给'parent'
属性指定值,意味着子bean定义的声明。
<bean id="inheritedTestBean" abstract="true"
class="org.springframework.beans.TestBean">
<property name="name" value="parent"/>
<property name="age" value="1"/>
</bean>
<bean id="inheritsWithDifferentClass"
class="org.springframework.beans.DerivedTestBean"
parent="inheritedTestBean" init-method="initialize">
<property name="name" value="override"/>
<!-- the age property value of 1 will be inherited from parent -->
</bean>
如果子bean定义没有指定class属性,它将使用父bean定义的class属性,当然也可以覆盖它。在后面一种情况中,子bean的class属性值必须同父bean兼容,也就是说它必须接受父bean的属性值。
一个子bean定义可以从父bean继承构造器参数值、属性值以及覆盖父bean的方法,并且可以有选择地增加新的值。如果指定了init-method,destroy-method和/或静态
factory-method,它们就会覆盖父bean相应的设置。
剩余的设置将总是从子bean定义处得到:依赖、自动装配模式、依赖检查、singleton、作用域和延迟初始化。
注意在上面的例子中,我们使用abstract属性显式地将父bean定义标记为抽象的。 下面是个父bean定义并没有指定class属性的例子,其中父bean必须显式地标上abstract
:
<bean id="inheritedTestBeanWithoutClass" abstract="true">
<property name="name" value="parent"/>
<property name="age" value="1"/>
</bean>
<bean id="inheritsWithClass" class="org.springframework.beans.DerivedTestBean"
parent="inheritedTestBeanWithoutClass" init-method="initialize">
<property name="name" value="override"/>
<!-- age will inherit the value of1
from the parent bean definition-->
</bean>
由于这样的父bean是不完整的,而且还被显式标记为抽象的
,因而它无法得到自己的实例。抽象
bean定义可作为子bean定义的模板。若要尝试单独使用这样的父bean(比如将它作为其他bean的ref属性而引用,或者直接使用这个父bean的id作为参数调用getBean()
方法),将会导致错误。同样地,容器内部的preInstantiateSingletons()
方法会完全忽略abstract的bean定义。
注意
默认情况下,ApplicationContext
(不是BeanFactory
)会预实例化所有singleton的bean。因此很重要的一点是:如果你只想把一个(父)bean定义当作模板使用,而它又指定了class属性,那么你就得将'abstract'属性设置为'true',否则应用上下文将会(试着)预实例化抽象
bean。
相关推荐
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc=...
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context=...
<?xml version="1.0" ...//www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
Spring4.0+Hibernate4.0+Struts2.3整合案例:实现增删改查。 ===================== application.xml: <?xml version="1.0" encoding="UTF-8"?> xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...
spring-ibatis 整合 springmvc 配置: <?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:mvc=...
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ...
springmvc笔记 新建或拷贝applicationContext.xml <beans xmlns="http://www.springframework.org/schema/beans" ... xmlns:p=... http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
asp.net_spring_nhibernate_oracle 网上关于在.net开发的例子不多,这个例子能跑,遗憾的是,nhibernate在存储过程不完美,在oracle 传回多结果集,只能接受到第一个,官网上也是这样说的。 提示:开发前,先将...
<property name="defaultAutoCommit" value="true" /> </bean> <!-- jdbc注入 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref=...
【项目源码】java京东商城购物网站 <?xml version="1.0" ... http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org
* Spring enterprise: Spring Java EE integration, Spring Integration, Spring Batch, jBPM with Spring, Spring Remoting, messaging, transactions, scaling using Terracotta and GridGrain, and more. ...
web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示: ...
spring声明式事务的配置 3. spring2.0配置事务 a) 将spring 1.2升级到spring2.0 i. 去掉spring1.2相关的包 ii. 添加spring2.0的jar包:spring.jar,aspecjrt.jar,aspectjweaver.jar 和cglib-nodep-2.1.3,jar iii. ...
这是一个springmvc-config.xml文件,<?xml version="1.0" encoding="UTF-8"?> ... xmlns:mvc=... http://www.springframework.org/schema/context/spring-context-4.2.xsd">
## Spring入门学习 首先认识下Spring的结构 ![架构图](http://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/image001.gif) 然后我们皆可以写我们的demo了 ### 我们的Bean类 对于bean的理解,希望...
主要功能如下: ===== 1、数据库 ====== Druid数据库连接池,监控数据库访问性能,详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助。...mvn clean package -Dmaven.test.skip=true -P test
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class=...
<tx:method name="get*" read-only="true"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED...
* spring的事务管理需要添加到业务逻辑上(事务边界的定义),不要回到Dao上 技术提示: 在编写经下代码时: <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" ...
一个可以将.class文件反编译回.java的工具,java反编译工具:XJad2.2,可打开.class 电脑端用,java jar模拟器:KEmulator 功能特点: ======== 1、XJad是基于Jad核心的Java源程序反编译软件,内置Jad1.5.8e2; 2...