'properties'에 해당되는 글 2건

  1. 2022.03.16 [Java] Read Properties File.
  2. 2013.08.13 [Spring] properties 파일 읽기.

[Java] Read Properties File.

ITWeb/개발일반 2022. 3. 16. 15:19

일반 Java Application 프로젝트에서 resources 폴더 아래 application.properties 같은 정보를 가져와야 할 때가 있습니다.

아래 처럼 사용 하시면 됩니다.

 

application.properties)

host=localhost
port=8888

 

MainApplication.java)

public void readProperties() {
    Properties prop = new Properties();
    
    try ( InputStream stream = MainApplication.class.getClassLoader().getResourceAsStream("application"
    +".properties")) {
    	prop.load(Stream);
    } catch (Exception e) {
    }
    
    try {
    	Resource resource = new ClassPathResource("application.properties");
        prop.load(resource.getInputStream());
    } catch (Exception e) {
    }
    
    System.out.println( prop.getProperty("host") );
    System.out.println( prop.getProperty("port") );
}

 

:

[Spring] properties 파일 읽기.

ITWeb/개발일반 2013. 8. 13. 18:35

해당 프로젝트에 conf 디렉토리 하나 만드시고 거기에 profile 별 디렉토리도 만드시고.. 

아래 같이 해주면 됩니다. (뭐 나만 알아 볼라고 대충 등록함.)



[pom.xml]

<resource>

<directory>${basedir}/conf/${env}</directory>

<targetPath>${project.build.directory}/conf</targetPath>

<filtering>false</filtering>

</resource>


[class]

resource = new ClassPathResource(propsFile);

props = PropertiesLoaderUtils.loadProperties(resource);


clusterName = props.getProperty("cluster.name");


[properties]

cluster.name=es-cluster

cluster.node.list=192.168.56.104:9300,192.168.56.104:9301


: