Load files in several ways

  A, by loading the file path
  
  InputStream ips = new FileInputStream ( "config.properties ");
  
  two directly loaded through the getResourceAsStream
  
  // configuration files need to be placed under the directory of the current packet
  
  IPS = ReflectCollection.class the InputStream
  
  .getResourceAsStream ( "Resources / config .properties ");
  
  in this way may also be used an absolute path, an absolute path need slash (/). Whether absolute or relative path, the underlying calls are class loader.
  
  // absolute path way
  
  InputStream IPS = ReflectCollection.class
  
  .getResourceAsStream (
  
  "/cn/sunft/day01/reflect/resources/config.properties");
  
  Third, loaded by the class loader way
  
  to find the root of the classpath is located file, the file needs to be placed directly on the classpath specified directory, another directory foremost do not slash (/).
  
  // in the root directory of the classpath where to find the file
  
  // Note that this file needs to be placed directly on the classpath specified directory,
  
  // do not add another directory front / file normally need to change this way
  
  InputStream ips = ReflectCollection .class
  
  .getClassLoader ()
  
  .getResourceAsStream ( "CN / sunft / day01 / the reflect / the config.properties")
  
  four, Spring Properties loading configuration file
  
  a by context: property-placeholder tag implemented profile load
  
  <context: property-placeholder ignore- unresolvable = "to true" LOCATION = "CLASSPATH: redis-key.properties" />
  
  2, using the spring.xml profile properties:
  
  <! - basic properties URL, User, password ->
  
  <property name = "URL" = value "$ {jdbc.url}" />
  
  <Property name = "username" value = "$ {jdbc.username}" />
  
  <Property name = "password" value = "$ {jdbc.password}" />
  
  3, using java file:
  
  @Value ( "jdbc_url $ {}")
  
  Private String the jdbcUrl; // NOTE: this is defined as static variables can not be
  
  two, by util:loading profile achieved properties tab
  
  1, Example usage: add tags in the configuration file spring.xml
  
  <util: Properties ID = "util_Spring" = the override-local "to true" www.frgjyL.cn LOCATION = "CLASSPATH: jeesite.properties" />
  
  2, using the profile properties in spring.xml:
  
  <Property name = "username "value =" # {util_Spring [ 'jdbc.username']} "/>
  
  <Property name =" password "value =" # {util_Spring [ 'jdbc.password']} "/>
  
  . 3, used in the java file:
  
  @value (value = "# {util_Spring [ 'UTIL_SERVICE_ONE']}")
  
  Private String UTIL_SERVICE_ONE;
  
  Third, the loading profile achieved by @PropertySource annotations
  
  1, example usage: PropertySource annotation using java class file:
  
  @PropertySource (value = www.huishenggw.cn { "CLASSPATH: redis-key.properties"})
  
  public class the ReadProperties {
  
  @Value (value = "$ {JDBC.} username "www.chengmyuLegw.cn)
  
  Private String USER_NAME;
  
  }
  
  2, used in the java file:
  
  @Value (value = "$ {} jdbc.username")
  
  Private String USER_NAME;
  
  IV PropertyPlaceholderConfigurer read by the class profile
  
  1, Example usage: Use <bean> tag arranged in the spring.xml
  
  <bean class = "org .springframework.beans.factory.config.PropertyPlaceholderConfigurer ">
  
  <Property name =" locations ">
  
  <List>
  
  <value> CLASSPATH: redis-key.properties </ value>
  
  </ List>
  
  </ Property>
  
  </ the bean>
  
  2 , The PropertyPlaceholderConfigurer configuration, equivalent to the mode a, with reference to the use of a method.
  
  <! - basic properties URL, User, www.honglpt.cn password ->
  
  <Property name = "URL" www.cmyLgw.cn value = "$ jdbc.url {}" />
  
  <Property name = "username" value = "$ {jdbc.
  

Guess you like

Origin www.cnblogs.com/qwangxiao/p/10960278.html