[SPRING] ๐Ÿงฉ Maven Project

์ž„๋‚™ํ˜„ยท2022๋…„ 10์›” 19์ผ
0

SpringFramework

๋ชฉ๋ก ๋ณด๊ธฐ
5/7
post-thumbnail
post-custom-banner

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Maven ์„ค์น˜ ๋ฐ Maven project๋ฅผ importํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์„ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


0. ๐Ÿ“ Maven ์„ค์น˜

1) โ›บ Maven ๋‹ค์šด๋กœ๋“œ

https://maven.apache.org/
์œ„์˜ ์‚ฌ์ดํŠธ์— ๋“ค์–ด๊ฐ€์„œ ์ตœ์‹ ๋ฒ„์ „์˜ Maven์„ ๋‹ค์šด๋ฐ›์•„ ์ค๋‹ˆ๋‹ค.



2) โ›บ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •

์ €๋Š” ์‹œ์Šคํ…œ ๋ณ€์ˆ˜ ๊ฐ€ ์•„๋‹Œ ์‚ฌ์šฉ์ž ๋ณ€์ˆ˜ ๋กœ ์„ค์ •ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

  • PATH : Path ๊ฒฝ๋กœ์— "[๋ฉ”์ด๋ธ ์„ค์น˜ํด๋”]\bin"์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ €์˜ ๊ฒฝ์šฐ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด cmd ์ฐฝ์— mvn - version ๋ฅผ ์ž…๋ ฅํ•˜์˜€์„ ๋•Œ, ์„ค์น˜ํ•œ Maven์˜ ๋ฒ„์ „์ด ๋‚˜์˜ค๋ฉด ์„ฑ๊ณต์ž…๋‹ˆ๋‹ค.




1. ๐Ÿ“ Maven project

1) โ™พ๏ธ ํ”„๋กœ์ ํŠธ ํด๋” ์ƒ์„ฑ

workspace ์•„๋ž˜์— import ํ•  ํ”„๋กœ์ ํŠธ ๋ช…์œผ๋กœ ํด๋”๋ฅผ ๋งŒ๋“  ํ›„ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜์œ„ํด๋”๋“ค์„ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 01.CHAP01 ์ด ํ”„๋กœ์ ํŠธ ๋ช…์ž…๋‹ˆ๋‹ค.



2) โ™พ๏ธ Maven ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

์œ„์™€ ๊ฐ™์ด ํด๋”์ƒ์„ฑ์„ ์™„๋ฃŒํ–ˆ๋‹ค๋ฉด, pom.xml ํŒŒ์ผ์„ ํ”„๋กœ์ ํŠธํŒŒ์ผ ์•„๋ž˜์— ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜ xml ํŒŒ์ผ์„ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค. ๋ฉ”๋ชจ์žฅ์œผ๋กœ๋„ ์ถฉ๋ถ„ํžˆ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ €๋Š” Notepad++๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ ํ›„ ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.

C:/PRACTICE/00.TOBY/01.CHAP01/workspace/01.CHAP01/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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
		http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>sp5</groupId>
	<artifactId>01.CHAP01</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.0.2.RELEASE</version>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>utf-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
	
</project>

  • ์ฃผ์š” ์ฝ”๋“œ
<artifactId>01.CHAP01</artifactId>
 : ํ”„๋กœ์ ํŠธ์˜ ์‹๋ณ„์ž๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ”„๋กœ์ ํŠธ ํด๋”์™€ ๋™์ผํ•˜๊ฒŒ ์ง€์ • ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.0.2.RELEASE</version>
</dependency>
 : ํ”„๋กœ์ ํŠธ์—์„œ 5.0.2.RELEASE ๋ฒ„์ „์˜ spring-context ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
 
 <plugin>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.7.0</version>
	<configuration>
		<source>1.8</source>
		<target>1.8</target>
		<encoding>utf-8</encoding>
	</configuration>
