Spring 다음날:ioc,di 의 개념,인터페이스 와 dj 를 사용 하여 프로 그래 밍
                                            
 6726 단어  자바 문제 포 지 셔 닝 총괄성능 경험 총화
                    
spring 은 실제 용기 프레임 워 크 로 각종 bean 을 설정 할 수 있 으 며,bean 과 bean 의 관 계 를 유지 할 수 있 습 니 다.어떤 bean 이 필요 할 때 getbean(id)으로 bean 을 가 져 올 수 있 습 니 다.
스프링 핵심 인용 패키지
사례:자모의 대소 문자 변환
인 터 페 이 스 를 만 듭 니 다.ChangeLetter4.567917.두 가지 유형 으로 이 인 터 페 이 스 를 실현 한다4.567917.대상 을 spring 용기 에 배치 합 니 다.4.567918.
인터페이스 ChangeLetter 사용 하기
package com.study.inter
public interface ChangeLetter{
    public String change();
}  클래스 UpperLetter 구현,소문 자 대문자
package com.service;
/**
 * Created by hejian on 16/9/7.
 */
public class UpperLetter implements ChangeLetter{
    private String str;
    public String Change(){
        //       
        str.toUpperCase();
        System.out.println(getStr());
        return str.toUpperCase();
    }
    public String getStr(){
        return str.toUpperCase();
    }
    public void setStr(String str){
        this.str = str;
    }
}  구현 클래스 LowerLetter,대문자 소문 자 변환
package com.service;
/**
 * Created by hejian on 16/9/7.
 */
public class LowerLetter implements ChangeLetter {
    private String str;
    public String Change(){
        //   ->   
        System.out.println(str.toLowerCase());
        return str.toLowerCase();
    }
    public void setStr(String str){
        this.str = str;
    }
    public String getStr(){
        return str;
    }
}  xml 프로필 은 다음 과 같 습 니 다.beans.xml 파일 을 만들어 서 해당 하 는 가방 아래 에 놓 을 수 있 습 니 다.예 를 들 어 com.service 에서 beans.xml 을 만 들 수 있 습 니 다.
<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 ="changeLette" class="com.service.LowerLetter">
        <property name="str" value="ABCED" />
    bean>
    
        
    
beans>  테스트 클래스 생 성
package com.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Created by hejian on 16/9/8.
 */
public class TestSpring {
    public static void main(String[ ] args){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/service/beans.xml");
        LowerLetter changeLetter = (LowerLetter) applicationContext.getBean("changeLette");
       changeLetter.Change();
        //       bean,     ,        ,    beans.xml   bean    changeLette,                  
        ChangeLetter changeLetter1 = (ChangeLetter) applicationContext.getBean("changeLette");
        changeLetter1.Change();
    }
}
  상기 사례 를 통 해 di 결합 인터페이스 프로 그래 밍 이 가 져 온 편리 함 을 알 수 있 고 프로그램 간 의 결합 도 를 줄 일 수 있다.