(原)spring学习笔记4.普通属性注入
2009-02-12 09:54
465 查看
参见spring中文帮助.chm文档
代码参考whyspringofioc
1.首先来个Bean1类
1package com.zyl.spring;
2
3import java.util.Date;
4import java.util.List;
5import java.util.Map;
6import java.util.Set;
7
我们可以看到Bean1类中有很多各种各样的属性.相应的创建setter/getter
2.然后相应配置xml:
1<?xml version="1.0" encoding="UTF-8"?>
2<beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 6<!-- id名称是随便取的 -->
7<bean id="bean11" class="com.zyl.spring.Bean1">
8<!-- name的值是类中定义的变量名 -->
9 <property name="strValue" value="依赖注入的值"/>
<property name="intValue" value="12311"/>
<property name="listValue">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="setValue">
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
<property name="arrayValue">
<list>
<value>array1</value>
<value>array2</value>
</list>
</property>
<property name="mapValue">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
</map>
</property>
<property name="dateValue">
<value>2008/03/06</value>
</property>
</bean>
注意,属性名(property name)是Bean1类中的变量名哦.
通过value属性给属性赋值,也可以用ref来指定引用.
这样就可以了~
3.我们写一个junit来测试
package test;
import junit.framework.TestCase;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zyl.spring.Bean1;
import com.zyl.spring.Bean2;
package com.zyl.spring;
2
3import java.beans.PropertyEditorSupport;
4import java.text.SimpleDateFormat;
5import java.util.Date;
6
7
format的格式可以在xml中配置,让spring来注入吧.
5.下面看xml中时间编辑器的配置:
1<bean id="UtilDateEdit666666666" class="org.springframework.beans.factory.config.CustomEditorConfigurer"><!-- 这个class是api中 -->
2<!-- 将属性编辑器注入"<property name="customEditors">"这里面的customEditors是固定的 -->
3 <property name="customEditors"> <!-- 这个name是固定哦 -->
4 <map>
5 <entry key="java.util.Date">
6 <!-- 内部使用的类 -->
7 <bean class="com.zyl.spring.UtilDateEdit">
8 <property name="format" value="yyyy/MM/dd"/>
9 </bean>
</entry>
</map>
</property>
</bean>
这样就大功告成了.
打印效果:.(传说中见证奇迹的时刻?)
sdfs2008/03/06
strValue依赖注入的值
intValue12311
listValue[list1, list2, list3]
setValue[set1, set2]
strArray[Ljava.lang.String;@4e280c
mapValue{key1=value1, key2=value2}
dateValueThu Mar 06 00:00:00 CST 2008
华丽的分割线-=-=-=-=-=-=-=-=-=-
sdfs2008/03/06
当有时间的处理发生时,spring会在配置文件中找到并调用时间编辑器.至于为什么.....恩..参考下spring的代码吧.
这次就到这里吧 --end--;
代码参考whyspringofioc
1.首先来个Bean1类
1package com.zyl.spring;
2
3import java.util.Date;
4import java.util.List;
5import java.util.Map;
6import java.util.Set;
7
我们可以看到Bean1类中有很多各种各样的属性.相应的创建setter/getter
2.然后相应配置xml:
1<?xml version="1.0" encoding="UTF-8"?>
2<beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 6<!-- id名称是随便取的 -->
7<bean id="bean11" class="com.zyl.spring.Bean1">
8<!-- name的值是类中定义的变量名 -->
9 <property name="strValue" value="依赖注入的值"/>
<property name="intValue" value="12311"/>
<property name="listValue">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="setValue">
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
<property name="arrayValue">
<list>
<value>array1</value>
<value>array2</value>
</list>
</property>
<property name="mapValue">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
</map>
</property>
<property name="dateValue">
<value>2008/03/06</value>
</property>
</bean>
注意,属性名(property name)是Bean1类中的变量名哦.
通过value属性给属性赋值,也可以用ref来指定引用.
这样就可以了~
3.我们写一个junit来测试
package test;
import junit.framework.TestCase;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zyl.spring.Bean1;
import com.zyl.spring.Bean2;
package com.zyl.spring;
2
3import java.beans.PropertyEditorSupport;
4import java.text.SimpleDateFormat;
5import java.util.Date;
6
7
format的格式可以在xml中配置,让spring来注入吧.
5.下面看xml中时间编辑器的配置:
1<bean id="UtilDateEdit666666666" class="org.springframework.beans.factory.config.CustomEditorConfigurer"><!-- 这个class是api中 -->
2<!-- 将属性编辑器注入"<property name="customEditors">"这里面的customEditors是固定的 -->
3 <property name="customEditors"> <!-- 这个name是固定哦 -->
4 <map>
5 <entry key="java.util.Date">
6 <!-- 内部使用的类 -->
7 <bean class="com.zyl.spring.UtilDateEdit">
8 <property name="format" value="yyyy/MM/dd"/>
9 </bean>
</entry>
</map>
</property>
</bean>
这样就大功告成了.
打印效果:.(传说中见证奇迹的时刻?)
sdfs2008/03/06
strValue依赖注入的值
intValue12311
listValue[list1, list2, list3]
setValue[set1, set2]
strArray[Ljava.lang.String;@4e280c
mapValue{key1=value1, key2=value2}
dateValueThu Mar 06 00:00:00 CST 2008
华丽的分割线-=-=-=-=-=-=-=-=-=-
sdfs2008/03/06
当有时间的处理发生时,spring会在配置文件中找到并调用时间编辑器.至于为什么.....恩..参考下spring的代码吧.
这次就到这里吧 --end--;
相关文章推荐
- spring集合属性的注入学习笔记
- Spring视频学习笔记(2)属性注入
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
- Spring学习笔记--注入Bean属性
- Java框架spring 学习笔记(八):注入对象类型属性
- Java框架spring 学习笔记(六):属性注入
- spring in action 学习笔记(六)通过setter注入bean属性
- Spring学习笔记(8)----属性注入的方式
- 【JavaEE学习笔记】Spring_02_IoC/DI依赖注入,集合对象属性注入,自动装配,代理模式
- Spring学习笔记(8)----属性注入的方式
- Spring学习笔记(二)——注解创建对象和注入属性
- Spring通过容器获取配置对象及事件注入(学习笔记二)
- spring学习(四)—属性注入的四种方法(通过配置文件)
- spring 控制反转与依赖注入原理-学习笔记
- Spring学习笔记(10)——方法注入
- Java学习笔记38:通过Spring Bean 注入static变量,来设计一套适合测试,开发,生产环境的配置项
- Spring学习笔记-注入实战篇
- Spring学习笔记 通过PropertyPlaceholderConfigurer来使用properties文件初始化Map类型属性
- Spring学习笔记 关于Bean定义的重用-使用abstract以及parent标签属性
- 【Struts2学习笔记(3)】为Action的属性注入值