Back-end/Spring

프로젝트 생성

calvin9150 2021. 3. 11. 21:50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<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>spring4</groupId>
  <artifactId>testPjt01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>
 
    </dependencies>
 
 
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
</project>
cs

 

1
2
3
4
5
6
7
8
9
10
<?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="tWalk" class = "testPjt01.TransportationWalk" />
         
 </beans>
cs
1
2
3
4
5
6
7
8
9
10
11
package testPjt01;
 
public class TransportationWalk {
    
    public void move() {
        System.out.println("도보로 이동 합니다.");
    }
    
    
}
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package testPjt01;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class MainClass {
 
    public static void main(String[] args) {
 
//        TransportationWalk transportationWalk = new TransportationWalk(); //생성자 호출 : 메모리에 로드
//        transportationWalk.move(); // 그 다음 메서드 호출인데... 스프링에선 필요없다.xml.. 컨테이너에서 알아서 관리해줌.
        
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");
        // 컨테이너 생성
        
        TransportationWalk transportationWalk = ctx.getBean("tWalk", TransportationWalk.class);
        //컨테이너에서 어떤 녀석(id가 tWalk, 데이터 타입이 TransportationWalk)을 사용하겠다.. getBean이 가져오는 메서드
        transportationWalk.move();
        
        ctx.close(); // 외부 리소스를 받았으면 닫아주어야 하는게 자바 원칙.
            
    }
 
}
cs