티스토리 뷰
main.java
package com.java.ex;
import java.io.IOException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;
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;
//위의 import는 항상 고정적으로 가져오도록 하자. 여러개를 사용할 수도 있기때문.
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("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();
AdminConnection.java
package com.java.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;
private String sub_adminId;
private String sub_adminPw;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet()");
setAdminId(env.getProperty("admin.id"));
setAdminPw(env.getProperty("admin.pw"));
}
public void setEnv(Environment env) {
this.env = env;
}
public void setAdminId(String adminId) {
this.adminId = adminId;
}
public void setAdminPw(String adminPw) {
this.adminPw = adminPw;
}
public Environment getEnv() {
return env;
}
public String getAdminId() {
return adminId;
}
public String getAdminPw() {
return adminPw;
}
public String getSub_adminId() {
return sub_adminId;
}
public void setSub_adminId(String sub_adminId) {
this.sub_adminId = sub_adminId;
}
public String getSub_adminPw() {
return sub_adminPw;
}
public void setSub_adminPw(String sub_adminPw) {
this.sub_adminPw = sub_adminPw;
}
@Override
public void setEnvironment(Environment environment) { // 연결 (빈즈가 만들어진 시점에 자동으로 불러짐)
System.out.println("setEnvironment()");
setEnv(environment);
}
@Override
public void destroy() throws Exception {
System.out.println("detroy()");
}
}
applicationCTX.xml
<?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.java.ex.AdminConnection" />
</beans>
admin.properties
admin.id=admin
admin.pw=12345
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- zipkin
- docker
- Logstash to ElasticSearch
- github
- producer
- elasticSearch
- 운동일기
- ACTUATOR
- prometheus
- Gateway
- springcloud
- rabbitmq
- Logstash 활용
- Feign
- config
- 운동
- MariaDB
- consumer
- 오늘저녁 삼겹살
- 루틴기록
- 미래의나에게동기부여
- kafka
- Spring + ELK
- UserService
- git
- MSA
- 빅-오
- Kafka Connect
- JWT
- LoadBalancer
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
글 보관함