- 浏览: 1229570 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (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问题
Spring依赖注入有两种:构造器注入与Set注入
其中以Set注入为首选。下面演示几个示例。
Bean类:User
package com.lwf.bean; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; public class User { private String name; private int age; private List listValues; private Map map ; private Set set; private String [] array; private Date date; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List getListValues() { return listValues; } public void setListValues(List listValues) { this.listValues = listValues; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Set getSet() { return set; } public void setSet(Set set) { this.set = set; } public String[] getArray() { return array; } public void setArray(String[] array) { this.array = array; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
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="userDaoImp4MySql" class="com.lwf.dao.UserDaoImp4MySql"/> <bean id="userDaoImp4Oracle" class="com.lwf.dao.UserDaoImp4Oracle"/> <bean id="userManager" class="com.lwf.manager.UserManagerImp"> <property name="userDao" ref="userDaoImp4Oracle"/> </bean> <bean id="upperAction" class="com.lwf.action.UpperAction"> <property name="message" value="good"/> </bean> <bean id="lowerAction" class="com.lwf.action.LowerAction"/> <bean id="user" class="com.lwf.bean.User"> <property name="name"><value>zhangdong</value></property> <property name="age" value="23" /> <property name="listValues"> <list> <value>list1</value> <value>list2</value> </list> </property> <property name="array"> <list> <value>array1</value> <value>array2</value> </list> </property> <property name="map"> <map> <entry> <key ><value>testCaseName</value></key> <value>testSpring</value> </entry> <entry> <key ><value>testCaseName1</value></key> <value>testSpring1</value> </entry> </map> </property> <property name="set"> <set> <value>set1</value> <value>set2</value> </set> </property> </bean> </beans>
测试类:
package com.lwf.client; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.lwf.bean.User; public class Client { public static void main(String[] args) { ApplicationContext ctx2 = new FileSystemXmlApplicationContext("/src/applicationContext.xml"); User user = (User)ctx2.getBean("user"); System.out.println(user.getName() +":"+ user.getAge()); List list = user.getListValues(); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } Map map = user.getMap(); for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { Map.Entry object = (Map.Entry) iter.next(); System.out.println((String)object.getKey() +":"+ (String)object.getValue()); } Set set = user.getSet(); for(Iterator iter = set.iterator();iter.hasNext();){ String str = (String)iter.next(); System.out.println(str); } String[] array = user.getArray(); System.out.println(array); } }
说明:上例测试了普通属性的注入,List,Map,Array,Set属性的注入。
测试打印出内容:
2010-05-18 15:56:16,794 INFO [org.springframework.context.support.FileSystemXmlApplicationContext] - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@3e86d0: display name [org.springframework.context.support.FileSystemXmlApplicationContext@3e86d0]; startup date [Tue May 18 15:56:16 CST 2010]; root of context hierarchy 2010-05-18 15:56:16,997 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\workdirlocal\spring_start\src\applicationContext.xml] 2010-05-18 15:56:17,388 INFO [org.springframework.context.support.FileSystemXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@3e86d0]: org.springframework.beans.factory.support.DefaultListableBeanFactory@184ec44 2010-05-18 15:56:17,450 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@184ec44: defining beans [userDaoImp4MySql,userDaoImp4Oracle,userManager,upperAction,lowerAction,user]; root of factory hierarchy zhangdong:23 list1 list2 testCaseName:testSpring testCaseName1:testSpring1 set1 set2 [Ljava.lang.String;@1d6776d
好,我们的User类中定义了Date date,现在测试Date类型,现在在applicationContext.xml中加入:
<property name="date" value="2010/05/18"></property>
在测试类中加入:
System.out.println(user.getDate());
测试结果:
我们会发现报错:
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'date'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
显然从字符串类型转换为Date类型出错,那么怎么办?
回忆一下Struts中actionForm怎么处理类型转换问题?
http://quicker.iteye.com/admin/blogs/629603
在spring中。我们采取类似的思路,先自己创建转换器再注册。
转换器继承于java.bean.下面的类:
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 ; 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中加入
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> </bean>
按F3查找类org.springframework.beans.factory.config.CustomEditorConfigurer,定位spring源代码到src目录,即可看到CustomEditorConfigurer类的源码
/* * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.beans.factory.config; import java.beans.PropertyEditor; import java.util.Iterator; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.beans.FatalBeanException; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.core.Ordered; import org.springframework.util.ClassUtils; /** * {@link BeanFactoryPostProcessor} implementation that allows for convenient * registration of custom {@link PropertyEditor property editors}. * * <p>As of Spring 2.0, the recommended usage is to use custom * {@link PropertyEditorRegistrar} implementations that in turn register * any desired editors on a given * {@link org.springframework.beans.PropertyEditorRegistry registry}. * Each PropertyEditorRegistrar can register any number of custom editors. * * <pre class="code"> * <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> * <property name="propertyEditorRegistrars"> * <list> * <bean class="mypackage.MyCustomDateEditorRegistrar"/> * <bean class="mypackage.MyObjectEditorRegistrar"/> * </list> * </property> * </bean></pre> * * <p>Alternative configuration example with custom editor classes: * * <pre class="code"> * <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> * <property name="customEditors"> * <map> * <entry key="java.util.Date" value="mypackage.MyCustomDateEditor"/> * <entry key="mypackage.MyObject" value="mypackage.MyObjectEditor"/> * </map> * </property> * </bean></pre> * * <p>Also supports "java.lang.String[]"-style array class names and primitive * class names (e.g. "boolean"). Delegates to {@link ClassUtils} for actual * class name resolution. * * <p><b>NOTE:</b> Custom property editors registered with this configurer do * <i>not</i> apply to data binding. Custom editors for data binding need to * be registered on the {@link org.springframework.validation.DataBinder}: * Use a common base class or delegate to common PropertyEditorRegistrar * implementations to reuse editor registration there. * * @author Juergen Hoeller * @since 27.02.2004 * @see java.beans.PropertyEditor * @see org.springframework.beans.PropertyEditorRegistrar * @see ConfigurableBeanFactory#addPropertyEditorRegistrar * @see ConfigurableBeanFactory#registerCustomEditor * @see org.springframework.validation.DataBinder#registerCustomEditor * @see org.springframework.web.servlet.mvc.BaseCommandController#setPropertyEditorRegistrars * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder */ public class CustomEditorConfigurer implements BeanFactoryPostProcessor, BeanClassLoaderAware, Ordered { protected final Log logger = LogFactory.getLog(getClass()); private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered private PropertyEditorRegistrar[] propertyEditorRegistrars; private Map customEditors; private boolean ignoreUnresolvableEditors = false; private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); public void setOrder(int order) { this.order = order; } public int getOrder() { return this.order; } /** * Specify the {@link PropertyEditorRegistrar PropertyEditorRegistrars} * to apply to beans defined within the current application context. * <p>This allows for sharing <code>PropertyEditorRegistrars</code> with * {@link org.springframework.validation.DataBinder DataBinders}, etc. * Furthermore, it avoids the need for synchronization on custom editors: * A <code>PropertyEditorRegistrar</code> will always create fresh editor * instances for each bean creation attempt. * @see ConfigurableListableBeanFactory#addPropertyEditorRegistrar */ public void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars) { this.propertyEditorRegistrars = propertyEditorRegistrars; } /** * Specify the custom editors to register via a {@link Map}, using the * class name of the required type as the key and the class name of the * associated {@link PropertyEditor} as value. * <p>Also supports {@link PropertyEditor} instances as values; however, * this is deprecated since Spring 2.0.7 and will be removed in Spring 3.0. * @param customEditors said <code>Map</code> of editors (can be <code>null</code>) * @see ConfigurableListableBeanFactory#registerCustomEditor */ public void setCustomEditors(Map customEditors) { this.customEditors = customEditors; } /** * Set whether unresolvable editors should simply be skipped. * Default is to raise an exception in such a case. * <p>This typically applies to either the editor class or the required type * class not being found in the classpath. If you expect this to happen in * some deployments and prefer to simply ignore the affected editors, * then switch this flag to "true". */ public void setIgnoreUnresolvableEditors(boolean ignoreUnresolvableEditors) { this.ignoreUnresolvableEditors = ignoreUnresolvableEditors; } public void setBeanClassLoader(ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; } public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (this.propertyEditorRegistrars != null) { for (int i = 0; i < this.propertyEditorRegistrars.length; i++) { beanFactory.addPropertyEditorRegistrar(this.propertyEditorRegistrars[i]); } } if (this.customEditors != null) { for (Iterator it = this.customEditors.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); Object value = entry.getValue(); Class requiredType = null; try { if (key instanceof Class) { requiredType = (Class) key; } else if (key instanceof String) { requiredType = ClassUtils.forName((String) key, this.beanClassLoader); } else { throw new IllegalArgumentException( "Invalid key [" + key + "] for custom editor: needs to be Class or String."); } if (value instanceof PropertyEditor) { beanFactory.registerCustomEditor(requiredType, (PropertyEditor) value); } else if (value instanceof Class) { beanFactory.registerCustomEditor(requiredType, (Class) value); } else if (value instanceof String) { Class editorClass = ClassUtils.forName((String) value, this.beanClassLoader); beanFactory.registerCustomEditor(requiredType, editorClass); } else { throw new IllegalArgumentException("Mapped value [" + value + "] for custom editor key [" + key + "] is not of required type [" + PropertyEditor.class.getName() + "] or a corresponding Class or String value indicating a PropertyEditor implementation"); } } catch (ClassNotFoundException ex) { if (this.ignoreUnresolvableEditors) { logger.info("Skipping editor [" + value + "] for required type [" + key + "]: " + (requiredType != null ? "editor" : "required type") + " class not found."); } else { throw new FatalBeanException( (requiredType != null ? "Editor" : "Required type") + " class not found", ex); } } } } } }
我们可以看前面注释部分其实已经告诉我们怎么做了。
就是注入customEditors属性,注意看源代码customEditors属性是一个Map。
那么在applicationContext.xml前面加入:
<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>
注意在注册的同时,我们为UtilDatePropertiesEditor注入了format属性,这个属性可以指定格式。
后面仍然保持代码:
<property name="date" value="2010/05/18"></property>
测试类代码中打印:System.out.println(user.getDate());
输出为:
Tue May 18 00:00:00 CST 2010
好了,现在我想改变格式如改成:2010-05-19。即
<property name="date" value="2010-05-19"></property>
那么顺便修改
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <bean class="com.bjsxt.spring.UtilDatePropertyEditor"> <property name="format" value="yyyy-MM-dd"/> </bean> </entry> </map> </property> </bean>
那么打印日期输出:
Wed May 19 00:00:00 CST 2010
说明我们的日期转换并注册成功。
以上可得:
什么是属性编辑器,其作用? * 自定义属性编辑器,是将spring配置文件中的字符串转换成相应的对象进行注入,spring已经内置了很多类型 的编辑器,我们可以自定义自己的属性编辑器 如何自定义属性编辑器? * 继承PropertyEditorSupport,覆写如下方法(参见UtilDatePropertyEditor.java): public void setAsText(String text) throws IllegalArgumentException * 将属性编辑器注册到spring中,参考:applicationContext.xml
对于配置文件有多个,并且名称相似如:
applicationContext-beans.xml
applicationContext-others.xml
那么我们在实例化容器的时候可以这样:
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml"); User user = (User)factory.getBean("user");
评论
[*]
发表评论
-
spring 3 和mybatis 3集成,并用junit4进行测试
2011-11-04 14:01 1465转:spring 3 和mybatis 3集成,并用junit ... -
java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource的解决方法
2011-11-03 16:17 2748用Myeclipse开发struts时,配置struts-co ... -
Strut2+Spring整合框架搭建
2011-11-02 22:19 1078参考:http://lukuijun.iteye.com/bl ... -
Spring+hibernate延迟加载报错解决办法之二
2010-06-29 17:28 1213在做删除操作的时候出现了org.springframework ... -
Spring+hibernate延迟加载报错解决办法之一
2010-06-29 17:25 1213我们在项目中一般都会使用Spring来管理Hibernate的 ... -
Spring项目中怎么配置log4j
2010-05-27 11:10 1585在spring项目中配置log4j http://blogg ... -
Spring与Struts集成方式三
2010-05-26 17:11 1152在集成方式一和二中我们是在web.xml中加入配置代码: & ... -
Spring与Struts集成方式二
2010-05-26 14:49 1045在集成方式一的基础上做改进: 第一种集成方案是在action ... -
Spring与Struts集成方式一
2010-05-25 14:13 916我们在Struts中在action类中调用Model层组件进行 ... -
Hibernate编程式事务与Spring Aop的声明式事务(spring与hibernate集成)
2010-05-24 17:15 2769采用编程式事务 事务主要分为:编程式事务和声明式事务 ... -
修改Eclipse配置,使得在配置文件中完成自动完成功能。
2010-05-24 15:10 2299在Eclipse中引入spring的配置文件applicati ... -
Spring aop 基于schema的AOP支持及JoinPoint的使用、如何使用CGLIB代理
2010-05-24 14:52 4129基于schema的aop只是将配置写到配置文件中。 代 ... -
Spring AOP 概念理解及@AspectJ支持
2010-05-20 15:56 1584为了更好的理解Spring简介一文http://quicker ... -
Spirng AOP简介
2010-05-19 17:28 2284AOP 面向切面编程(AOP)通过提供另外一种思考程序结构的 ... -
Spring Bean中的自动装配——byType
2010-05-19 17:08 1182自动装配byType即通过查找类属性在配置文件中bean中定义 ... -
Spring Bean中的自动装配——byName
2010-05-19 16:34 3151自动装配(autowire)协作者 Spring IoC容器 ... -
Spring Bean的作用域:用Spring来解决Struts中Action的单实例问题
2010-05-19 10:47 1562我们知道在Struts中Action是从map中拿出来的,是单 ... -
Spring Bean定义的继承
2010-05-19 10:36 1327现有Bean2,Bean3,Bean4,Bean5 可观察到 ... -
spring 初探
2010-05-17 16:53 1255Spring核心设计思想为IOC ... -
简单DAO层示例
2010-05-14 17:30 2080在使用spring架构之前,我们怎么设计自己的DAO层的呢? ...
相关推荐
基本类型的属性注入是Spring中最常见的用法之一,它支持所有的Java基本数据类型如`int`、`long`、`double`、`boolean`等,也包括它们对应的包装类如`Integer`、`Long`、`Double`、`Boolean`等。例如: ```xml ...
对于`List`、`Set`和`Map`等集合类型的注入,Spring同样支持。例如,注入listValue、setValue和mapValue: ```xml <!-- ... --> <list> <value>Item1 <value>Item2 </list> <set> <value>Value1 ...
例如,将`Map`类型转换为`List`类型或使用自定义对象接收复杂的参数。 **示例**: ```java Map, Object> params = new HashMap(); // 转换为List List<String> names = (List) params.get("names"); // 或者使用...
- List、Set、Map等接口及其实现类ArrayList、LinkedList、HashSet、HashMap等。 - Iterator、ListIterator等迭代器用于遍历集合。 5. **输入输出流**: - 字节流:InputStream、OutputStream。 - 字符流:...
- **集合框架**:了解List、Set、Map等集合类的特性和使用方法。 #### 二、Java界面编程 (了解) - **AWT**:掌握AWT的基本组件和布局管理器。 - **事件机制**:了解如何处理用户的输入事件。 - **Swing**:学习更...
### JAVA开发面试知识点详解 #### 1. ReentrantLock知识点 **知识点概述:** ...- **List、Set、Map**:这些接口并不直接继承自`Collection`接口。`Map`接口是独立的,而`List`和`Set`是`Collection`的子接口。
在Java中,基本类型(如int、char等)的传递是按值传递的,而对象类型的传递则是按引用传递的。理解这一点对于正确处理变量和对象之间的交互至关重要。 **2. hashCode & equals** `hashCode()` 和 `equals()` 方法...
- **基本数据类型**:如int、double、char等。 - **引用数据类型**:主要包括类(Class)、接口(Interface)、数组(Array)。 #### 2. 类与对象 - **类**:是具有相同属性和行为的对象的抽象。 - **对象**:类的...
6、以上所有类型的集合,List,Set,Map(有些局限性) However nothing is added to the list of classes that can be created (i.e. put up for remoting) without you declaring it. 2、 The Converters DWR已经默认...
Java集合框架包括List、Set、Map等接口和实现,为处理对象数组提供了强大支持。此外,Java SE(标准版)和Java EE(企业版)还提供了丰富的API,如Swing用于桌面应用,Spring框架用于企业级开发。 对于初学者,理解...