출처 : it-pig.tistory.com/24
- 파일 구조
- resource : appCTX1, appCTX2, appCTX3
- java : ServerInfo, RunConfig, DevConfig, MainClass.java
- Annotation과 xml 방식 포함
- Profile, Environment, 사용(어노테이션, xml)
appCTX1.xml, appCTX2.xml, appCTX3.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"
profile = "dev">
<!--
profile = "run" --> appCTX1
profile = "dev" --> appCTX2
profile = "pej" --> appCTX3
-->
<bean id="serverInfo" class = "com.javalec.ex.ServerInfo">
<property name="ipNum" value="Localhost"/>
<property name="portNum" value="8181"/>
</bean>
</beans>
ServerInfo.java
package com.javalec.ex;
// Annotation방식으로 의존주입할 데이터 클래스
public class ServerInfo {
private String ipNum;
private String portNum;
public String getIpNum() {
return ipNum;
}
public void setIpNum(String ipNum) {
this.ipNum = ipNum;
}
public String getPortNum() {
return portNum;
}
public void setPortNum(String portNum) {
this.portNum = portNum;
}
}
RunConfig.java
package com.javalec.ex;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("run")
public class RunConfig {
@Value("199.255.255.255")
private String ipNum;
@Value("80")
private String portNum;
@Bean
public ServerInfo serverInfo() {
ServerInfo serverConnection = new ServerInfo();
serverConnection.setIpNum(ipNum);
serverConnection.setPortNum(portNum);
return serverConnection;
}
}
DevConfig.java
package com.javalec.ex;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("dev")
public class DevConfig {
@Value("Localhost")
private String ipNum;
@Value("8181")
private String portNum;
@Bean
public ServerInfo serverInfo() {
ServerInfo serverConnection = new ServerInfo();
serverConnection.setIpNum(ipNum);
serverConnection.setPortNum(portNum);
return serverConnection;
}
}
MainClass.java
package com.javalec.ex;
import java.util.Scanner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] agrs) {
String config = null;
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
// 어떤걸 쓸지는 사용자가 맘대로 선택할 수 있음. 코드를 바꾸지 않는상태에서
if(str.equals("dev")) {
config = "dev";
}else if(str.equals("run")) {
config = "run";
}else if(str.equals("pej")) {
config = "pej";
}
// XML 로드 방식
// GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
//
// ctx.getEnvironment().setActiveProfiles(config);
// ctx.load("appCTX1.xml","appCTX2.xml","appCTX3.xml");
//
// ServerInfo info = ctx.getBean("serverInfo",ServerInfo.class);
// System.out.println("ip : " + info.getIpNum());
// System.out.println("port : " + info.getPortNum());
// Annotation, Environment, register
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles(config);
ctx.register(RunConfig.class, DevConfig.class);
ctx.refresh();
ServerInfo info = ctx.getBean("serverInfo", ServerInfo.class);
System.out.println("ip : " + info.getIpNum());
System.out.println("port : " + info.getPortNum());
scanner.close();
ctx.close();
}
}
반응형
'자바과정 > 스프링' 카테고리의 다른 글
스프링 AOP(자바 방식, Spring XML 방식) (3) | 2021.04.08 |
---|---|
스프링 실습예제(Xml 방식의 AOP) (0) | 2021.04.08 |
스프링 실습 예제(static과 load로 외부 데이터 2개) (0) | 2021.04.07 |
스프링 실습 예제 (외부 데이터 사용) (0) | 2021.04.07 |
Spring Bean scope 범위 종류 (0) | 2021.04.06 |
댓글