- 浏览: 1208286 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (718)
- HTML (13)
- JS基础 (23)
- JS应用 (40)
- AJAX (6)
- JSP相关 (12)
- JAVA基础 (52)
- JAVA应用 (74)
- APPLET (11)
- SWING\RCP (2)
- JAVA反射 (6)
- 设计模式 (26)
- 数据库设计 (20)
- Struts (35)
- Struts2 (12)
- Spring (22)
- Hibernate (45)
- Ibatis (18)
- mybatis (3)
- SSH (8)
- UML (5)
- WebService (3)
- XML (16)
- Log4j (7)
- WEB容器 (26)
- 数据结构 (36)
- Linux (34)
- Ruby on Rails (1)
- 其它技术 (27)
- IDE配置 (15)
- 项目实战 (2)
- Oracle (69)
- JAVA报表 (7)
- Android学习 (2)
- 博客链接 (1)
- 网络基础 (1)
- WEB集群 (1)
- .Net开发 (11)
- PB (4)
- 系统构建 (15)
最新评论
-
jnjeC:
牛逼啊哥们,讲得太好了
Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包 -
九尾狐的yi巴:
很好 感谢!
Itext中文处理(更新版) -
luweifeng1983:
有用的,重启一下嘛。
设置eclipse外部修改文件后自动刷新 -
Master-Gao:
设置了也不管用,怎么破呢?
设置eclipse外部修改文件后自动刷新 -
aigo_h:
锋子还有时间写博客,还是很闲哈!
Add directory entries问题
现有Bean2,Bean3,Bean4,Bean5
可观察到Bean2引用了其它三个Bean,而Bean3的属性为id,name,date;Bean4属性为id,name;Bean5为date
那么是不是可以将id,name抽取出来进行配置,从而相当于构建一个继承关系。
代码:
package com.lwf.bean; public class Bean2 { private Bean3 bean3; private Bean4 bean4; private Bean5 bean5; public Bean3 getBean3() { return bean3; } public void setBean3(Bean3 bean3) { this.bean3 = bean3; } public Bean4 getBean4() { return bean4; } public void setBean4(Bean4 bean4) { this.bean4 = bean4; } public Bean5 getBean5() { return bean5; } public void setBean5(Bean5 bean5) { this.bean5 = bean5; } }
package com.lwf.bean; import java.util.Date; public class Bean3 { private int id; private String name; private Date date; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
package com.lwf.bean; public class Bean4 { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.lwf.bean; public class Bean5 { private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package com.lwf.bean; import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class UtilDatePropertiesEditor extends PropertyEditorSupport { private String format="yyyy/MM/dd" ; public void setAsText(String text) throws IllegalArgumentException { SimpleDateFormat sdf = new SimpleDateFormat(format); try { Date dateVal = sdf.parse(text); this.setValue(dateVal); } catch (ParseException e) { e.printStackTrace(); } } public void setFormat(String format){ this.format = format; } }
配置文件:
applicationContext.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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <bean class="com.lwf.bean.UtilDatePropertiesEditor"> <property name="format" value="yyyy/MM/dd"/> </bean> </entry> </map> </property> </bean> <bean id="bean2" class="com.lwf.bean.Bean2"> <property name="bean3" ref="bean3"/> <property name="bean4"> <ref bean="bean4"/> </property> <property name="bean5"> <bean id="bean5" class="com.lwf.bean.Bean5"> <property name="age" value="30"></property> </bean> </property> </bean> </beans>
applicationContext_1.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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="parentBean" abstract="true"> <property name="id" value="1"/> <property name="name" value="zhang"/> </bean> <bean id="bean3" class="com.lwf.bean.Bean3" parent="parentBean"> <property name="date" value="2010/05/19"/> </bean> <bean id="bean4" class="com.lwf.bean.Bean4" parent="parentBean"/> </beans>
测试类:
package com.lwf.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lwf.bean.Bean2; public class Client { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext*.xml"); Bean2 bean2 = (Bean2)ctx.getBean("bean2"); System.out.println(bean2.getBean3().getName()); System.out.println(bean2.getBean3().getId()); System.out.println(bean2.getBean3().getDate()); System.out.println(bean2.getBean4().getId()); System.out.println(bean2.getBean4().getName()); System.out.println(bean2.getBean5().getAge()); } }
输出:
2010-05-19 10:14:28,122 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@bfbdb0: display name [org.springframework.context.support.ClassPathXmlApplicationContext@bfbdb0]; startup date [Wed May 19 10:14:28 CST 2010]; root of context hierarchy 2010-05-19 10:14:28,310 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\workdirlocal\spring_start\bin\applicationContext.xml] 2010-05-19 10:14:28,716 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\workdirlocal\spring_start\bin\applicationContext_1.xml] 2010-05-19 10:14:28,794 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@bfbdb0]: org.springframework.beans.factory.support.DefaultListableBeanFactory@16df84b 2010-05-19 10:14:28,981 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@16df84b: defining beans [customEditorConfigurer,bean2,parentBean,bean3,bean4]; root of factory hierarchy zhang 1 Wed May 19 00:00:00 CST 2010 1 zhang 30
注意在配置文件里面定义的继承关系
依赖对象的注入,可以采用: * ref属性 * <ref>标签 * 内部<bean>的定义 ---------------------------------------------------------------------------------------------------------- 如何将公共的注入定义描述出来: * 定义公共的注入属性(通过<bean>标签定义,指定abstract="true") * 具有相同注入属性的类,指定parent属性即可
发表评论
-
spring 3 和mybatis 3集成,并用junit4进行测试
2011-11-04 14:01 1431转:spring 3 和mybatis 3集成,并用junit ... -
java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource的解决方法
2011-11-03 16:17 2684用Myeclipse开发struts时,配置struts-co ... -
Strut2+Spring整合框架搭建
2011-11-02 22:19 1053参考:http://lukuijun.iteye.com/bl ... -
Spring+hibernate延迟加载报错解决办法之二
2010-06-29 17:28 1196在做删除操作的时候出现了org.springframework ... -
Spring+hibernate延迟加载报错解决办法之一
2010-06-29 17:25 1185我们在项目中一般都会使用Spring来管理Hibernate的 ... -
Spring项目中怎么配置log4j
2010-05-27 11:10 1557在spring项目中配置log4j http://blogg ... -
Spring与Struts集成方式三
2010-05-26 17:11 1123在集成方式一和二中我们是在web.xml中加入配置代码: & ... -
Spring与Struts集成方式二
2010-05-26 14:49 1021在集成方式一的基础上做改进: 第一种集成方案是在action ... -
Spring与Struts集成方式一
2010-05-25 14:13 888我们在Struts中在action类中调用Model层组件进行 ... -
Hibernate编程式事务与Spring Aop的声明式事务(spring与hibernate集成)
2010-05-24 17:15 2749采用编程式事务 事务主要分为:编程式事务和声明式事务 ... -
修改Eclipse配置,使得在配置文件中完成自动完成功能。
2010-05-24 15:10 2269在Eclipse中引入spring的配置文件applicati ... -
Spring aop 基于schema的AOP支持及JoinPoint的使用、如何使用CGLIB代理
2010-05-24 14:52 4099基于schema的aop只是将配置写到配置文件中。 代 ... -
Spring AOP 概念理解及@AspectJ支持
2010-05-20 15:56 1559为了更好的理解Spring简介一文http://quicker ... -
Spirng AOP简介
2010-05-19 17:28 2260AOP 面向切面编程(AOP)通过提供另外一种思考程序结构的 ... -
Spring Bean中的自动装配——byType
2010-05-19 17:08 1165自动装配byType即通过查找类属性在配置文件中bean中定义 ... -
Spring Bean中的自动装配——byName
2010-05-19 16:34 3125自动装配(autowire)协作者 Spring IoC容器 ... -
Spring Bean的作用域:用Spring来解决Struts中Action的单实例问题
2010-05-19 10:47 1536我们知道在Struts中Action是从map中拿出来的,是单 ... -
Spring Set注入:基本类型、List、Map、Set、Array、Date类型注入
2010-05-18 15:58 11504Spring依赖注入有两种:构造器注入与Set注入 其中以S ... -
spring 初探
2010-05-17 16:53 1227Spring核心设计思想为IOC ... -
简单DAO层示例
2010-05-14 17:30 2055在使用spring架构之前,我们怎么设计自己的DAO层的呢? ...
相关推荐
众所周知Spring里面的bean就类似是定义的一个组件,而这个组件的作用就是实现某个功能的,下面这篇文章主要给大家介绍了关于spring入门教程之bean继承与自动装配的相关资料,需要的朋友可以参考借鉴,下面随着小编来...
Spring系列第4篇:xml中bean定义详解(-)Spring系列第5篇:创建bean实例这些方式你们都知道?Spring系列第6篇:玩转bean scope,避免跳坑里!Spring系列第7篇:依赖注入之手动注入Spring系列第8篇:自动注入...
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.1.1. 使用BeanPostProcessor的Hello World示例 3.7.1.2. RequiredAnnotationBeanPostProcessor示例 3.7.2. 用...
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用...
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. The ApplicationContext 3.8.1. ...
10.2.2 装载Spring Bean至Tapestry页面中 10.3 集成JavaServer Faces 10.3.1 解析变量 10.3.2 发布请求已处理事件 10.4 集成WebWork 10.4.1 WebWork1 10.4.2 XWork/WebWork2 10.5 小...
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. The ApplicationContext 3.8.1. ...
1.13 继承Bean配置 47 1.13.1 问题 47 1.13.2 解决方案 47 1.13.3 工作原理 48 1.14 从Classpath中扫描组件 50 1.14.1 问题 50 1.14.2 解决方案 51 1.14.3 工作原理 51 1.15 小结 56 第2章 高级...
第一个 Spring MVC 程式 WebApplicationContext Handler Mapping Handler Interceptor Controller 继承架构 ModelAndView View Resolver Exception Resolver 使用 Controller ...
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用...
4.7 Bean 定义的继承. 77 4.8 容器扩展点. 78 4.8.1 使用BeanPostProcessor 来自定义bean 78 4.8.1.1 示例:BeanPostProcessor 风格的Hello World.. 79 4.8.1.2 示例:...
1.13 继承Bean配置 47 1.13.1 问题 47 1.13.2 解决方案 47 1.13.3 工作原理 48 1.14 从Classpath中扫描组件 50 1.14.1 问题 50 1.14.2 解决方案 51 1.14.3 工作原理 51 1.15 小结 56 第2章 高级...
4.11.1 使用Java类提供Bean定义信息 4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition ...
其中BeforeAdvice继承自Adivce接口,这两者都是标签接口,并没有定义任何具体的方法。before方法会在目标对象的指定方法执行之前被执行,在before方法种,你可以取得指定方法的Method实例、参数列表和目标对象,在...
中文叫做一次性控制器,也就像一次性筷子那样用完即丢 并不在Spring的Controller继承架构中,而是一个独立的接口定义 当映射的不是一个model 对象时很有用 Bean的作用域必须是prototype ,他们本身不是线程安全的
4.11.1 使用Java类提供Bean定义信息 4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition ...
Web Bean 甚至提供了必要的整合点来让未来 Java EE 规格或是非标准架构所定义的其它类型组件能够利落地与 Web Bean 整合、有效利用 Web Bean 服务,并与任何其它类型的 Web Bean 进行互动。 Web Bean 受到了几个现有...
Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式,如图 1 所示。 组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下: ☆...
第3~9行定义了一个数据源,其实现类是apache的BasicDataSource,第11~25行定义了Hibernate的会话工厂,会话工厂类用Spring提供的LocalSessionFactoryBean维护,它注入了数据源和资源映射文件,此外还通过一些键值...
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述............................2.5.3.2 使用组件定义bean的元数据..............................................................