Spring 간단 한 의존 주입 예

3676 단어 springsshJ2EE
이 사례 는'경량급 Java EE 기업 응용 실전(제3 판)-Struts 2+Spring 3+Hibernate'에서 발췌 한 것 이다.
준비:
eclipse 개발 환경,spring-framework-3.0.5.CI-834-dependencies,com.springsource.org.apache.comons.logging-1.1.1.jar
(그 중 eclipse 는 http://www.eclipse.org/downloads/  다운로드,spring 파일http://www.springsource.org/download/community 책 에 있 는 CD 에서 다운로드,jar 파일 제공)
  • eclipse 를 열 고 자바 프로젝트 를 만 듭 니 다
  • New-->Java project-->항목 이름 입력-->Finish
    2. 
    필요 한 라 이브 러 리 도입
    spring-framework-3.0.5.CI-834-dependencies 를 하 드 디스크 의 한 디 렉 터 리 로 압축 해제 합 니 다.
    jar 파일 을 사용자 라 이브 러 리 로 가 져 오기:Window-->Preferences-->Java-->Build Path->User Libraries-->New...->사용자 라 이브 러 리 이름 을 입력 하 십시오(이 예 는 spring 과 comming-logging)-->Add External JARs...(spring 사용자 라 이브 러 리 에 대해 이전 단계 의 압축 을 풀 디 렉 터 리 를 지정 합 니 다.comming-logging 에 대해 첨부 파일 에서 다운로드 할 com.springsource.org.apache.comons.logging-1.1.1.jar 를 지정 합 니 다)
    3.    예 항목 도입 에 필요 한 라 이브 러 리
    오른쪽 클릭 예 항목-->Build Path-->Add Libraries..-->User Library 선택-->Next-->방금 도 입 된 라 이브 러 리 선택-->Finish
    4.    예 를 들 어 프로젝트 준비 작업 이 완료 되 었 습 니 다.다음은 코드 작성 입 니 다.
    src 디 렉 터 리 아래 각각 세 개의 가방 을 만 듭 니 다.가방 이름 은 각각 
     apprun  
    프로그램 시작 코드 파일 저장
     wyrhero.app.service
    POJO 인터페이스 추상 코드 파일 저장
    wyrhero.app.service.impl
    구현 POJO 인터페이스 구현 코드 파일 저장
    wyrhero.app.service 패키지 에서 다음 코드 파일 을 만 듭 니 다:
    Axe.java
    package wyrhero.app.service;
    
    public interface Axe {
    	public String chop();
    }
    

    Person.java
    package wyrhero.app.service;
    
    public interface Person {
    	public void useAxe();
    }
    

    wyrhero.app.service.impl 패키지 에서 다음 코드 파일 을 만 듭 니 다:
    Chinese.java
    package wyrhero.app.service.impl;
    
    import wyrhero.app.service.Axe;
    import wyrhero.app.service.Person;
    
    public class Chinese implements Person {
    
    	private Axe axe;
    	
    	
    	public void setAxe(Axe axe) {
    		this.axe = axe;
    	}
    
    
    	public void useAxe() {
    		System.out.println(axe.chop());
    	}
    
    }

    StoneAxe.java
    package wyrhero.app.service.impl;
    
    import wyrhero.app.service.Axe;
    
    public class StoneAxe implements Axe {
    
    	public String chop() {
    		return "      ";
    	}
    }
    

    apprun 패키지 에서 시작 코드 파일 만 들 기:
    BeanTest.java
    package apprun;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import wyrhero.app.service.Person;
    
    public class BeanTest {
    	public static void main(String[] args) throws Exception
    	{
    		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
    		Person p = ctx.getBean("chinese", Person.class);
    		p.useAxe();
    	}
    }
    

    5.항목 설정
    프로젝트 의 src 파일 에 bean.xml 라 는 xml 파일 을 만 들 고 다음 내용 을 입력 하 십시오.
    bean.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Spring        ,  spring-beans-3.0.xsd     -->
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://www.springframework.org/schema/beans"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    	<bean id="chinese" class="wyrhero.app.service.impl.Chinese">
    		<property name="axe" ref="stoneAxe" />
    	</bean>
    	
    	<bean id="stoneAxe" class="wyrhero.app.service.impl.StoneAxe" />
    </beans>
    

    6.실행 예
    오른쪽 클릭 항목-->Run As-->Java 응용 프로그램
    Console 창 에서 실행 결 과 를 볼 수 있 습 니 다.

    좋은 웹페이지 즐겨찾기