스프링(5)

서울IT코드정리 /kyChoi·2021년 11월 10일
0

스프링

목록 보기
8/17

Context -> Environment -> PropertySources -> PropertySources
ctx.getEnvironment() env.getPropertySources()
프로퍼티 추가 : propertySources.addLast() 추출 env.getProperty()

package com.javalec.ex;

import java.io.IOException;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.support.ResourcePropertySource;

public class MainClass {
	
	public static void main(String[] args) {
		
		ConfigurableApplicationContext ctx = new GenericXmlApplicationContext();
        //context로 객체 생성
		ConfigurableEnvironment env=ctx.getEnvironment();
		MutablePropertySources propertySources = env.getPropertySources();
		//객체로 Environment 객체 생성, env로 프로퍼티객체생성
		try {
			propertySources.addLast(new ResourcePropertySource("classpath:admin.properties"));
            ResourcePropertySouce 가 문자열을 객체로 만들어버림			System.out.println(env.getProperty("admin.id"));
			System.out.println(env.getProperty("admin.pw"));
			//propertySources 에 admin.properties 를 읽어와 붙여넣음
		}catch(IOException e) {}
		GenericXmlApplicationContext gCtx = (GenericXmlApplicationContext)ctx;
		gCtx.load("applicationCTX.xml");
		gCtx.refresh(); //빈 객체가 생성되려면 필요합니다
		
		AdminConnection adminConnection = gCtx.getBean("adminConnection", AdminConnection.class);
		System.out.println("admin ID : " + adminConnection.getAdminId());
		System.out.println("admin PW : " + adminConnection.getAdminPw());
		
		gCtx.close();
		ctx.close();
	}

}
profile
건물주가 되는 그날까지

0개의 댓글