이 기사는 Spring Reading 속성 구성 파일의 두 가지 방법을 소개합니다. 특정 콘텐츠를 살펴 보겠습니다.
1. Spring Factory를 통해 읽으십시오
예:
public class PropertyConfig {private static AbstractBeanFactory beanFactory = null;private static final Map<String,String> cache = new oncurrentHashMap<>();@Inject public PropertyConfig(AbstractBeanFactory beanFactory) {this.beanFactory = beanFactory;}/** * Get the Value of the configuration file based on the key * @param key * @return */public static String getProperty (string key) {String propvalue = "" "; if (cache.containskey (key)) {propvalue = cache.get (key);} else {try {propvalue = beanfactory.resolvemeddedDvalue ("$ { " + key.trim () +"} "; cache.put (불법 행위 exception ex) {ex.printstacktrace ();}} propvalue;}} 반환스프링 XML 구성
<ean> <property name = "SystemProperTiesModeName"value = "System_Properties_Mode_Override"/> <속성 이름 = "resulerEsourcenotFound"value = "true"/> <property name = "list> <list> value> classPath : propss/$ {property-path} .properties </value> </value> </property> </bean>프로젝트에서 사용하십시오
String maxtimeinsecondsprop = propertyconfig.getProperty ( "maxtimeInseconds");
2. Spirng 프로그램 코드를 직접 사용하여 프로젝트의 구성 파일 메소드를 읽으십시오.
import org.springframework.core.io.classpathresource; import org.springframework.core.io.resource; import org.springframework.core.io.support.propertiesloaderutils; import org.springframework.core.io.filesystemresource; 공개 클래스 테스트 {/** * @param args */public static void main (String [] args) {String configfile = "d : /test/application.properties"; // 구성 파일이 classPath 디렉토리에 있으면 classPathResource 객체를 사용할 수 있습니다. // resource resource = new ClassPathResource ( "/application.properties"); Resource Resource = 새로운 FileSystemResource (configfile); try {properties property = propertiesloaderutils.loadProperties (resource); 문자열 드라이버 = property.getProperty ( "jdbc.driver"); 문자열 url = property.getProperty ( "jdbc.url"); 문자열 username = property.getProperty ( "jdbc.username"); 문자열 암호 = property.getProperty ( "jdbc.password"); } catch (ioexception e1) {//log.error("Read Config File Evenge ", e1); }}}구성 파일이 ClassPath 디렉토리에 있으면 classPathResource 객체를 사용할 수 있습니다.
Resource Resource = New ClassPathResource ( "/Application.Properties");
요약
위의 것은 Spring Factory를 사용하여 속성 구성 파일의 샘플 코드를 읽는 것입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!