Custom Editor Example : IoC Factory Beans « Spring « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Spring » IoC Factory Beans 




Custom Editor Example

/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/


///////////////////////////////////////////////////////////////////////////////////////
//File:custom.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean name="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.util.regex.Pattern">
                    <bean class="PatternPropertyEditor"/>
                </entry>
            </map>
        </property>
    </bean>
    <bean id="exampleBean" class="CustomEditorExample">
        <property name="searchPattern">
            <value>(dog|fox)</value>
        </property>
        <property name="textToSearch">
            <value>The quick brown fox jumped over the lazy dog.</value>
        </property>
    </bean>
</beans>


///////////////////////////////////////////////////////////////////////////////////////

import java.beans.PropertyEditorSupport;
import java.util.regex.Pattern;

public class PatternPropertyEditor extends PropertyEditorSupport {

    public void setAsText(String textthrows IllegalArgumentException {
        Pattern pattern = Pattern.compile(text);
        setValue(pattern);
    }
}

///////////////////////////////////////////////////////////////////////////////////////
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class CustomEditorExample {

    private Pattern searchPattern;

    private String textToSearch;

    public static void main(String[] args) {
        ConfigurableListableBeanFactory factory = new XmlBeanFactory(
                new FileSystemResource("build/custom.xml"));

        CustomEditorConfigurer config = (CustomEditorConfigurerfactory
                .getBean("customEditorConfigurer");

        config.postProcessBeanFactory(factory);

        CustomEditorExample bean = (CustomEditorExamplefactory
                .getBean("exampleBean");

        System.out.println(bean.getMatchCount());
    }

    public void setSearchPattern(Pattern searchPattern) {
        this.searchPattern = searchPattern;
    }

    public void setTextToSearch(String textToSearch) {
        this.textToSearch = textToSearch;
    }

    public int getMatchCount() {
        Matcher m = searchPattern.matcher(textToSearch);

        int count = 0;
        while (m.find()) {
            count++;
        }

        return count;
    }
}

           
       














CustomEditorExample.zip( 1,477 k)
Related examples in the same category
1.Hierarchical Bean Factory Usage
2.Message Digest Example
3.Method Replacement Example
4.Property Editor Bean
5.Logging Bean Example
6.Accessing Factory Beans
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.