</plugin>
 : JDK 1.8 ๋ฒ„์ „์œผ๋กœ ์ž๋ฐ” ์†Œ์Šค๋ฅผ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค. ์ž๋ฐ” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ฝ์„ ๋•Œ, ์‚ฌ์šฉํ•  ์ธ์ฝ”๋”ฉ์€ UTF-8 ์ž…๋‹ˆ๋‹ค.

โ‘  Maven dependency ์„ค์ •

์œ„์˜ pom.xml ์—์„œ dependency์™€ plugin์˜ ๋‘ ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ dependency ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.0.2.RELEASE</version>
</dependency>

Maven์€ ํ•œ ๊ฐœ์˜ ๋ชจ๋“ˆ์„ artifact๋ผ๋Š” ๋‹จ์œ„๋กœ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์œ„ ์„ค์ •์€ spring-context๋ผ๋Š” ์‹๋ณ„์ž๋ฅผ ๊ฐ€์ง„ 5.0.2.RELEASE ๋ฒ„์ „์˜ ์•„ํ‹ฐํŒฉํŠธ์— ๋Œ€ํ•œ ์˜์กด(dependency)์„ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ์˜์กด์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์ธ ์ž๋ฐ” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํด๋ž˜์Šค ํŒจ์Šค์— spring-context ๋ชจ๋“ˆ์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.

๊ฐ ์•„ํ‹ฐํŒฉํŠธ์˜ ์™„์ „ํ•œ ์ด๋ฆ„์€ ์•„ํ‹ฐํŒฉํŠธ์ด๋ฆ„-๋ฒ„์ „.jar์ด๋ฏ€๋กœ, ์œ„ ์„ค์ •์€ Maven ํ”„๋กœ์ ํŠธ์˜ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•˜๊ณ  ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค ํŒจ์Šค์— spring-context-5.0.2.RELEASE.jar ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธ ํ•ฉ๋‹ˆ๋‹ค.

โ‘ก Maven repository

์ด์ „ ํฌ์ŠคํŒ…์ธ ๐Ÿงฉ ํ”„๋กœ์ ํŠธ ์„ธํŒ…์„ ๋ณด๋ฉด ํ”„๋กœ์ ํŠธ ๋‚ด์— repository ํด๋”์™€ setting.xml ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š”๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์„ ํ™œ์šฉํ•˜๋ฉด ํ”„๋กœ์ ํŠธ์—์„œ ๋‹ค์šด๋กœ๋“œ๋˜๋Š”
Maven ํŒŒ์ผ๋“ค(ex. spring-context-5.0.2.RELEASE.jar)์ด ๋ชจ๋‘ ํ”„๋กœ์ ํŠธ ํด๋” ๋‚ด์˜ repository๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.



3) โ™พ๏ธ Project import

Maven ํ”„๋กœ์ ํŠธ๋ฅผ Eclipse์— import ํ•˜๋ ค๋ฉด File - Import... ๋ฉ”๋‰ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฉ”๋‰ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์ฐฝ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ™”๋ฉด์—์„œ Maven/Existing Maven Projects๋ฅผ ์„ ํƒํ•œ ํ›„ Next ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ Browse... ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์•ž์„œ ์ƒ์„ฑํ•œ Maven ํ”„๋กœ์ ํŠธ ํด๋”๋ฅผ Root Directory: ๋กœ ์„ ํƒํ•ด ์ฃผ๋ฉฐ, ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋‚˜์˜จ๋‹ค๋ฉด Finish ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์„œ ๋งˆ๋ฌด๋ฆฌํ•ด ์ค๋‹ˆ๋‹ค.

๊ธฐ๋‹ค๋ฆฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด Eclipse์— ํ”„๋กœ์ ํŠธ๊ฐ€ import ๋˜๋Š”๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Eclipse์— ์ƒ์„ฑ๋œ ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณด๋ฉด ๋‹ค์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • src/main/java ํด๋”
    : ์†Œ์Šค ํด๋”๋กœ ์ •์˜๋˜์–ด ์žˆ์Œ
  • Maven Dependencies
    : ๋ฉ”์ด๋ธ ์˜์กด์— ์„ค์ •ํ•œ ์•„ํ‹ฐํŒฉํŠธ๊ฐ€ Eclipse ํ”„๋กœ์ ํŠธ์˜ ํด๋ž˜์ŠคํŒจ์Šค์— ์ถ”๊ฐ€๋จ

