01_Spring 입문 사례

Spring 입문 사례 -- IoC
  • 가이드 jar 패키지
  • spring-beans-4.2.4.RELEASE.jar
    spring-context-4.2.4.RELEASE.jar
    spring-core-4.2.4.RELEASE.jar
    spring-expression-4.2.4.RELEASE.jar
    commons-logging-1.2.jar
    log4j-1.2.16.jar
    
  • 실체 클래스 작성
  • public class HelloWorld {
        public void show() {
            System.out.println("Hello World show()");
        }
    }
    
  • applicationContext. xml 프로필 작성
  • 
    
        
    
    
    
    
  • 테스트 클래스 작성
  • import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class HelloWorldTest {
        @Test
        public void test1() {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
            helloWorld.show();
        }
        
        @Test
        public void test2() {
            HelloWorld helloWorld = new HelloWorld();
            helloWorld.show();
        }
    }
    
    // ApplicationContext   BeanFactory       ,           ApplicationContext      ClassPathXmlApplicationContext 。
    

    Spring 입문 사례 -- DI
  • 상례 HelloWorld 클래스 에 속성 과 대응 하 는 getter / setter 방법 을 추가 합 니 다
  • public class HelloWorld {
        private String info;
        
        public String getInfo() {
            return info;
        }
        public void setInfo(String info) {
            this.info = info;
        }
        public void show() {
            System.out.println("Hello World" + info);
        }
    }
    
  • applicationContext. xml 파일 에 property 태그 설정
  • 
    
        
        
            
        
    
    
  • 테스트 코드
  • public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
        helloWorld.show();//HelloWorld  info       
    }
    

    IoC 와 DI 개념 비교
  • IoC 가 반전 을 제어 하 는 것 은 대상 의 정례 화 권 리 를 Spring 용기 에서 관리 하 는 것 을 말한다.
  • DI 의존 주입 은 Spring 용기 가 대상 을 만 드 는 과정 에서 대상 이 의존 하 는 속성 이 설정 을 통 해 대상 에 주입 되 는 것 을 말한다.
  • 좋은 웹페이지 즐겨찾기