Spring Introduction Study Notes (2.15) -- register the Attribute Editor in spring

Source: Internet
Author: User
Tags dateformat

I. knowledge points

Attribute Editor is a function of the JavaBeans API. It is used for conversion between attribute values and text values. Each Attribute Editor is only used for a certain type of attributes.

Spring IoC container supports Using Attribute Editor to simplify bean Configuration. For example, you can use the java.net. url Attribute Editor to specify the URL string used for URL type attributes. Spring will automatically convert the URL string into a URL object and inject it into your attributes. Spring comes with a variety of attribute editors used to convert common bean attributes.

You must register the property editor in the Spring IoC container before using it.Customeditorconfigurer is implemented as a bean factory post-processor and is used to register your custom attribute editor before any bean instantiation.

Ii. Sample Code

Package COM. codeproject. jackie. springrecipesnote. springadvancedioc; import Java. util. date;/*** product sales ranking * @ author Jackie **/public class productranking {private product bestseller; private date fromdate; private date todate; /*** @ return the bestseller */Public Product getbestseller () {return bestseller ;} /*** @ Param bestseller * the bestseller to set */Public void setbestseller (product bestseller) {This. bestseller = bestseller;}/*** @ return the fromdate */public date getfromdate () {return fromdate ;} /*** @ Param fromdate * The fromdate to set */Public void setfromdate (date fromdate) {This. fromdate = fromdate;}/*** @ return the todate */public date gettodate () {return todate ;} /*** @ Param todate * The todate to set */Public void settodate (date todate) {This. todate = todate ;}}


The following is a date string that the Java program converts to a specific mode.

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");productRanking.setFromDate(dateFormat.parse("2007-09-01"));productRanking.setToDate(dateFormat.parse("2007-09-30"));

Spring medium-price bean configuration is as follows:

<?xml version="1.0" encoding="UTF-8"?><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-3.2.xsd">        <bean id="dateFormat" class="java.text.SimpleDateFormat">    <constructor-arg value="yyyy-MM-dd" />    </bean>        <bean id="productRanking" class="com.codeproject.jackie.springrecipesnote.springadvancedioc.ProductRanking"><property name="bestSeller">    <bean class="com.codeproject.jackie.springrecipesnote.springadvancedioc.Disc"><property name="name" value="CD-RW" /><property name="price" value="1.5" />    </bean></property><property name="fromDate">    <bean factory-bean="dateFormat" factory-method="parse"><constructor-arg value="2007-09-01"></constructor-arg>    </bean></property><property name="toDate">    <bean factory-bean="dateFormat" factory-method="parse"><constructor-arg value="2007-09-30"></constructor-arg>    </bean></property>     </bean></beans>

The preceding configuration is too complicated for setting date attributes. The Spring IoC container can use the property editor to convert text values for your properties.Spring's customdateeditor class is used to convert a date string to the java. util. Date attribute.Declare the instance in the bean configuration file.

<Bean id = "dateeditor" class = "org. springframework. beans. propertyeditors. customdateeditor "> <constructor-Arg> <Bean class =" Java. text. simpledateformat "> <constructor-Arg value =" yyyy-mm-dd "> </constructor-Arg> </bean> </constructor-Arg> <! -- Indicates whether the editor allows null values --> <constructor-Arg value = "true"/> </bean>

Then register this Attribute Editor in the customeditorconfigurer instance, so that spring can convert the type to the java. util. Date attribute.

<bean id="dateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor">    <constructor-arg>    <bean class="java.text.SimpleDateFormat">        <constructor-arg value="yyyy-MM-dd"></constructor-arg>    </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="productRanking" class="com.codeproject.jackie.springrecipesnote.springadvancedioc.ProductRanking">    <property name="bestSeller">       <bean class="com.codeproject.jackie.springrecipesnote.springadvancedioc.Disc">  <property name="name" value="CD-RW" />  <property name="price" value="1.5" />       </bean>    </property>    <property name="fromDate" value="2007-09-01" />    <property name="toDate" value="2007-09-30" /></bean>

Test class propertyeditortest

package com.codeproject.jackie.springrecipesnote.springadvancedioc;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author jackie * */public class PropertyEditorTest {@Testpublic void testPropertyEditor(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");ProductRanking productRanking = (ProductRanking) context.getBean("productRanking");System.out.println("Product ranking from " + productRanking.getFromDate() + " to " + productRanking.getToDate());}}

In addition to customdateeditor, spring comes with multiple attribute editors that convert common data types, such as customnumbereditor, classeditor, fileeditor, localeeditor, stringarraypropertyeditor, and urleditor. classeditor, fileeditor, localeeditor, and urleditor are pre-registered by spring and do not need to be registered again. For more information about the use of these editors, see javadoc for these classes in the org. springframework. Beans. propertyeditors package.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.