본문 바로가기
Back-end/Spring Framework

java spring 프로젝트 생성과 실행 기초

by javapp 자바앱 2020. 7. 21.
728x90

1. 메이븐 프로젝트로 생성

 

2. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>spring4</groupId>

  <artifactId>test1</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-conpiler-plugin</artifactId>

                    <version>3.1</version>

                    <configuration>

                           <source>1.8</source>

                           <target>1.8</target>

                           <encoding>utf-8</encoding>

                    </configuration>

              </plugin>

       </plugins>

  </build>

</project>

 

 

 

3. src/main/resources/applicationContext.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="tWalk" class="test1.TransportationWalk" />  <!-- 불러올 클래스 -->

      

</beans>

 

 

4. 메인클래스에서 컨테이너에 있는 빈 불러 오기

 

       public static void main(String[] args) {

//           TransportationWalk t = new TransportationWalk();

//           t.move();

            

             //Xml 불러옴

             GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");

            

             TransportationWalk t = ctx.getBean("tWalk", TransportationWalk.class); //xml의 빈 불러옴

             t.move();

             //외부 리소스 사용하면 클로즈

       }

결과

댓글