Spring read JDBC (file mode)

Source: Internet
Author: User

loading an external configuration using the Propertyplaceholderconfigurer class in a spring project, you might need to read the configuration from the properties file into the bean, such as the database connection information, memcached The server's address port information, and so on, is best independent of the jar package or war package, which makes it easier to modify the configuration. Spring provides the Propertyplaceholderconfigurer class to handle this matter. For example, to load MySQL configuration information in a configuration file: Jdbc.driver=Com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://Localhost:3306/mypageJdbc.username=Rootjdbc.password=123456we need to define the location property of the Propertyplaceholderconfigurer in the spring configuration file, which points to the position of the configuration file:<bean id= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" ><property name= " Systempropertiesmodename "value=" system_properties_mode_override "/><property name=" Location "value=" file:${ App_home}/conf/mypage.properties "/></bean><bean id=" DataSource "class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" ><property name= "Driverclassname" value= "${ Jdbc.driver} "/><property name=" url "value=" ${jdbc.url} "/><property name=" username "value=" ${ Jdbc.username} "/><property name=" password "value=" ${jdbc.password} "/></bean>in the example above, the Systempropertiesmodename property of the Propertyplaceholderconfigurer class is System_properties_mode_ Override this means that configuration information can be obtained from the parameters of the JVM virtual machine. The ${app_home} in the value of the configuration location can set different values depending on the environment, such as in the development environment, setting up the directory for the development environment, and the online environment configures the path of the line configuration file. For jar package files, you need to specify the value of App_home when you start the jar package, for example: Java-classpath test.jar-dapp_home=/data0/conf cn.outofmemory.App and for a war package running in a Tomcat container, you need to modify the bin in the next Tomcat directory/catalina.sh file, add or modify the values of the following variables in this file: Java_opts= "-xms2560m-xmx5120m-dapp_home=/application/outofmemory.cn/conf/"Note-xms2560m-xmx5120m is to modify the memory value of the virtual machine-Dapp_home affects the configuration file location. After assigning the necessary properties to propertyplaceholderconfigurer, the values in the configuration file can be implanted into the bean in the form of ${variable}. If you want to place the configuration file inside a jar package or war package, you need to remove file: In the location, for example, in a war package that is usually placed in the/web-inf/conf path, then you can write this:<bean id= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" ><property name= " Systempropertiesmodename "value=" system_properties_mode_override "/><property name=" Location "value="/ Web-inf/conf/mypage.properties "/></bean>

Spring read JDBC (file mode)

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.