Spring4.0学习笔记(3) —— Spring_Bean之间的关系
1、继承关系
bean-relation.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="address" class="com.spring.relation.Address">
<property name="city" value="beijing^"></property>
<property name="location" value="los angles"></property>
</bean> <bean id="address2" parent="address">
<property name="location" value="new York"></property>
</bean>
</beans>
Address.java
package com.spring.autowire;
public class Address {
private String city;
private String location;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "Address [city=" + city + ", location=" + location + "]";
}
}
测试
package com.spring.relation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-relation.xml");
Address address = (Address)ctx.getBean("address");
System.out.println(address); Address address2 = (Address)ctx.getBean("address2");
System.out.println(address2); }
}
【拓展】 abstract = true的时候,该bean是无法被实例化的。可以去掉用于实例化的class 属性,这样该bean仅为模板bean
<bean id="address" class="com.spring.relation.Address" abstract="true">
<property name="city" value="beijing^"></property>
<property name="location" value="los angles"></property>
</bean> <bean id="address2" parent="address">
<property name="location" value="new York"></property>
</bean>
Spring4.0学习笔记(3) —— Spring_Bean之间的关系的更多相关文章
- Spring4.0学习笔记(11) —— Spring AspectJ 的五种通知
Spring AspectJ 一.基于注解的方式配置通知 1.额外引入的jar包: a) com.springsource.org.aopalliance-1.0.0.jar b) com.sprin ...
- Spring4.0学习笔记(10) —— Spring AOP
个人理解: Spring AOP 与Struts 的 Interceptor 拦截器 有着一样的实现原理,即通过动态代理的方式,将目标对象与执行对象结合起来,降低代码之间的耦合度,主要运用了Proxy ...
- Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...
- Spring4.0学习笔记(6) —— 通过工厂方法配置Bean
1.静态工厂方法: bean package com.spring.factory; public class Car { public Car(String brand) { this.brand ...
- Spring4.0学习笔记(5) —— 管理bean的生命周期
Spring IOC 容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务 Spring IOC 容器对Bean的生命周期进行管理的过程: 1.通过构造器或工厂方法 ...
- Spring4.0学习笔记(4) —— 使用外部属性文件
1.配置xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- Spring4.0学习笔记(2) —— 自动装配
Spring IOC 容器可以自动装配Bean,需要做的是在<bean>的autowire属性里指定自动装配的模式 1)byType 根据类型自动装配,若IOC 容器中有多个目标Bean类 ...
- Spring4.0学习笔记(1) —— 基础知识
1.基本定义 IOC: 其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源,而应用了 IOC之后,容器主动将资源推送给它所管理的组件,组件索要做 ...
- Spring4.0学习笔记(12) —— JDBCTemplate 操作数据库
整体配置 1.配置xml文件 <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi ...
随机推荐
- Qt中添加背景图片的方法
工作似乎走上正轨了,上周五的工作是做一个界面,用到QFrame和QPushButton,QFrame做主面板,QPushButton为其子控件,需要在主面板上贴背景图片,还需要在QPushButton ...
- Children of the Candy Corn (bfs+dfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8120 Accepted: 3547 Description The c ...
- COJ 1008 WZJ的数据结构(八) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...
- 无向图求割点 UVA 315 Network
输入数据处理正确其余的就是套强联通的模板了 #include <iostream> #include <cstdlib> #include <cstdio> #in ...
- 【转】Android的权限permission
原文网址:http://blog.csdn.net/nei504293736/article/details/6974599 <uses-permission android:name=&quo ...
- 2015第35周五JavaScript变量
java语言里有一句很经典的话:在java的世界里,一切皆是对象. Javascript虽然跟java没有半点毛关系,但是很多会使用javascript的朋友同样认为:在javascript的世界里, ...
- 向Int数组插入随机1到100
这是一个经典的面试题,考察了几个知识点 下边的代码是传统经典的做法 ]; ArrayList myList=new ArrayList(); Random rnd=new Random(); ) { ...
- Php面向对象 – 单例模式
Php面向对象 – 单例模式 保证类仅仅有一个实例 1. 怎样能够解决一个类能够被无限地实例化? New,就能实例化一次,怎么去限制,用户不能无限次地new? 将构造方法私有化.全部外部的new ...
- Android中Application类用法
Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...
- 不相交集python实现
1.不相交集是解决等价关系的一种数据结构,执行合并和查找的速度都很快,M次执行合并和查找的执行时间为(M*logN). 在一个集合中.对于每一对元素(a,b),a,b∈S,对于关系R假设满足以下三个条 ...