12, Spring Bean Base (4) __spring Learning Tutorial

Source: Internet
Author: User
Tags dateformat string format import database
12. Spring Bean Base (4)

This article mainly introduces the following several knowledge points injection date into the Bean property (using Customdateeditor) propertyplaceholderconfigurer instance Bean configuration Inheritance

Let's get down to business.

inject date into bean properties

First step: Create a bean

Package com.main.autowrite.customDateEditor;

Import java.util.Date;

public class Showdate {
    private date date;

    Public Date getDate () {return
        date;
    }

    public void setdate (date date) {
        this.date = date;
    }

    @Override public
    String toString () {return
        "showdate [date=] + date +"] ";
    }

}

Step Two: Add the bean configuration file

There are two main ways to configure a bean configuration file
Mode one:

<?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-2.5.xsd ">

    <bean id= "DateFormat" class= "Java.text.SimpleDateFormat" >
        <constructor-arg value= "YYYY-MM-DD"/ >
    </bean>

    <bean id= "showdate" class= "Com.main.autowrite.customDateEditor.ShowDate" >
        <property name= "Date" >
            <bean factory-bean= "DateFormat" factory-method= "Parse" >
                    < Constructor-arg value= "2014-12-31"/>
            </bean>
        </property>
    </bean>
</ Beans>

Mode two:
First create a Date property converter Utildatepropertyeditor.java,

Package com.main.autowrite.customDateEditor;

Import Java.beans.PropertyEditorSupport;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.Date;

public class Utildatepropertyeditor extends PropertyEditorSupport {

    private String format= "Yyyy-mm-dd";  

    @Override public  
    void Setastext (String text) throws IllegalArgumentException {  

        SimpleDateFormat sdf = new SimpleDateFormat (format);  
        try{  
            Date d = sdf.parse (text);  
            This.setvalue (d);  
        } catch (ParseException e) {  

            e.printstacktrace ();  
        }  
    }  

    public void SetFormat (String format) {  
        This.format = format;  
    }  
}

Then call it in the bean configuration file
Note: The following configuration is based on the Spring 4.0 version, and if you use the previous version of Spring 4.0, you need to put the com.main.autowrite.customDateEditor.UtilDatePropertyEditors declare it as a bean and then reference the bean in the map

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-2.5.xsd "> <context:annotation-config/> <bean class=" Org.springframework.beans.factor
                Y.config.customeditorconfigurer "> <property name=" customeditors "> <map>
            <entry key= "Java.util.Date" value= "Com.main.autowrite.customDateEditor.UtilDatePropertyEditor"/> </map> </property> </bean> <bean id= "showdate" class= "com.main.autowrite.c Ustomdateeditor.showdate "> <proPerty name= "Date" value= "2015-12-23"/> </bean> </beans> 

Test:

    @Test public
    void Test () {
        ApplicationContext context = new Classpathxmlapplicationcontext (
                "com/main/ Autowrite/customdateeditor/bean.xml ");

        Showdate showdate = (showdate) context.getbean ("Showdate");
        System.out.println (showdate);
    }

Propertyplaceholderconfigurer Instance

In a project with a database connection, it is generally customary to put the details of a database connection separately in a file (database.properties, for example) to facilitate the operation of the database administrator.

Example:
database.properties

    Jdbc.driverclassname=com.mysql.jdbc.driver
    Jdbc.url=jdbc:mysql://localhost:3306/demo
    jdbc.username= Root
    jdbc.password=123456

Bean configuration file Referencing database.properties

<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 class=" org.springframework.beans.fact Ory.config.PropertyPlaceholderConfigurer "> <property name=" Location "> &LT;VALUE&GT;DATABASE.P roperties</value> </property> </bean> <bean id= "Hellodao" class= "..." > &L T;property name= "DataSource" ref= "DataSource"/> </bean> <bean id= "DataSource" class= "ORG.SPR" Ingframework.jdbc.datasource.DriverManagerDataSource "> <property name=" driverclassname "value=" ${jdbc.drive Rclassname} "/> <property name= url" value= "${jdbc.url}"/> <property name= "username" value= "$ {jdbc.username} "/> <property name=" password "value=" ${Jdbc.password} "/> </bean> </beans> 

Detailed description (three steps):
First, import database.properties files

<bean
        class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >

        < Property name= "Location" >
            <value>database.properties</value>
        </property>
    </ Bean>

Second, refer to the Database.properties file details, and declared as a bean named DataSource

<bean id= "DataSource"
        class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >

        < Property Name= "Driverclassname" value= "${jdbc.driverclassname}"/> <property name=
        "url" value= "${jdbc.url } "/>
        <property name=" username "value=" ${jdbc.username} "/> <property name=" password "value="
        ${ Jdbc.password} "/>
    </bean>

Iii. referencing DataSource Beans

<bean id= "Hellodao" class= "> <property name=" dataSource "ref=" DataSource "
        />
</bean>

Bean Configuration Inheritance

1. General Inheritance

An entity class Hello.java

public class hello{
    private int type;
    private String name;
    //....
}

Bean configuration file

<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=" Hello "class=" Com.main.Hello " >
        <property name= "type" value= "1"/>
    </bean>

    <bean id= "Hellochildren" parent= " Basecustomermalaysia ">
        <property name=" name "value=" Jack "/>
    </bean>

</beans>

Output Hellochildren Bean's property value

Hellochildren [Type=1,name=jack]

2. Abstract Inheritance
Note: The parent class bean is not allowed to be instantiated, and as you can see in the normal inheritance above, theHello Bean can still be instantiated.

Hello hello = (hello) context.getbean ("Hello");

When we configure this, the Hello bean will not be instantiated

<bean id= "Hello" class= "Com.main.Hello" abstract= "true" >
        <property name= "type" value= "1"/>
    < /bean>

3, pure template inheritance
Shares a property value that has already been set without defining or modifying the Bean's property value

4, overwrite the property value of the Father Bean

    <bean id= "Hello" class= "Com.main.Hello" >
        <property name= "type" value= "1"/>
    </bean>

    <bean id= "Hellochildren" parent= "Basecustomermalaysia" >
        <property name= "type" value= ""/>
        < Property name= "Name" value= "Jack"/>
    </bean>

The result of getting the hellochildren bean output is:

Hello [Type=2,name=jack]

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.