Spring: Injecting values into the Java Persistence property set using the <prop> tag

Source: Internet
Author: User

Spring: Injecting values into the Java Persistence property set using the <prop> tag

Use spring-provided <prop> to inject values into the Java Persistence property set, that is, to inject values into the Java.util.Properties object.

<props> is a simplified <map> that corresponds to an object dependency that has a configuration type of java.util.Properties.

Because properties can only specify a String type of key and value, <props> 's configuration is much simpler, with only a fixed format.

1. Create the Testutil class and add a property of type properties, then add getter and setter methods. The code is as follows:

 Package com.importnew; Import java.util.Properties;  Public class testutil {    private  Properties prop;      Public Properties Getprop () {        return  prop;    }      Public void SetProp (Properties prop) {        this. prop = prop;    }    } 

2. Write the spring configuration file, configure the Testutil, and assign a value to the Properties property. The code is as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-conte        Xt.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd ">       <BeanID= "Testutil"class= "Com.importnew.TestUtil"  >         < Propertyname= "prop">             <Props>                 <propKey= "Driver">Com.mysql.jdbc.Driver</prop>                 <propKey= "url">Jdbc:mysql://localhost:3306/test</prop>                 <propKey= "username">Root</prop>                 <propKey= "Password">Root</prop>             </Props>         </ Property>     </Bean></Beans>

3. Write the test class testspring with the following code:

 Packagetest;Importjava.util.Properties;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.importnew.TestUtil; Public classtestspring { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Testutil Testutil= (testutil) context.getbean ("Testutil");
Properties prop=Testutil.getprop (); String Driver= Prop.getproperty ("Driver"); String URL= Prop.getproperty ("url"); String username= Prop.getproperty ("username"); String Password= Prop.getproperty ("Password"); System.out.println ("Driver =" + Driver + ". url = "+ URL +". Username = "+username+". Password = "+ password+". "); }}

———————————————————————————————————————————————————

Note the:<props> element. Each <props> can be nested multiple <prop>, <prop> inside no element can be used, can only be a string, which is determined by the semantics of Java.util.Properties.

End

Spring: Injecting values into the Java Persistence property set using the <prop> tag

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.