Spring 프로필 분석

10878 단어 Spring프로필
Spring 프로필 개요
간단 한 소개
Spring 의 설정 파일 은 Spring 공장 에서 Bean 생 성,의존 관계 주입 및 Bean 예제 배 포 를 지도 하 는'도면'입 니 다.그 는 하나 이상 의 벽돌 이 있 는 XML 문서 입 니 다.J2EE 프로그래머 는 이'도면'을 유연 하 게 활용 하여 자신의'생 성 의도'를 정확하게 표현 하 는 것 을 배 워 야 합 니 다.
Spring 프로필 의 예제
Spring 프로필 의 일반 구조

Spring 용기 고 층 보기
Spring 용기 시작 기본 조건:
Spring 의 프레임 패키지
Bean 의 설정 정보
Bean 의 메타 데이터 정보
Bean 의 실현 류

Bean 의 속성 정보
예 를 들 어 데이터 원본 의 사용자 이름,비밀번호
Bean 의 의존 관계
Spring 의존 관계 설정 에 따라 Bean 간 조립 완료
Bean 의 행동 설정
예 를 들 어 생명주기 범위,생명주기 각 과정의 반전 함수
Bean 생 성 방식
빈 이 가 구조 기 를 통 해서 만 들 었 는 지 공장 방법 을 통 해서 만 들 었 는 지 설명 하 는 거 예요.
Bean 의 실현 류
XML 기반 설정
Spring 의 설정 파일 은 XML 형식 을 기반 으로 합 니 다.Spring 1.0 의 설정 은 DTD 형식 을 사용 합 니 다.Spring 2.0 이후 에는 Schema 형식 을 사용 합 니 다.후 자 는 서로 다른 유형의 설정 에 자신의 네 임 스페이스 를 가지 게 하고 설정 파일 이 더욱 확장 되 었 습 니 다.
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
<aop:config>
<aop:pointcut expression="execution(* com.sale.service.*.*(..))" id="mycut"/>
</aop:config>
</beans>
xmlns="http://www.springframework.org/schema/beans":기본 네 임 공간 을 표시 하 며 Spring Bean 에 사용 합 니 다.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance":xsi 표준 네 임 스페이스 를 표시 합 니 다.사용자 정의 네 임 스페이스 를 지정 하 는 schema 파일 입 니 다.
xmlns:op="http://www.springframework.org/schema/aop":사용자 정의 네 임 스페이스 를 표시 합 니 다.op 은 이 네 임 스페이스 의 줄 임 말 을 표시 합 니 다.
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd">:모든 네 임 스페이스 에 구체 적 인 schema 파일 을 지정 하 는 데 사용 합 니 다.
:기본 네 임 스페이스 설정