Maven Dependencies์— ๋“ฑ๋ก๋œ jar ํŒŒ์ผ๋“ค์€ ์•ž์„œ ์„ค๋ช…ํ•œ repository์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ Eclipse๋Š” Maven ํ”„๋กœ์ ํŠธ๋ฅผ import ํ•  ๋•Œ ํ•„์š”ํ•œ ๋ชจ๋“  jarํŒŒ์ผ์„ ๋กœ์ปฌ๋กœ ๋ฐ›์•„์˜จ ๋‹ค์Œ ๊ทธ ํŒŒ์ผ์„ ํด๋ž˜์ŠคํŒจ์Šค์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ Eclipse๋ฅผ ์ด์šฉํ•ด์„œ Spring์„ ์ด์šฉํ•œ ์ž๋ฐ” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ์ค€๋น„๊ฐ€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.



4) โ™พ๏ธ Example

๊ฐ„๋‹จํ•˜๊ฒŒ Spring์„ ์ด์šฉํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ž‘์„ฑํ•  ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.

  • Greeter.java : ์ฝ˜์†”์— ๊ฐ„๋‹จํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ž๋ฐ” ํด๋ž˜์Šค
  • AppContext.java : Spring ์„ค์ • ํŒŒ์ผ
  • Main.java : main() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด Spring๊ณผ Greeter๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ž๋ฐ” ํด๋ž˜์Šค

๋จผ์ € Eclipse์—์„œ src/main/java ์†Œ์Šค ํด๋”์— chap02 ํŒจํ‚ค์ง€๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ํด๋ž˜์Šค๋“ค์„ ๋ชจ๋‘ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค.


01.CHAP01/src/main/java/chap02/Greeter.java

package chap02;

public class Greeter {

	private String format;
	
	public String greet(String guest) {
		return String.format(format, guest);
	}
	
	public void setFormat(String format) {
		this.format = format;
	}
	
}

Greeter ํด๋ž˜์Šค์˜ greet() ๋ฉ”์„œ๋“œ๋Š” String์˜ ๋ฌธ์ž์—ด ํฌ๋งท์„ ์ด์šฉํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. greet() ๋ฉ”์„œ๋“œ์—์„œ ์‚ฌ์šฉํ•  ๋ฌธ์ž์—ด ํฌ๋งท์€ setFormat() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์„ค์ •ํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Greeter ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด Greeter ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๋’ค, setFormat() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ž์—ด ํฌ๋งท์„ ์ง€์ •ํ•˜๊ณ , greet() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ์„ฑํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Greeter greeter = new Greeter();
greeter.setFormat("%s, ํ—ค์œ„");
String str = greeter.greet("Spring");	//str์€ "Spring, ํ—ค์œ„"๊ฐ€ ๋œ๋‹ค.

01.CHAP01/src/main/java/chap02/AppContext.java

package chap02;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppContext {

	@Bean
	public Greeter greeter() {
		Greeter g = new Greeter();
		g.setFormat("%s, ํ—ค์œ„");
		return g;
	}
	
}

06ํ–‰์˜ @Configuration ์–ด๋…ธํ…Œ์ด์…˜์€ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ Spring ์„ค์ • ํด๋ž˜์Šค๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

AppContext.java์—์„œ ํ•ต์‹ฌ์€ @Bean ์–ด๋…ธํ…Œ์ด์…˜ ์•„๋ž˜์— ์žˆ๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. Spring์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š”๋ฐ, ์ด ์ฝ”๋“œ๊ฐ€ ํ•œ ๊ฐœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์„ค์ •์„ ๋‹ด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

