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