본문 바로가기
웹 프로그래밍(풀스택-->java)/웹프로그래밍(백엔드-->java)

50. 스프링 프레임 워크 사용하기

by 백엔드개발자0107 2021. 12. 17.

이번에는 기존에 만들어놨던 자바프로그램을 스프링프레임워크로 바꾸면

 

어떻게 되는지 알아볼것이다.  자세한 내용은 수업을 진행하면서 하나하나 알아보자

 

스프링 프레임워크같은 경우에는 처음에 프로젝트를 셋팅하는데 어려움이 있다.

 

하지만 제대로 한번 스프링 프레임워크가 셋팅된 이후에는 매우 많이 편해진다.

 

 

--> 스프링 프레임워크는 어떠한 개발도구가 제공되는게 아닌 그냥 자바 어플리케이션을 개발할떄 스프링 그룹에서

 

만든 자바 라이브러리를 붙여놓고 그 라이브러리에서 제공되고 있는 클래스를 이용해서 작업을 하는 그런방식으로 이용한다.

 


필요한 스프링프레임워크 라이브러리 다운!

 

log-back

 

<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
    <scope>test</scope>
</dependency>

 

 

mvn-repository

 

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>${org.slf4j-version}</version>

</dependency>

 

spring-context

 

<!-- 프로젝트에서 사용할 라이브러리 정 -->

<dependencies>

<!-- spring context -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${org.springframework-version}</version>

</dependency>

 


스프링 프레임워크 초기 세팅

 

 

 

pom.xml

 

더보기
더보기

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>kr.co.softcampus</groupId>

<artifactId>SpringBasic</artifactId>

<version>0.0.1-SNAPSHOT</version>

 

<!-- xml에서 사용할 속성들 -->

<properties>

<!-- 자바버전 -->

<java-version>1.8</java-version>

<!-- 스프링버전 -->

<org.springframework-version>5.1.9.RELEASE</org.springframework-version>

<!-- <org.springframework-version>4.3.25.RELEASE</org.springframework-version> -->

<org.slf4j-version>1.7.26</org.slf4j-version>

<ch.qos.logback-version>1.2.3</ch.qos.logback-version>

</properties>

 

<!-- 프로젝트에서 사용할 라이브러리 정 -->

<dependencies>

<!-- spring context -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${org.springframework-version}</version>

</dependency>

 

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>${org.slf4j-version}</version>

</dependency>

 

<dependency>

<groupId>ch.qos.logback</groupId>

<artifactId>logback-classic</artifactId>

<version>${ch.qos.logback-version}</version>

<exclusions>

<exclusion>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

</exclusion>

</exclusions>

<scope>runtime</scope>

</dependency>

 

 

</dependencies>

 

</project>

 

 

 


beans.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">

 

</beans>

 

 


MainClass.java

 

더보기
더보기

package kr.co.softcampus.main;

 

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class MainClass {

 

public static void main(String[] args) {

 

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml");

 

ctx.close();

 

}

 

}

 


데이터 추가

 

beans.xml에 데이터를 추가한다.

<bean id="hello" class="kr.co.softcampus.beans.HelloWorldEn"/>

일단, kr/co/softcampus/beans/ 에다가 자바클래스파일을 만들면 이렇게 beans.xml에 닉네임인 hello를 붙여서 선언해주어야 한다.

 

 

MainClass.java

package kr.co.softcampus.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import kr.co.softcampus.beans.HelloWorld;

public class MainClass {

	public static void main(String[] args) {
		
		//beans.xml 파일을 로딩한다.
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml");
		
		
		//beans.xml에 정의한 bean 객체의 주소값을 가져온다.
		HelloWorld hello1 = (HelloWorld)ctx.getBean("hello");
		callMethod(hello1);
		
		HelloWorld hello2 = ctx.getBean("hello",HelloWorld.class);
		callMethod(hello2);
		
		ctx.close();
		
	}

	public static void callMethod(HelloWorld hello) {
		// TODO Auto-generated method stub

	}
	
}

--> 정리하자면 이 MainClass.java 파일을 보면 어디에도 HelloWorldEn 이라는게 없다.

 

아이디가 "hello"라고 지정되어 있는 클래스를 가지고 객체를 만들어서 가지고 오라는 이야기이다.

즉 클래스를 변경하고자 한다면 beans.xml 의

더보기
더보기

<bean id="hello" class="kr.co.softcampus.beans.HelloWorldEn"/>

이 부분만 수정하면 된다.

이렇게 하면 유지보수가 매우 용이해진다.