Spring 간단 한 의존 주입 예
준비:
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 파일 제공)
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 창 에서 실행 결 과 를 볼 수 있 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.