【Java】Spring中为bean注入Date对象

160 阅读6分钟
原文链接: click.aliyun.com
  1. 云栖社区>
  2. 博客列表>
  3. 正文

Spring中为bean注入Date对象

泳泳啊泳泳 2018-01-08 12:27:39 浏览40 评论0

java spring string class schema bean type

摘要: 比如我们有下面的一个bean: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import java.util.Date;    public class Customer {        Date date;  ...

比如我们有下面的一个bean:







import java.util.Date;



  


public class Customer {



  



    Date date;




  



    public Date getDate() {




        return date;




    }




  



    public void setDate(Date date) {




        this.date = date;




    }




  



    @Override




    public String toString() {




        return "Customer [date=" + date + " ] ";




    }




  


}





  注意我们上面的bean中有一个Date,但是如果我们使用下面的配置:








<beans xmlns="http://www.springframework.org/schema/beans "




    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "




    xsi:schemaLocation="http://www.springframework.org/schema/beans 

                                                    

                                                        
                                                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
                                                    

                                                    

                                                      

                                                    

                                                        
                                                        <bean 
                                                            id="customer" class="com.mkyong.common.Customer">
                                                    

                                                    

                                                            
                                                        <property 
                                                            name="date" value="2010-01-31" />
                                                    

                                                    

                                                        
                                                        </bean
                                                            >
                                                    

                                                    

                                                      

                                                    

                                                    </
                                                        beans>                                                            

                                                

                                                

                                        

  然后我们尝试着运行的话


                                            

                                                                

                                                                    

                                                                    public 
                                                                        class App {                                                                            

                                                                    

                                                                        
                                                                        public static 
                                                                            void main(String[] args) {                                                                                

                                                                    

                                                                            
                                                                        ApplicationContext context = new 
                                                                            ClassPathXmlApplicationContext(
                                                                    

                                                                    

                                                                                    
                                                                        "SpringBeans.xml");                                                                            

                                                                    

                                                                      

                                                                    

                                                                            
                                                                        Customer cust = (Customer) context.getBean("customer"
                                                                            );
                                                                    

                                                                    

                                                                            
                                                                        System.out.println(cust);
                                                                    

                                                                    

                                                                      

                                                                    

                                                                        
                                                                        }
                                                                    

                                                                    
}

                                                                

                                                            

                                        

会出现如下的错误:

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 foun

在这里提供两种解决办法:

1. Factory bean

声明一个dateFormat的bean,然后引用。如下解决:


                                            

                                                                

                                                                    

                                                                    <
                                                                        beans xmlns="http://www.springframework.org/schema/beans"                                                                            

                                                                    

                                                                        
                                                                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                                                    

                                                                    

                                                                        
                                                                        xsi:schemaLocation="http://www.springframework.org/schema/beans
                                                                    

                                                                    

                                                                        
                                                                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
                                                                    

                                                                    

                                                                      

                                                                    

                                                                        
                                                                        <bean 
                                                                            id="dateFormat" class="java.text.SimpleDateFormat">
                                                                    

                                                                    

                                                                            
                                                                        <constructor-arg 
                                                                            value="yyyy-MM-dd" />
                                                                    

                                                                    

                                                                        
                                                                        </bean
                                                                            >
                                                                    

                                                                    

                                                                      

                                                                    

                                                                        
                                                                        <bean 
                                                                            id="customer" class="com.mkyong.common.Customer">
                                                                    

                                                                    

                                                                            
                                                                        <property 
                                                                            name="date">
                                                                    

                                                                    

                                                                                
                                                                        <bean 
                                                                            factory-bean="dateFormat" factory-method="parse">
                                                                    

                                                                    

                                                                                    
                                                                        <constructor-arg 
                                                                            value="2010-01-31" />
                                                                    

                                                                    

                                                                                
                                                                        </bean
                                                                            >
                                                                    

                                                                    

                                                                            
                                                                        </property
                                                                            >
                                                                    

                                                                    

                                                                        
                                                                        </bean
                                                                            >
                                                                    

                                                                    

                                                                      

                                                                    

                                                                    </
                                                                        beans>                                                                            

                                                                

                                                            

                                        

  

2. CustomDateEditor

我们声明一个CustomDateEditor,将String转换为Date对象。


                                            

                                                                

                                                                    

                                                                    <
                                                                        bean id="dateEditor"                                                                            

                                                                    

                                                                           
                                                                        class="org.springframework.beans.propertyeditors.CustomDateEditor">
                                                                    

                                                                    

                                                                      

                                                                    

                                                                            
                                                                        <constructor-arg
                                                                            >
                                                                    

                                                                    

                                                                                
                                                                        <bean 
                                                                            class="java.text.SimpleDateFormat">
                                                                    

                                                                    

                                                                                    
                                                                        <constructor-arg 
                                                                            value="yyyy-MM-dd" />
                                                                    

                                                                    

                                                                                
                                                                        </bean
                                                                            >
                                                                    

                                                                    

                                                                            
                                                                        </constructor-arg
                                                                            >
                                                                    

                                                                    

                                                                            
                                                                        <constructor-arg 
                                                                            value="true" />
                                                                    

                                                                    

                                                                        
                                                                        </bean
                                                                            >
                                                                    

                                                                

                                                            

                                        

  


                                            

                                                                

                                                                    

                                                                    <
                                                                        bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">                                                                            

                                                                    

                                                                           
                                                                        <property 
                                                                            name="customEditors">
                                                                    

                                                                    

                                                                               
                                                                        <map
                                                                            >
                                                                    

                                                                    

                                                                                   
                                                                        <entry 
                                                                            key="java.util.Date">
                                                                    

                                                                    

                                                                                       
                                                                        <ref 
                                                                            local="dateEditor" />
                                                                    

                                                                    

                                                                                   
                                                                        </entry
                                                                            >
                                                                    

                                                                    

                                                                               
                                                                        </map
                                                                            >
                                                                    

                                                                    

                                                                           
                                                                        </property
                                                                            >
                                                                    

                                                                    

                                                                       
                                                                        </bean
                                                                            >
                                                                    

                                                                

                                                            

                                        

  完整的配置为:


                                            

                                                                

                                                                    

                                                                    <
                                                                        beans xmlns="http://www.springframework.org/schema/beans"                                                                            

                                                                    

                                                                        
                                                                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                                                    

                                                                    

                                                                        
                                                                        xsi:schemaLocation="http://www.springframework.org/schema/beans
                                                                    

                                                                    

                                                                        
                                                                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
                                                                    

                                                                    

                                                                      

                                                                    

                                                                        
                                                                        <bean 
                                                                            id="dateEditor"
                                                                    

                                                                    

                                                                            
                                                                        class="org.springframework.beans.propertyeditors.CustomDateEditor">
                                                                    

                                                                    

                                                                      

                                                                    

                                                                            
                                                                        <constructor-arg
                                                                            >
                                                                    

                                                                    

                                                                                
                                                                        <bean 
                                                                            class="java.text.SimpleDateFormat">
                                                                    

                                                                    

                                                                                    
                                                                        <constructor-arg 
                                                                            value="yyyy-MM-dd" />
                                                                    

                                                                    

                                                                                
                                                                        </bean
                                                                            >
                                                                    

                                                                    

                                                                            
                                                                        </constructor-arg
                                                                            >
                                                                    

                                                                    

                                                                            
                                                                        <constructor-arg 
                                                                            value="true" />
                                                                    

                                                                    

                                                                      

                                                                    

                                                                        
                                                                        </bean
                                                                            >
                                                                    

                                                                    

                                                                      

                                                                    

                                                                        
                                                                        <bean 
                                                                            class="org.springframework.beans.factory.config.CustomEditorConfigurer">
                                                                    

                                                                    

                                                                            
                                                                        <property 
                                                                            name="customEditors">
                                                                    

                                                                    

                                                                                
                                                                        <map
                                                                            >
                                                                    

                                                                    

                                                                                    
                                                                        <entry 
                                                                            key="java.util.Date">
                                                                    

                                                                    

                                                                                        
                                                                        <ref 
                                                                            local="dateEditor" />
                                                                    

                                                                    

                                                                                    
                                                                        </entry
                                                                            >
                                                                    

                                                                    

                                                                                
                                                                        </map
                                                                            >
                                                                    

                                                                    

                                                                            
                                                                        </property
                                                                            >
                                                                    

                                                                    

                                                                        
                                                                        </bean
                                                                            >
                                                                    

                                                                    

                                                                      

                                                                    

                                                                        
                                                                        <bean 
                                                                            id="customer" class="com.mkyong.common.Customer">
                                                                    

                                                                    

                                                                            
                                                                        <property 
                                                                            name="date" value="2010-02-31" />
                                                                    

                                                                    

                                                                        
                                                                        </bean
                                                                            >
                                                                    

                                                                    

                                                                      

                                                                    

                                                                    </
                                                                        beans>                                                                            

                                                                

                                                            

                                        

  

 


============================================================================== 本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835191.html,如需转载请自行联系原作者
版权声明:本文内容由互联网用户自发贡献,版权归作者所有,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。

用云栖社区APP,舒服~

【云栖快讯】中办国办印发《推进互联网协议第六版(IPv6)规模部署行动计划》加快推进基于 IPv6 的下一代互联网规模部署,计划指出2025年末中国 IPv6 规模要达到世界第一,阿里云也第一时间宣布了将全面提供IPv6服务,那么在全面部署 IPV6 前,你需要了解都在这儿  详情请点击 评论文章 (0) (0) (0)
分享到:

相关文章

网友评论