spring 初步

1、spring普通属性注入
	参见spring文档3.3.3章节
	
什么属性编辑器,作用?
	* 自定义属性编辑器,时将spring配置文件中的字符串转换成对象进行注入
	  spring已经有内置的属性编辑器,我们可以自定义属性编辑器

public class Trans extends PropertyEditorSupport {
	private String format;
	@Override
	public void setAsText(String arg0) throws IllegalArgumentException {
		SimpleDateFormat s=new SimpleDateFormat(format);
		try {
			Date d=s.parse(arg0);
			this.setValue(d);
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
	}
	public void setFormat(String format) {
		this.format = format;
	}
}
	  
如何自定义属性编辑器?
	* 继承PropertyEditorSupport类,覆写setAsText(),参见:UtilDatePropertyEditor.java
	* 将属性编辑器注入到spring中,参见:applicationContext-editor.xml
	
	<bean id="trans" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date" >
					<bean class="com.bjsxt.spring.Trans">
						<property name="format">
							<value>yyyy-MM-dd</value>
						</property>
					</bean>
				</entry>
			</map>
		</property>
	</bean>
		
---------------------------

	
依赖对象的注入,可以采用:
	* ref属性
	* <ref/>标签
	* 内部<bean>的定义
	
如何注入定义描述出来?
	* 通过<bean>标签将公共的部分定义出来,并指定abstract="true"
	* 具有相同属性的类指定其parent属性即可
	参见:applicationContext-other.xml		

 

Myeclipse

E:\192.168.0.150\11_Spring\spring-framework-2.0\dist\resources\spring-tx-2.0.xsd
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd


E:\192.168.0.150\11_Spring\spring-framework-2.0\dist\resources\spring-beans-2.0.xsd
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd


E:\192.168.0.150\11_Spring\spring-framework-2.0\dist\resources\spring-aop-2.0.xsd
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值