Spring์ด ์ƒ์„ฑํ•˜๋Š” ๊ฐ์ฒด๋ฅผ Bean ๊ฐ์ฒด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, ์ด Bean ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ greeter() ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ์—๋Š” @Bean ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

@Bean ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ฉ”์„œ๋“œ์— ๋ถ™์ด๋ฉด ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ Spring์ด ๊ด€๋ฆฌํ•˜๋Š” Bean ๊ฐ์ฒด๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

@Bean ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์€ Bean ๊ฐ์ฒด๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Spring์ด ํ˜„์žฌ ์ฝ”๋“œ์˜ @Bean ์•„๋ž˜์—์„œ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ greeter๋ผ๋Š” ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์ด๋ฆ„์€ Bean ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋’ค์—์„œ ์ด ๊ฐ’์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

@Bean ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์•Œ๋งž๊ฒŒ ์ดˆ๊ธฐํ™” ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ๋Š”

g.setFormat("%s, ์•ˆ๋…•ํ•˜์„ธ์š”!");

์—์„œ Greeter ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


01.CHAP01/src/main/java/chap02/Main.java

package chap02;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = 
				new AnnotationConfigApplicationContext(AppContext.class);
		Greeter g = ctx.getBean("greeter", Greeter.class);
		String msg = g.greet("Spring");
		System.out.println(msg);
		ctx.close();
	}
}

  • ์ฃผ์š” ์ฝ”๋“œ
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

: AnnotationConfigApplicationContext ํด๋ž˜์Šค๋Š” ์ž๋ฐ” ์„ค์ •์—์„œ ์ •๋ณด๋ฅผ ์ฝ์–ด์™€ Bean ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.


AnnotationConfigApplicationContext ctx = 
				new AnnotationConfigApplicationContext(AppContext.class);

: AnnotationConfigApplicationContext ๊ฐ์ฒด๋ฅผ ์ƒ์„คํ•  ๋•Œ ์•ž์„œ ์ž‘์„ฑํ•œ AppContext ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑ์ž ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

AnnotationConfigApplicationContext๋Š” AppContext์— ์ •์˜ํ•œ @Bean ์„ค์ • ์ •๋ณด๋ฅผ ์ฝ์–ด์™€ Greeter ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.


Greeter g = ctx.getBean("greeter", Greeter.class);

: getBean() ๋ฉ”์„œ๋“œ๋Š” AnnotationConfigApplicationContext๊ฐ€ ์ž๋ฐ” ์„ค์ •์„ ์ฝ์–ด์™€ ์ƒ์„ฑํ•œ Bean ๊ฐ์ฒด๋ฅผ ๊ฒ€์ƒ‰ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

getBean() ๋ฉ”์„œ๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” @Bean ์–ด๋…ธํ…Œ์ด์…˜์˜ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์ธ Bean ๊ฐ์ฒด์˜ ์ด๋ฆ„์ด๋ฉฐ, ๋‘ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๊ฒ€์ƒ‰ํ•  Bean ๊ฐ์ฒด์˜ ํƒ€์ž…์ž…๋‹ˆ๋‹ค.

์•ž์„œ ์ž‘์„ฑํ•œ ์ž๋ฐ” ์„ค์ •(AppContext)์„ ๋ณด๋ฉด @Bean ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์ด greeter ์ด๊ณ  ์ƒ์„ฑํ•œ ๊ฐ์ฒด์˜ ๋ฆฌํ„ด ํƒ€์ž…์ด Greeter ์ด๋ฏ€๋กœ, 10ํ–‰์˜ getBean() ๋ฉ”์„œ๋“œ๋Š” greeter() ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ์„ฑํ•œ Greeter ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

@Bean
public Greeter greeter() {
	Greeter g = new Greeter();
    g.setFormat("%s, ํ—ค์œ„");
    return g;
}

String msg = g.greet("Spring");

: 11ํ–‰์—์„œ๋Š” Greeter ๊ฐ์ฒด์˜ greet() ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Greet.java ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด greet() ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

