2022.10.06 Spring

sofia·2022년 10월 12일
0

Spring

목록 보기
4/11
post-thumbnail

저번 시간에 이어서

Char 8

연습문제 1

oracle.account

emp.id=scott
emp.pw=tiger

EmpConnection.java

package com.javalec.spring_8_1;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

public class EmpConnection implements EnvironmentAware, InitializingBean {
	private Environment Env;
	private String empId;
	private String empPw;

	@Override
	public void setEnvironment(Environment environment) {
		setEnv(environment);//객체 생성 전 
		
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		setEmpId(Env.getProperty("emp.id"));
		setEmpPw(Env.getProperty("emp.pw"));
		
	}

	public Environment getEnv() {
		return Env;
	}

	public void setEnv(Environment env) {
		Env = env;
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

	public String getEmpPw() {
		return empPw;
	}

	public void setEmpPw(String empPw) {
		this.empPw = empPw;
	}
	
	
}

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-3.2.xsd">
	
	<bean id="empConnection" class="com.javalec.spring_8_1.EmpConnection"></bean>

</beans>

MainClass.java

package com.javalec.spring_8_1;

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:oracle.account"));
			//외부파일 가져올때 객체를 사용
            //예외처리 try catch (외부파일이 없을경우도 있기때문)
			System.out.println(env.getProperty("emp.id"));
			System.out.println(env.getProperty("emp.pw"));
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("파일을 불러올 수 없습니다.");
		}
			GenericXmlApplicationContext gctx = (GenericXmlApplicationContext) ctx;
			gctx.load("ex_applicationCTX.xml");
			gctx.refresh();
			
			EmpConnection connection = gctx.getBean("empConnection", EmpConnection.class);
			
			System.out.println("emp ID : "+connection.getEmpId());
			System.out.println("emp PW : "+connection.getEmpPw());
	}
}

연습문제 2

mssql.account

ms_student.id=stu101
ms_student.pw=1234

mysql.account

my_student.id=stu201
my_student.pw=5678

DbConnection.java

package com.javalec.spring_ex_8_2;

public class DbConnection {
	private String mssqlId;
	private String mssqlPw;
	private String mysqlId;
	private String mysqlPw;
	public String getMssqlId() {
		return mssqlId;
	}
	public void setMssqlId(String mssqlId) {
		this.mssqlId = mssqlId;
	}
	public String getMssqlPw() {
		return mssqlPw;
	}
	public void setMssqlPw(String mssqlPw) {
		this.mssqlPw = mssqlPw;
	}
	public String getMysqlId() {
		return mysqlId;
	}
	public void setMysqlId(String mysqlId) {
		this.mysqlId = mysqlId;
	}
	public String getMysqlPw() {
		return mysqlPw;
	}
	public void setMysqlPw(String mysqlPw) {
		this.mysqlPw = mysqlPw;
	}
}

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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<context:property-placeholder location="classpath:mssql.account, classpath:mysql.account"/>

	<bean id="dbConnection" class="com.javalec.spring_ex_8_2.DbConnection">
		<property name="mssqlId">
			<value>${ms_student.id}</value>
		</property>
		<property name="mssqlPw">
			<value>${ms_student.pw}</value>
		</property>
		<property name="mysqlId">
			<value>${my_student.id}</value>
		</property>
		<property name="mysqlPw">
			<value>${my_student.pw}</value>
		</property>
	</bean>
</beans>

MainClass.java

package com.javalec.spring_ex_8_2;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class MainClass {
	public static void main(String[] args) {
		AbstractApplicationContext ctx = new GenericXmlApplicationContext("applicationCTX.xml");
		
		DbConnection dbConnection = ctx.getBean("dbConnection",DbConnection.class);
		System.out.println("mssqlID : "+dbConnection.getMssqlId());
		System.out.println("mssqlPW : "+dbConnection.getMssqlPw());
		System.out.println("mysqlID : "+dbConnection.getMysqlId());
		System.out.println("mysqlPW : "+dbConnection.getMysqlPw());
	}
}

연습문제 3

DbInfo.java

package com.javalec.spring_ex_8_3;

