Spring学习笔记之Bean定义继承
2007-05-31 23:33
405 查看
若Bean定义文件内容很多,而且部分Bean的定义有重复,如属性相同,只有几个Bean有不同 的设置,则可以考虑继承某个Bean定义,好处多多:
People类
package com.nalis.spring;
public class People ...{
private String name;
private int age;
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
}
src目录下的beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd">
<bean id="inheritedPeople" abstract="true">
<property name="name" value="guest" />
<property name="age" value="18" />
</bean>
<bean id="temp1" class="com.nalis.spring.People"
parent="inheritedPeople">
<property name="name" value="nalis" />
<property name="age" value="24" />
</bean>
</beans>
测试类:BeaninheritedDemo
package com.nalis.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeaninheritedDemo ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(
"beans.xml");
People people = (People) context.getBean("temp1");
System.out.println("name: " + people.getName());
System.out.println("age: " + people.getAge());
}
}
此上是完全抽象的Bean定义继承,还可以从一个Bean实例的定义中继承
<bean id="people" class="com.nalis.spring.People">
<property name="name" value="zhang3" />
<property name="age" value="44" />
</bean>
<bean id="temp2" class="com.nalis.spring.People" parent="people">
<property name="name" value="li4" />
</bean>
此例"people"已经实例化,若有必要,可用其他Bean(如"temp2")继承其定义.
People类
package com.nalis.spring;
public class People ...{
private String name;
private int age;
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
}
src目录下的beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd">
<bean id="inheritedPeople" abstract="true">
<property name="name" value="guest" />
<property name="age" value="18" />
</bean>
<bean id="temp1" class="com.nalis.spring.People"
parent="inheritedPeople">
<property name="name" value="nalis" />
<property name="age" value="24" />
</bean>
</beans>
测试类:BeaninheritedDemo
package com.nalis.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeaninheritedDemo ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(
"beans.xml");
People people = (People) context.getBean("temp1");
System.out.println("name: " + people.getName());
System.out.println("age: " + people.getAge());
}
}
此上是完全抽象的Bean定义继承,还可以从一个Bean实例的定义中继承
<bean id="people" class="com.nalis.spring.People">
<property name="name" value="zhang3" />
<property name="age" value="44" />
</bean>
<bean id="temp2" class="com.nalis.spring.People" parent="people">
<property name="name" value="li4" />
</bean>
此例"people"已经实例化,若有必要,可用其他Bean(如"temp2")继承其定义.
相关文章推荐
- Java框架spring 学习笔记(五):Bean定义继承
- Spring 学习笔记之 Bean 定义的继承
- Spring学习笔记 关于Bean定义的重用-使用abstract以及parent标签属性
- spring学习笔记(5)——bean的继承与依赖关系
- 【Spring学习笔记-5】Spring中的抽象bean以及bean继承
- spring学习笔记(3)Bean命名、定义与配置
- Spring 学习笔记2 - Bean的定义与Ioc的使用
- Spring学习笔记 关于Bean定义的重用-使用abstract以及parent标签属性
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- Spring源码学习IOC(4):IoC容器解析Bean定义资源并注册解析后的Bean
- JavaWeb学习笔记之Spring中bean的作用域
- Spring核心Bean工厂装配__Spring学习笔记
- Spring学习笔记(六) Bean的作用域 & Spel 表达式
- Effective C# 学习笔记(二十二)多用接口定义实现,少用继承
- [Spring Data Repositories]学习笔记--定义自己的repository
- spring in action 学习笔记(四)bean的作用域
- [原创]java WEB学习笔记97:Spring学习---Spring 中的 Bean 配置:IOC 和 DI
- [原创]java WEB学习笔记100:Spring学习---Spring Bean配置:SpEL详细介绍及代码演示
- Spring学习笔记 创建自动扫描并注入的bean
- Spring学习笔记(二)Spring的三种实例化Bean的方式