Spring 자동 조립 Bean 실현 과정 상세 설명

이 글 은 주로 Spring 자동 조립 Bean 실현 과정 에 대한 상세 한 설명 을 소개 하 였 으 며,글 에서 예시 코드 를 통 해 매우 상세 하 게 소개 하 였 으 며,여러분 의 학습 이나 업무 에 대해 어느 정도 참고 학습 가 치 를 가지 고 있 으 므 로 필요 한 분 들 은 참고 하 시기 바 랍 니 다.
자동 조립 을 사용 하려 면요소 의 autowire 속성 을 설정 해 야 합 니 다.autowire 속성 은 다섯 개의 값 이 있 는데 구체 적 으로 표 1 과 같다.
표 1 autowire 의 속성 과 역할
 
명칭.
설명 하 다.
byName
Property name 에 따라 자동 으로 설치 되 며,한 Bean 의 name 이 다른 Bean 의 Property name 과 같 으 면 이 Bean 을 Property 에 자동 으로 설치 합 니 다.
byType
Property 의 데이터 형식(Type)에 따라 자동 으로 조립 되 며,한 Bean 의 데이터 형식 이 다른 Bean 의 Property 데이터 형식 과 호 환 되면 자동 으로 조립 됩 니 다.
constructor
구조 방법의 매개 변수 데이터 형식 에 따라 by Type 모드 의 자동 조립 을 진행 합 니 다.
autodetect
기본 구조 방법 이 발견 되면 constructor 모드 를 사용 합 니 다.그렇지 않 으 면 by Type 모드 를 사용 합 니 다.
no
기본 적 인 상황 에서 자동 조립 을 사용 하지 않 고 Bean 의존 은 ref 요 소 를 통 해 정의 해 야 합 니 다.
다음은 사례 를 통 해 자동 조립 을 어떻게 실현 하 는 지 보 여 준다.우선 applicationContext.xml 프로필 을 자동 조립 형식 으로 수정 합 니 다.다음 과 같 습 니 다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="personDao" class="com.mengma.annotation.PersonDaoImpl" />
<bean id="personService" class="com.mengma.annotation.PersonServiceImpl"
autowire="byName" />
<bean id="personAction" class="com.mengma.annotation.PersonAction"
autowire="byName" />
</beans>
위 설정 파일 에 서 는 personService 와 personAction 을 설정 하 는요소 에 id 와 class 속성 을 제외 하고 autowire 속성 을 추가 하고 속성 값 을 by Name(속성 이름 에 따라 자동 으로 조립)으로 설정 합 니 다.
기본적으로 설정 파일 에 서 는 ref 를 통 해 Bean 을 설치 해 야 하지만 autowire="by Name"이 설정 되 어 있 습 니 다.Spring 은 설정 파일 에서 속성 이름 personDao 와 같은을 자동 으로 찾 습 니 다.찾 은 후 setPersonDao(PersonDao personDao)방법 으로 id 를 personDao 의 Bean 에 id 를 personService 의 Bean 에 주입 합 니 다.이 때 는 ref 를 통 해 조립 할 필요 가 없다.
JUnit 을 사용 하여 테스트 클래스 의 test()방법 을 다시 실행 합 니 다.콘 솔 의 표시 결 과 는 그림 과 같 습 니 다.

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기