파일 구성
- resources : AppCTX.xml, admin.properties
- java : AdminConnection.java, MainClass.java
- 추가예정 : 주석, 분석
resources
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "adminConnection" class="com.javalec.ex.AdminConnection"/>
</beans>
admin.properties
admin.id=pej
admin.pw=12345
AdminConnection.java
package com.javalec.ex;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
public class AdminConnection implements EnvironmentAware, InitializingBean, DisposableBean {
// 의존성 주입은 의존성을 분리시켜 사용한다.
private Environment env;
private String adminId;
private String adminPw;
// 내부에 변수로 Environment 클래스를 사용함. AdminConnection클래스는 Environment클래스에 의존관계가 생기게 됨.
@Override
public void setEnvironment(Environment env) {
// DI 의존주입: 내부가 아니라 외부에서 객체를 생성해 넣어준다.
System.out.println("setsetEnvironment()");
setEnv(env);
}
public Environment getEnv() {
return env;
}
public void setEnv(Environment env) {
this.env = env;
}
public String getAdminId() {
return adminId;
}
public void setAdminId(String adminId) {
this.adminId = adminId;
}
public String getAdminPw() {
return adminPw;
}
public void setAdminPw(String adminPw) {
this.adminPw = adminPw;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("AfterProperties");
// 빈즈가 만드는 시점에 외부 데이터를 넣겠다는거임.
setAdminId(env.getProperty("admin.id"));
setAdminPw(env.getProperty("admin.pw"));
}
@Override
public void destroy() throws Exception {
System.out.println("destroy");
}
}
MainClass.java
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();
ConfigurableEnvironment env = ctx.getEnvironment();
MutablePropertySources propertySources = env.getPropertySources();
try {
propertySources.addLast(new ResourcePropertySource("classpath:admin.properties"));
System.out.println(env.getProperty("admin.id"));
System.out.println(env.getProperty("admin.pw"));
}catch(IOException e) {}
///////////////////////////////////
GenericXmlApplicationContext gCtx = (GenericXmlApplicationContext)ctx;
gCtx.load("AppCTX.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, Environment 사용) (0) | 2021.04.07 |
---|---|
스프링 실습 예제(static과 load로 외부 데이터 2개) (0) | 2021.04.07 |
Spring Bean scope 범위 종류 (0) | 2021.04.06 |
스프링 라이프사이클 실습(생명주기, Bean 객체 생성 - 소멸) (0) | 2021.04.06 |
라이프 사이클이란? (0) | 2021.04.06 |
댓글