:aop 네 임 스페이스 설정
Schema 파일 의 용도
Spring 3.0 의 설정 Schema 파일 은 각 모듈 클래스 패키지 에 있 습 니 다.모듈 에 해당 하 는 Schema 파일 이 있 으 면 모듈 클래스 패키지 에서 config 디 렉 터 리 를 찾 을 수 있 습 니 다.Schema 파일 은 이 디 렉 터 리 와 같 습 니 다.다음은 이러한 Schema 파일 에 대한 용도 입 니 다.
예시 설명:spring-aop-3.0.xsd
네 임 스페이스:http://www.springframework.org/schema/aop
Schema 파일:http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
1.Spring-beans.xsd:Bean 설정 에 사용
2.Spring-aop-3.0.xsd:AOP 설정
3.Spring-tx-3.0.xsd:성명 식 사무 설정 의 Schema
4.Spring-mvc-3.0.xsd:3.0 새로 추 가 된
5.Spring-utils-3.0.xsd:복잡 한 표준 설정 을 간소화 합 니 다.
6.Spring-jee-3.0.xsd:jee 의 ejb 와 jndi 등 기능 을 간소화 하기 위 한 설정 입 니 다.
7.Spring-jdbc-3.0.xsd:3.0 새로 추 가 된 것 으로 Spring 내 접 데이터베이스 에서 제공 하 는 Schema 를 설정 합 니 다.
8.Spring-jms-3.0.xsd:jms 설정
9.Spring-lang-3.0.xsd:동적 언어 에 대한 지원 을 추가 하고 동적 언어 정 의 를 통합 합 니 다.
10.Spring-oxm-3.0.xsd:설정 대상 xml 이 Schema 에 매 핑 됨
11.Spring-task-3.0.xsd:작업 스케줄 링 의 Schema
12.Spring-tool-3.0.xsd:통 합 된 Spring 유용 도구 로 정 의 된 Schema
Spring Bean 의 이름
빈 마다 하나 이상 의 id 가 있 을 수 있 습 니 다.우 리 는 첫 번 째 id 를'식별 자'로 만 들 고 나머지 id 는 id 별명 이 라 고 합 니 다.이 id 는 IoC 용기 에서 유일 해 야 합 니 다.
Bean id 의 명명 방식
모든 한정 클래스 이름 설정,유일<bean class="com.sale.entity.SaleProduct" ></bean>지정 id,유일<bean  id="saleProduct"class="com.sale.entity.SaleProduct" ></bean>지정 name,유일<bean name="saleProduct" class="com.sale.entity.SaleProduct" ></bean>id 와 name 을 지정 합 니 다.유일 합 니 다.<beanid="saleProduct"name="saleProduct"class="com.sale.entity.SaleProduct" ></bean>여러 name 을 지정 합 니 다.<bean name="bean1;alias1;alias2" class="com.sale.entity.SaleProduct" ></bean>여러 id 지정,유일<bean id="bean1;alias1;alias2" class="com.sale.entity.SaleProduct" ></bean>별명

<bean id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
<alias name="saleProduct" alias="alias1"/>
Bean id 의 명명 약속
1.xml 명명 규범 에 따른다.
2.알파벳,숫자,밑줄 로 구성
3.낙타 봉 식,첫 번 째 단어 이니셜 소문 자,두 번 째 하지만 첫 번 째 자모 대문자 시작
Spring Bean 의 정례 화
Spring IoC 용 기 는 Bean 을 어떻게 예화 합 니까?전통 적 인 응용 프로그램 은 new 와 반사 방식 을 통 해 빈 을 예화 할 수 있다.2 Spring IoC 용 기 는 Bean 이 정의 한 설정 메타 데이터 에 따라 반사 체 제 를 사용 하여 Bean 을 만들어 야 합 니 다.
Spring IoC 용기 가 Bean 예제 를 만 드 는 방식
구조 기 를 사용 하여 빈 을 예화 하 다.
기본 구조<bean id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>매개 변수 없 는 구조 가 있어 야 합 니 다.
유 삼 구조

<bean id="saleProduct" class="com.sale.entity.SaleProduct" >
<constructor-arg name="prodName" value="  " ></constructor-arg>
</bean>
매개 변수 구조 가 있어 야 합 니 다.
정적 공장 을 사용 하여 Bean 을 예화 합 니 다.
필요 한 class 속성,factory-method 속성 은 빈 을 예화 하 는 방법 을 지정 하고 정적 공장 방법 을 사용 하여 방법 적 인 파 라 메 터 를 지정 할 수 있 습 니 다.Spring IoC 용 기 는 이 속성 이 지정 한 방법 으로 빈 을 가 져 옵 니 다.

<bean factory-method="newInstance" id="saleProduct" class="com.sale.entity.SaleProduct" >
<constructor-arg index="0" value="Hello" ></constructor-arg>
</bean>
실례 공장 방법 을 사용 하여 빈 을 실례 화하 다
class 속성 을 지정 할 수 없습니다.factory-bean 은 공장 Bean,factory-method 는 빈 을 예화 하 는 방법 을 지정 하고 인 스 턴 스 공장 방법 을 사용 하여 파 라 메 터 를 지정 할 수 있 습 니 다.