// format ํ•„๋“œ๋Š” setFormat() ๋ฉ”์„œ๋“œ๋กœ ์„ค์ •ํ•จ
public String greet(String guest) {
	return String.format(format, guest);
}

์•ž์„œ AppContext ์„ค์ •์— ๋”ฐ๋ฅด๋ฉด ์ƒ์„ฑํ•œ Greeter ๊ฐ์ฒด์˜ format ํ•„๋“œ์˜ ๊ฐ’์€ "%s, ํ—ค์œ„"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ 11ํ–‰์˜ g.greet("Spring") ์ฝ”๋“œ๋Š” ๋‹ค์Œ์˜ ์ฝ”๋“œ์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

String.format("%s, ํ—ค์œ„", "Spring")	// ๊ฒฐ๊ณผ : "Spring, ํ—ค์œ„"



5) โ™พ๏ธ Code Execution

์•„๋ž˜์™€ ๊ฐ™์ด Main.java ์—์„œ ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค๋ฅผ ํด๋ฆญํ•˜์—ฌ Java Application ์„ ํด๋ฆญํ•ด ์ฃผ์–ด๋„ ๋˜๊ณ , ctrl + F11์„ ๋ˆŒ๋Ÿฌ์„œ ์‹คํ–‰ํ•ด ์ฃผ์–ด๋„ ๋ฉ๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋ฉด ์„ฑ๊ณต์ž…๋‹ˆ๋‹ค.




2. ๐Ÿ“ Spring Container

์•ž์—์„œ ์ž‘์„ฑํ•œ Main.java ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ • ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ Bean ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํ•ด๋‹น Bean ๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// 1. ์„ค์ • ์ •๋ณด๋ฅผ ์ด์šฉํ•ด์„œ Bean ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•จ
AnnotationConfigApplicationContext ctx =
	new AnnotationConfigApplicationContext(AppContext.class);

// 2. Bean ๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ•จ
Greeter g = ctx.getBean("greeter", Greeter.class);

ApplicationContext(๋˜๋Š” BeanFactory)๋Š” Bean ๊ฐ์ฒด์˜ ์ƒ์„ฑ, ์ดˆ๊ธฐํ™”, ๋ณด๊ด€, ์ œ๊ฑฐ ๋“ฑ์„ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์–ด์„œ ApplicationContext๋ฅผ ์ปจํ…Œ์ด๋„ˆ(Container)๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

  • Spring Container์˜ Bean ๊ฐ์ฒด ๊ด€๋ฆฌ

    Spring Container๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ Bean ๊ฐ์ฒด์™€ Bean ์ด๋ฆ„์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ •๋ณด๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด chap02.Greeter ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ greeter๋ผ๋Š” ์ด๋ฆ„์˜ Bean์œผ๋กœ ์„ค์ •ํ–ˆ๋‹ค๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ Container๋Š” ๋‹ค์Œ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ greeter ์ด๋ฆ„๊ณผ Greeter ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•œ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

    ์ด๋ฆ„๊ณผ ์‹ค์ œ ๊ฐ์ฒด์˜ ๊ด€๊ณ„๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‹ค์ œ ๊ฐ์ฒด์˜ ์ƒ์„ฑ, ์ดˆ๊ธฐํ™”, ์˜์กด ์ฃผ์ž… ๋“ฑ Spring Container๋Š” ๊ฐ์ฒด ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

1) ๐Ÿท๏ธ Singleton Obejct

์‹ฑ๊ธ€ํŠผ ๊ฐ์ฒด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

01.CHAP01/src/main/java/chap02/Main2.java

package chap02;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main2 {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = 
				new AnnotationConfigApplicationContext(AppContext.class);
		Greeter g1 = ctx.getBean("greeter", Greeter.class);
		Greeter g2 = ctx.getBean("greeter", Greeter.class);
		System.out.println("(g1 == g2) = " + (g1 == g2));
		ctx.close();
	}
}


