ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Framework] 시작, bean
    Spring/Framework 2022. 9. 10. 13:21

    Workspace 경로 변경

    JAVA파일에 spring 폴더를 생성해 workspace 경로 설정

     
    project 생성
    File → New → Maven Project

     

    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>spring</groupId>
      <artifactId>Sp01</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>SpringEx</name>
      
      <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>
    
    수정 후 Update Project

    package first;
    public interface Calculator {
    	public int cal(int firstNum, int secondNum);
    }
    package first;
    public class Sum implements Calculator{
    	public int cal(int firstNum, int secondNum) {
    		int result=0;
    		result=firstNum+secondNum;
    		return result;
    	}
    }
    package first;
    public class Sub implements Calculator{
    	public int cal(int firstNum, int secondNum) {
    		int result=0;
    		result=firstNum-secondNum;
    		return result;
    	}
    }
    package first;
    public class MainClass {
    	public static void main(String[] args) {
    		Calculator c=new Sum();
    		int result=c.cal(10,20);
    		System.out.println("10+20="+result);
    		c=new Sub();
    		result=c.cal(10, 20);
    		System.out.println("10-20="+result);
    	}
    }
    MainClass 실행 결과

    bean

    <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>spring</groupId>
      <artifactId>Sp02</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>SpringEx</name>
       
      <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>
    package com.mav.sq02;
    
    public class WalkClass {
    	public void move() {
    		System.out.println("Walking");
    	}
    }
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!-- 현재 폴더는 스프링 컨테이너로 사용되고 있음을 알리는 태그: <beans> -->
    <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이라는 태그로 등록 -->
    <bean class="com.mav.sq02.WalkClass"  id="cWalk"></bean>
    </beans>
    package com.mav.sq02;
    
    import org.springframework.context.support.GenericXmlApplicationContext;
    
    public class MainClass {
    
    	public static void main(String[] args) {
    
    		// 일반적인 클래스의 객체 생성 및 사용의 예
    		WalkClass wc=new WalkClass();
    		wc.move();
    		
    		// 스프링 프레임워크에서는 클래스의 객체 생성 및 운영이 다르게 운영됨
    		// 위처럼 필요할 때 new 인스턴스를 생성하지 않고 
    		// 프로그램 시작시에 미리 생성&보관하고 있다가 필요할 때 꺼내쓰는 방법을 사용함
    		
    		// 만들어진 인스턴스 보관장소를 "스프링 컨테이너"라고 부름
    		// 현재 프로젝트에서 사용할 스프링 컨테이너는 applicationContext.xml임
    		// applicationContext.xml은 기본 위치기ㅏ src-main-resources 폴더가 됨
    
    		// 스프링 컨테이너에 담겨 있는 객체들을 Bean이라고 부름
    		// 스프링 컨테이너에 담겨 있는 빈을 필요할 때 꺼내 쓰려면 아래와 같이 
    		// 컨테이너의 사용권한을 갖고 있는 객체를 생성하여 사용
    		GenericXmlApplicationContext ctx
    		=new GenericXmlApplicationContext("classpath:applicationContext.xml");
    		// 스프링 컨테이너에서 빈을 꺼내 쓸 수 있는 기능이 있는 객체를 생성함
    		
    		// 꺼내는 방법은 기존 new 인스턴스를 래퍼런스 변수에 저장하듯
    		// ctx로 꺼낸 빈을 랠퍼런스 변수에 저장하듯 사용함
    		WalkClass wc2=ctx.getBean("cWalk",WalkClass.class);
    		// cWalk: 스프링 컨테이너 내부에 있는 그 클래스의 id 값
    		// WalkClasss.class: 스프링컨테이너 내부에 있는 그 클래스의 이름
    		
    		wc2.move();
    		ctx.close();
    		
    		// 아직까지 일반 자바 프로젝트에서 사용하는 new WalkCLass()와 사용상
    		// 차이점은 없어 보이기도 하고 오히려 불편해보이기도 함
    		// 차이점이라면 new 인스턴스를 사용하면 사용할 때마다 새로운 인스턴스가 생성되는 반면
    		// getBean()은 싱글턴 방식처러머 하나의 생성된 객체가 계속 사용된다는 점이 다름
    		// getBean()으로 같은 클래스의 서로 다른 인스턴스를 두개 사용해야 한다면
    		// 다른 id 값으로 Bean을 두개 추가하여 사용함
    		// <bean class="com.mav.sp02.WalkClass" id="cWalk"></bean>
    	}
    }

    댓글

Designed by Tistory.