각진 세상에 둥근 춤을 추자

[Spring] 개발환경 구축 본문

Spring

[Spring] 개발환경 구축

circle.j 2023. 1. 3. 00:14

1. 프로젝트 생성

 

- [Project Explorer 마우스 우클릭] - [New] - [Other]

 

- Maven Project 클릭 - Next

 

-  Create a simple project 체크 - Next

 

  • Artifact Id: 프로젝트명 입력
  • Group Id: kr.co.프로젝트명

 

 


 

2. pom.xml

- 해당 프로젝트 pom.xml 마우스 우클릭 - Open With - XML Editor

 

- 아래 복사한 내용 붙여넣기 

https://mvnrepository.com/artifact/org.springframework/spring-context/5.3.24

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.24</version>
</dependency>

 


 

3. application.xml (스프링 설정 파일)

https://docs.spring.io/spring-framework/docs/5.3.24/reference/html/core.html#spring-core

 

Core Technologies

In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do

docs.spring.io

 

[해당 프로젝트] - [src/main/resources] - application.xml 생성 

 

해당 프로젝트에 application.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="객체에 접근하기 위한 key 값" class="패키지명+클래스명">  
        <!-- collaborators and configuration for this bean go here -->
    </bean>


</beans>

 

 


 

4. 프로젝트 실습 

 

(1) Greeting.java

package kr.co.ch01;

public class Greeting {
	
	public void show() {
		System.out.println("Greeting Spring!");
	}
	
}

 

(2) Hello.java

package kr.co.ch01;

public class Hello {

	public void show() {
		System.out.println("Hello Spring!");
	}
	
}

 

(3) Welcome.java

package kr.co.ch01;

public class Welcome {

	public void show() {
		System.out.println("Welcome Spring!");
	}
	
}

 

(4) application.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- 스프링 컨테이너에 객체 등록 -->
    <bean id="hello" class="kr.co.ch01.Hello"></bean>
    <bean id="welcome" class="kr.co.ch01.Welcome"></bean>
    <bean id="greeting" class="kr.co.ch01.Greeting"></bean>

</beans>

 

(5) Main.java

package kr.co.ch01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		
		ApplicationContext ctx = new GenericXmlApplicationContext("application.xml");
		
		Hello bean1 = (Hello) ctx.getBean("hello");
		Welcome bean2 = (Welcome) ctx.getBean("welcome");
		Greeting bean3 = (Greeting) ctx.getBean("greeting");
		
		bean1.show();
		bean2.show();
		bean3.show();
		
	}
}

 

'Spring' 카테고리의 다른 글

[Spring] 스프링 MVC 회원 정보 입력 결과 출력하기  (0) 2023.01.05
[Spring] 스프링 MVC + 개발환경 수동 설정 + 간단 실습  (1) 2023.01.05
[Spring] AOP  (0) 2023.01.03
[Spring] IoC&DI  (0) 2023.01.03
[Spring] Spring 소개  (0) 2023.01.02