public class DbInfo {
	private String driver;
	private String url;
	private String user;
	private String password;
	public String getDriver() {
		return driver;
	}
	public void setDriver(String driver) {
		this.driver = driver;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

oracleCTX.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="dbInfo" class="com.javalec.spring_ex_8_3.DbInfo">
		<property name="driver">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
			<value>jdbc:oracle:thin:@localhost:1521:ORCL</value>	
		</property>
		<property name="user">
			<value>scott</value>
		</property>
		<property name="password">
			<value>tiger</value>
		</property>
	</bean>
</beans>

mysqlCTX.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="dbInfo" class="com.javalec.spring_ex_8_3.DbInfo">
		<property name="driver">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/JSPDB</value>
		</property>
		<property name="user">
			<value>root</value>
		</property>
		<property name="password">
			<value>mysql</value>
		</property>
	</bean>
</beans>

MainClass.java

package com.javalec.spring_ex_8_3;

import java.util.Scanner;

import org.springframework.context.support.GenericXmlApplicationContext;

public class MainClass {
	public static void main(String[] args) {
		String config="";
		Scanner scanner = new Scanner(System.in);
		String str = scanner.next();
		
		if (str.equals("oracle")) {
			config = "oracle";
		}else if (str.equals("mysql")) {
			config = "mysql";
		}
		
		scanner.close();
		
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.getEnvironment().setActiveProfiles(config);
		ctx.load("oracleCTX.xml","mysqlCTX.xml");
		
		DbInfo dbInfo = ctx.getBean("dbInfo",DbInfo.class);
		System.out.println("driver : "+dbInfo.getDriver());
		System.out.println("url : "+dbInfo.getUrl());
		System.out.println("user : "+dbInfo.getUser());
		System.out.println("password : "+dbInfo.getPassword());
	}
}

연습문제 4

jdbc.driver

oracle=oracle.jdbc.driver.OracleDRiver
mysql=com.mysql.jdbc.Driver

JdbcDriver.java

package com.javalec.spring_ex_8_4;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

public class JdbcDriver implements EnvironmentAware, InitializingBean {
	private Environment env;
	private String oracleJdbcDriver;
	private String mysqlJdbcDriver;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		setOracleJdbcDriver(env.getProperty("oracle"));
		setMysqlJdbcDriver(env.getProperty("mysql"));
	}
	
	@Override
	public void setEnvironment(Environment environment) {
		setEnv(environment);
		
	}
	
	
	public Environment getEnv() {
		return env;
	}

	public void setEnv(Environment env) {
		this.env = env;
	}

	public String getOracleJdbcDriver() {
		return oracleJdbcDriver;
	}

	public void setOracleJdbcDriver(String oracleJdbcDriver) {
		this.oracleJdbcDriver = oracleJdbcDriver;
	}

	public String getMysqlJdbcDriver() {
		return mysqlJdbcDriver;
	}

	public void setMysqlJdbcDriver(String mysqlJdbcDriver) {
		this.mysqlJdbcDriver = mysqlJdbcDriver;
	}

	
}

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="jdbcDriver" class="com.javalec.spring_ex_8_4.JdbcDriver"></bean>
</beans>

MainClass.java

package com.javalec.spring_ex_8_4;

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("applicationCTX.xml");
		ConfigurableEnvironment env = ctx.getEnvironment();
		MutablePropertySources propertySources = env.getPropertySources();
		
		try {
			propertySources.addLast(new ResourcePropertySource("classpath:jdbc.driver"));
			System.out.println(env.getProperty("oracle"));
			System.out.println(env.getProperty("mysql"));
		} catch (IOException e) {//외부파일이 없을 수도 있기 때문에 예외처리 
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		GenericXmlApplicationContext gCtx = (GenericXmlApplicationContext) ctx;
		//캐스팅
		gCtx.load("applicationCTX.xml");

		
		
		JdbcDriver jdbcDriver = gCtx.getBean("jdbcDriver",JdbcDriver.class);
		System.out.println("OracleJdbcDriver : "+jdbcDriver.getOracleJdbcDriver());
		System.out.println("MysqlJdbcDriver : "+jdbcDriver.getMysqlJdbcDriver());
		
	}
}

당시 오전에 발표하고, 평가시험 진행 등의 이유로 진도를 많이 나가지 않음

0개의 댓글