10ํ–‰๊ณผ 11ํ–‰์€ ์ด๋ฆ„์ด greeter์ธ Bean ๊ฐ์ฒด๋ฅผ ๊ตฌํ•ด์„œ ๊ฐ๊ฐ g1๊ณผ g2 ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

Greeter g1 = ctx.getBean("greeter", Greeter.class);
Greeter g2 = ctx.getBean("greeter", Greeter.class);

12ํ–‰์—์„œ๋Š” g1๊ณผ g2๊ฐ€ ๊ฐ™์€ ๊ฐ์ฒด์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ์ฝ˜์†”์— ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

System.out.println("(g1 == g2) = " + (g1 == g2));

๊ฒฐ๊ณผ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ ์ง€ ๊ถ๊ธˆํ–ˆ๋Š”๋ฐ, ์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด (g1 == g1)์˜ ๊ฒฐ๊ณผ๊ฐ€ true๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.


(g1 == g1)์˜ ๊ฒฐ๊ณผ๊ฐ€ true๋ผ๋Š” ๊ฒƒ์€ g1๊ณผ g2๊ฐ€ ๊ฐ™์€ ๊ฐ์ฒด๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์•„๋ž˜ ์ฝ”๋“œ์—์„œ getBean() ๋ฉ”์„œ๋“œ๋Š” ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Greeter g1 = ctx.getBean("greeter", Greeter.class);
Greeter g2 = ctx.getBean("greeter", Greeter.class);

๋ณ„๋„ ์„ค์ •์„ ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ Spring์€ ํ•œ ๊ฐœ์˜ Bean ๊ฐ์ฒด๋งŒ์„ ์ƒ์„ฑํ•˜๋ฉฐ, ์ด๋•Œ Bean ๊ฐ์ฒด๋Š” Singleton ๋ฒ”์œ„๋ฅผ ๊ฐ–๋Š”๋‹ค๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

Singleton์€ ๋‹จ์ผ ๊ฐ์ฒด(single object)๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋‹จ์–ด๋กœ์„œ Spring์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•œ ๊ฐœ์˜ @Bean ์–ด๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•ด ํ•œ ๊ฐœ์˜ Bean ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ค์ •์„ ์‚ฌ์šฉํ•˜๋ฉด greeter์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ํ•œ ๊ฐœ์™€ greeter1์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ํ•œ ๊ฐœ, ์ด๋ ‡๊ฒŒ ๋‘ ๊ฐœ์˜ Bean ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

@Bean
public Greeter greeter() {
	Greeter g = new Greeter();
	g.setFormat("%s, ํ—ค์œ„");
	return g;
}
@Bean
public Greeter greeter1() {
	Greeter g = new Greeter();
	g.setFormat("์•ˆ๋…•ํ•˜์‹ ๊ฐ€, %s์—ฌ");
	return g;
}

Singleton ๋ฒ”์œ„ ์™ธ์— ํ”„๋กœํ† ํƒ€์ž… ๋ฒ”์œ„๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๊ด€ํ•œ ๋‚ด์šฉ์€ ์ถ”ํ›„์— ๊ธฐํšŒ๊ฐ€ ๋œ๋‹ค๋ฉด ์ •๋ฆฌํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


์˜ค๋Š˜์€ Maven project๋ฅผ importํ•˜์—ฌ ๊ธฐ๋ณธ์ ์ธ ์ฝ”๋“œ๋“ค์„ ์‹คํ–‰ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์—๋Š” DI ์ฆ‰ Dependency Injectioin์— ๋Œ€ํ•œ ํฌ์ŠคํŒ…์„ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ’ก Reference

https://www.kame.co.kr/nkm/detail.php?tcode=306&tbook_jong=3

๐Ÿˆโ€โฌ› git

์‹ค์Šตํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
https://github.com/LimNakHyun/Spring/tree/main/02.CHAPTER2/src/main/java/chap02

profile
์ฝ”๋”ฉํ•˜๋Š” ์ˆ˜ํ•™๊ณผ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€