Spring支持bean配置继承。我们可以定义一个bean,然后进一步配置它来创建新的bean。
通过使用bean继承,我们可以共享公共值,属性或配置。
子bean继承其父bean配置,属性和属性。
此外,子bean可以覆盖继承的值。
package com.www..cnmon; public class Customer { private int type; private String action; private String Country; public int getType() { return type; }//from w ww.ja v a 2 s.co m public void setType(int type) { this.type = type; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } public String getCountry() { return Country; } public void setCountry(String country) { Country = country; } @Override public String toString() { return "Customer [type=" + type + ", action=" + action + ", Country=" + Country + "]"; } }
此外,子bean可以覆盖继承的值。...
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="generalCustomer" class="com.www..cnmon.Customer"> <property name="country" value="USA" /> </bean> <bean id="specialCustomer" parent="generalCustomer"> <property name="action" value="backup" /> <property name="type" value="1" /> </bean> </beans>
在上面“generalCustomer"bean的配置文件中包含国家财产的“美国"值。
在上面“generalCustomer"bean的配置文件中包含国家财产的“美国"值。...
这里是运行此应用程序的代码。
package com.www..cnmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); Customer cust = (Customer)context.getBean("specialCustomer"); System.out.println(cust); } }
上面的代码生成以下结果。
客户[type = 1,操作=备份,国家=美国]
在上面的例子中,我们仍然可以实例化“generalCustomer"bean如下。
Customer cust = (Customer)context.getBean("generalCustomer");
要使此基本bean作为模板,并且不允许Java代码实例化它,在< bean>中添加“abstract"属性。 元件。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="generalCustomer" class="com.www..cnmon.Customer" abstract="true"> <property name="country" value="USA" /> </bean> <bean id="specialCustomer" parent="generalCustomer"> <property name="action" value="backup" /> <property name="type" value="1" /> </bean> </beans>
在bean定义中将“generalCustomer"bean标记为抽象之后。它成为bean继承的纯模板。
如果我们尝试实例化它,我们将遇到以下错误消息。
Customer cust = (Customer)context.getBean("generalCustomer");
org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name "generalCustomer": Bean definition is abstract
父bean不需要类属性,它只需要共享的共享属性。
这里的是一个例子
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="generalCustomer" abstract="true"> <property name="country" value="USA" /> </bean> <bean id="specialCustomer" parent="generalCustomer" class="com.www..cnmon.Customer"> <property name="action" value="backup" /> <property name="type" value="1" /> </bean> </beans>
在上面的xml代码中, generalCustomer
bean是一个模板托管其他bean的公共属性以继承。
我们可以通过在子bean中指定新值来覆盖继承的值。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="generalCustomer" class="com.www..cnmon.Customer" abstract="true"> <property name="country" value="USA" /> </bean> <bean id="specialCustomer" parent="generalCustomer"> <property name="country" value="Japan" /> <property name="action" value="backup" /> <property name="type" value="1" /> </bean> </beans>
“specialCustomer"bean覆盖父“generalCustomer"国家财产,从“美国"到“日本"。
上面的代码生成以下结果。
fmt:message 标签 fmt:message标签映射一个关键字给局部消息,然后执行参数替换。语法格式fmt:message key=string bundle=string...
fn:endsWith()函数 fn:endsWith()函数用于确定一个字符串是否以指定后缀结尾。 语法 fn:endsWith()函数的语法如下:c:if test=${...
fn:substringAfter()函数 fn:substringAfter()函数返回字符串中指定子串后面的部分。语法 fn:substringAfter()函数的语法如下:$...
sql:dateParam标签与sql:query标签和sql:update标签嵌套使用,用来提供日期和时间的占位符。如果是一个null值,则将占位符...