<!--       Bean -->
<bean id="saleProduct1" class="com.sale.entity.SaleProduct" ></bean>
<!--       Bean -->
<bean id="saleProduct2" factory-bean="saleProduct1" >
<constructor-arg index="0" value="Hello" ></constructor-arg>
</bean>
Spring Bean 의 역할 영역
Spring Bean 에서 말 하 는 역할 영역 은 설정 파일 에서'scope'입 니 다.대상 지향 프로 그래 밍 에서 일반적으로 대상 이나 변수 간 의 보 이 는 범 위 를 가리킨다.Spring 용기 에 서 는 Bean 대상 이 다른 Bean 대상 에 비해 요청 범 위 를 말 합 니 다.
Spring Bean 의 역할 영역 유형
Singleton
Spring IoC 용기 에는 Bean 의 인 스 턴 스 만 존재 합 니 다.Bean 은 단리 방식 으로 존재 합 니 다.단일 인 스 턴 스 모델 은 가장 중요 한 설정 모델 중 하나 입 니 다.Spring 에서 이 를 초월 하여 비 스 레 드 안전 대상 에 게 단일 모드(일반적으로 DAO 층 에 사용)를 사용 할 수 있 습 니 다.

<bean scope="singleton" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>prototype
용기 에서 Bean 을 호출 할 때마다 getBea()를 호출 할 때마다 new Bean()을 실행 하 는 것 과 같은 새로운 인 스 턴 스 를 되 돌려 줍 니 다.기본적으로 Spring 용 기 는 시작 할 때 propotype 의 Bean 을 예화 하지 않 습 니 다.

<bean scope="prototype" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>Spring 의 WebApplication Conext 를 사용 할 때 다른 세 가지 Bean 의 역할 도 메 인,즉 request,session,globleSession 을 사용 할 수 있 습 니 다.웹 응용 환경 과 관련 된 Bean 역할 영역 을 사용 할 때 웹 용기 에 추가 설정 을 해 야 합 니 다.
저 버 전 웹 용기 설정:

<filter>
 <filter-name>requestContextFilter</filter-name>
 <filter-class>org.springframework.web.RequestConextFilter</filter-class>
</filter>
<filter-mapping>
 <filter-name>requestContextFilter</filter-name>
 <servlet-name>/*</servlet-name>
</filter-mapping>
높 은 버 전의 웹 용기 설정:

<listener>
 <listener-class>
org.springframework.web.context.request.RequestConextLinstener
 </listener-class>
</listener>
request
http 요청 을 시작 할 때 Spring 은 새로운 인 스 턴 스 를 만 듭 니 다.<bean scope="request" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>Session
현재 세 션<bean scope="Session" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>Global session
httpSession 세 션<bean scope="Global session" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>사용자 정의 역할 영역
spring 2.0 에서 Spring 의 Bean 역할 도 메 인 체 제 는 확장 할 수 있 습 니 다.이 는 Spring 이 제공 하 는 미리 정 의 된 Bean 역할 도 메 인 을 사용 할 수 있 을 뿐만 아니 라 자신의 역할 도 메 인 을 정의 할 수 있 습 니 다.심지어 기 존의 역할 도 메 인 을 다시 정의 할 수 있 습 니 다(이렇게 하 는 것 을 권장 하지 않 으 며 내 장 된 sinleton 과 prototype 역할 도 메 인 을 덮어 쓸 수 없습니다)
사용자 정의 Scope 클래스 구현:Org.springframework.bean.factory.config.scope사용자 정의 Scope 클래스 등록:ConfigurableBeanFactory.registerScope(String scopeName,Scope scope)사용자 정의 Scope 사용 하기:

Scope customScope = new ThreadScope();
beanFactory.registerScope(“thread”,customScope);
<bean id=”***” class=”***” scope=”scopeName”>
이상 은 본 고의 모든 내용 입 니 다.본 고의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 도움 이 되 기 를 바 랍 니 다.또한 저 희 를 많이 지지 해 주시 기 바 랍 니 다!

좋은 웹페이지 즐겨찾기