'Spring Framework'에 해당되는 글 10건
- 테스트 코드 작성 관련 링크 2010/02/08
- [iBATIS] 8. Annotation 기반으로 JUnit4를 이용한 Spring TDD ~ 9. Reference (4) 2008/09/03
- [iBATIS] 7. iBATIS + Spring + transaction 2008/09/02
- [dW] Acegi로 자바 애플리케이션 보안화 하기, Part 1: 아키텍처 개요와 보안 필터 (한글) (4) 2008/03/23
- [Spring] 스프링에서 VelocityTools 환경설정 (2) 2008/03/20
- [Spring] Bean과 BeanFactory의 후처리 (8) 2008/02/12
- [Spring] 자동 묶기(Autowire) (2) 2008/02/12
- [Spring] 세터 주입(Setter Injection)의 대안 (5) 2008/02/05
- [Spring] 빈 묶기(Bean wiring) (2) 2008/02/05
- [Spring] 스프링 컨테이너의 두 종류 2008/02/05
테스트 코드 작성 관련 링크
Posted at 2010/02/08 14:46// Posted in 나만의 작업'나만의 작업' 카테고리의 다른 글
| CouchDB 관련 링크 (0) | 2010/02/09 |
|---|---|
| 테스트 코드 작성 관련 링크 (0) | 2010/02/08 |
| 한국 구글 검색 결과 페이지의 'HOT 토픽' (6) | 2009/11/27 |
| [Jersey] WebApplicationException and Mapping Exception to Responses (0) | 2009/07/30 |
| [Jersey] Building Responses (4) | 2009/07/21 |
| [Jersey] Representation and Java Types (0) | 2009/07/16 |
| Jersey의 JSON Support (2) | 2009/06/08 |
| Jersey의 Exception Handling (0) | 2009/06/05 |
| Jersey의 Return Type (0) | 2009/06/04 |
[iBATIS] 8. Annotation 기반으로 JUnit4를 이용한 Spring TDD ~ 9. Reference
Posted at 2008/09/03 10:13// Posted in 나만의 작업/iBatis8. Annotation 기반으로 JUnit4를 이용한 Spring TDD
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback=true)
@RunWith : 테스트를 실행할 org.junit.runner.Runner 구현 클래스를 지정할 수 있다.
스프링은 스프링 컨텍스트 설정 및 DI, 트랜잭션 처리 등을 지원해주는 Runner 구현 클래스를 제공하고 있다.
Junit 4 기반의 테스트에서 스프링 컨텍스트에 설정된 빈 객체를 사용하고 싶다면 SpringJunit4ClassRunner 클래스를 @RunWith 어노테이션 값으로 설정하면 된다.
@ContextConfiguration : 스프링 컨텍스트를 생성할 때 사용될 설정 파일의 경로
@Autowired, @Resource : 테스트 코드에서 필요로 하는 스프링 빈 객체 설정
- 트랜잭션 처리를 위한 설정
@TransactionConfiguration : 테스트 클래스에 적용되며, 트랜잭션 관리자와 기본 롤백 규칙을 설정한다.
@Rollback : 테스트 메소드에 적용되며, 메소드 단위로 롤백 규칙을 설정한다.
@NotTransactional : 트랜잭션을 적용하지 않을 메소드에 적용한다.
@Transactional : 테스트 클래스에 적용. 이 어노테이션을 적용함으로써 각 테스트 메소드는 트랜잭션 범위 내에서 실행된다.
만약 테스트 메소드에서 명시적으로 롤백 여부를 설정하고 싶다면 @Rollback 어노테이션을 사용하면 된다.
@Rollback(false)
테스트 메소드에 대한 트랜잭션이 시작되기 전에 어떤 초기화 작업을 해주어야 한다며, @BeforeTransaction 어노테이션이 적용된 메소드를 작성하면 된다.
반대로 트랜잭션이 종료된 이후에 정리 작업을 해주어야 할 경우에는 @AfterTransaction 어노테이션이 적용된 메소드를 작성하면 된다.
@RunWith (SpringJUnit4ClassRunner. class )
@ContextConfiguration (locations = { "classpath:/com/mydomain/data/beans.xml" })
@TransactionConfiguration (transactionManager = "transactionManager" ,defaultRollback= true )
@Transactional
public class iBatisDaoTest {
@Autowired
private ImageService imageService ;
private int id ;
@Test
public void testInsertImage() {}
}
9. Reference
SQL Maps2.0 개발자가이드 ( 한글판 )
http://ibatis.apache.org/docs/java/pdf/iBATIS-SqlMaps-2_ko.pdf
SQL Maps2.0 tutorial( 한글판 )
http://ibatis.apache.org/docs/java/pdf/iBATIS-SqlMaps-2-Tutorial_ko.pdf
iBATIS 에서 생성되는 SQL 문을 보기 위한 log4j 셋팅
http://openframework.or.kr/JSPWiki/Wiki.jsp?page=IbatisLog4jSettingToShowSQL
Sql2ibatis
http://openframework.or.kr/JSPWiki/Wiki.jsp?page=Sql2ibatis
DDL2iBatis
http://openframework.or.kr/JSPWiki/attach/Hibernate/DDL2iBatis-exe.zip
Spring 2.5, Eclipse and JUnit 4.4
http://dertompson.com/index.php/2007/12/12/spring-25-eclipse-and-junit-44/
iBATIS 인 액션: 쉽고 강력한 SQL 매핑 프레임워크 아이바티스
- 클린턴 비긴.브랜든 구딘.래리 메도스 지음 | 이동국.손권남 번역, 위키북스
스프링 2.5 프로그래밍
- 최범균 , 가메출판사
'나만의 작업 > iBatis' 카테고리의 다른 글
| [iBatis] iBatis에서 Log4j를 이용하여 쿼리를 로그로 남기려면? (4) | 2009/01/20 |
|---|---|
| [iBatis] 자동 생성 Key (2) | 2009/01/18 |
| [iBatis] 자바빈즈와 Map 타입의 결과 (0) | 2009/01/18 |
| [iBATIS] 8. Annotation 기반으로 JUnit4를 이용한 Spring TDD ~ 9. Reference (4) | 2008/09/03 |
| [iBATIS] 7. iBATIS + Spring + transaction (0) | 2008/09/02 |
| [iBATIS] 6. iBATIS + Spring (0) | 2008/09/01 |
| [iBATIS] 5. Transaction (2) | 2008/09/01 |
| [iBATIS] 4. How to (2) | 2008/08/29 |
| [iBATIS] 1.Overview ~ 3. Introduce iBATIS (4) | 2008/08/28 |
[iBATIS] 7. iBATIS + Spring + transaction
Posted at 2008/09/02 10:03// Posted in 나만의 작업/iBatis7. iBATIS + Spring + transaction
1. 코드 기반의 트랜잭션 처리 (Progrmmatic Transaction)
2. 선언적 트랜잭션 (Declarative Transaction)
- <tx:advice> 태그를 이용
- TransactionProxyFactoryBean 태그를 이용
- @Transactional 어노테이션을 이용
<tx:advice> 태그 이용
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
<aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
@ Transactional 어노테이션을 이용
@Transactional(readOnly = true)
public class DefaultFooService implements FooService {
public Foo getFoo(String fooName) {
// do something
}
// these settings have precedence for this method
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void updateFoo(Foo foo) {
// do something
}
}
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<tx:annotation-driven transaction-manager="txManager"/>
'나만의 작업 > iBatis' 카테고리의 다른 글
| [iBatis] iBatis에서 Log4j를 이용하여 쿼리를 로그로 남기려면? (4) | 2009/01/20 |
|---|---|
| [iBatis] 자동 생성 Key (2) | 2009/01/18 |
| [iBatis] 자바빈즈와 Map 타입의 결과 (0) | 2009/01/18 |
| [iBATIS] 8. Annotation 기반으로 JUnit4를 이용한 Spring TDD ~ 9. Reference (4) | 2008/09/03 |
| [iBATIS] 7. iBATIS + Spring + transaction (0) | 2008/09/02 |
| [iBATIS] 6. iBATIS + Spring (0) | 2008/09/01 |
| [iBATIS] 5. Transaction (2) | 2008/09/01 |
| [iBATIS] 4. How to (2) | 2008/08/29 |
| [iBATIS] 1.Overview ~ 3. Introduce iBATIS (4) | 2008/08/28 |
[dW] Acegi로 자바 애플리케이션 보안화 하기, Part 1: 아키텍처 개요와 보안 필터 (한글)
Posted at 2008/03/23 23:32// Posted in 나만의 작업/dW공부해 봐서 마침 dW에 글이 있나 찾아보니 역시나 있군요~
우리나라 말로하면 "아저씨"(지극히 저의 개인적인 생각)란 이름과 비슷한 아씨지(어떤분은 머 다른말로 표현하셨던데 어쩜 그게 더 비슷한거 같기도 하고)
아무튼!
What is Acegi Security?
Acegi Security is a powerful, flexible security solution for enterprise software, with a particular emphasis on applications that use Spring. Using Acegi Security provides your applications with comprehensive authentication, authorization, instance-based access control, channel security and human user detection capabilities.
머~ 이런 일을 한다고 합니다~로그인할때 해당 유저가 로그인할 권한이 있는지 Acegi 보안을 통해 접근할지 못하게 할지등등의 일을
처리할 수 있습니다.
Acegi로 자바 애플리케이션 보안화 하기, Part 1: 아키텍처 개요와 보안 필터 (한글)
이 문서에서의 내용은 Spring 프레임워크를 사용하여 IoC와 XML설정 파일을 이용해 보안처리를 하기위해 Acegi를 적용한 간단한 샘플 예제가 있습니다.
설정으로 보안과 관련해서 처리해야 할 일들을 줄일 수 있다면야~ 좋겠죠~?
'나만의 작업 > dW' 카테고리의 다른 글
| [dW] Practically Groovy: Reduce code noise with Groovy (0) | 2008/06/24 |
|---|---|
| [dW] Ajax에서 XML 처리하기 (2) | 2008/05/28 |
| [dW] Learn 10 good XML usage habits (2) | 2008/05/23 |
| [dW] XStream으로 자바 객체를 XML로 직렬화하기 (6) | 2008/05/22 |
| [dW] HTML 5와 XHTML 2에 관련된 글(dW문서와 그외) (2) | 2008/04/28 |
| [dW] Ajax 오버홀(overhaul), Part 1: Ajax와 jQuery로 기존 사이트 개선하기 (한글) (4) | 2008/04/20 |
| [dW] Acegi로 자바 애플리케이션 보안화 하기, Part 1: 아키텍처 개요와 보안 필터 (한글) (4) | 2008/03/23 |
| [dW] 클래스 로딩 문제 분석하기 (4) | 2008/03/04 |
| [dW] IBM developerWorks 리뷰 블로거 2.0 선발과 Mylyn 2.0 통합된 태스크 관리 & 자동화된 콘텍스트 관리 (8) | 2008/02/23 |
-
2008/03/25 12:42 [Edit/Del] [Reply]스프링을 무지 열심히 공부하나 봐요...
스프링에 관심이 도통 가지 않는 것을 보니
벽 보고 반성 좀 해야겠습니다.
갑자기 날씨가 추워졌어요 감기 조심해요. -
[Spring] 스프링에서 VelocityTools 환경설정
Posted at 2008/03/20 09:05// Posted in 나만의 작업/Spring필요한 설정을 기억하기 위해 기록합니다.
VelocityTools는 벨로시티 템플릿에서 숫자나 날짜, url등의 포맷팅을 지원하는 툴이고,
VelocityTools프로젝트에는 GenericTools, VelocityView, VelocityStruts 세개의 부분으로 나눠져 있습니다.
보통 GenericTools를 많이 쓸일이 많기 때문에~ 조금 정리해보면,
- DateTool
- A tool for manipulating and formatting dates.
- MathTool
- A tool for performing floating point math.
- NumberTool
- A tool for formatting numbers.
- RenderTool
- A tool to evaluate and render arbitrary strings of VTL (Velocity Template Language).
- EscapeTool
- A tool to help with common escaping needs in Velocity templates.
- ResourceTool
- A tool to simplify access to ResourceBundles for internationalization or other dynamic content needs.
- Alternator and AlternatorTool
- Utility class for easily alternating over values in a list and tool for easy creation of Alternators in templates.
- ValueParser
- A tool to retrieve and parse String values pulled from a map. This provides the basis for the ParameterParser in VelocityView.
- ListTool
- A tool to help when working with arrays or Lists. This tool transparently handles both the same way.
- SortTool
- A tool that allows a user to sort a collection (or array, iterator, etc) on any arbitary set of properties exposed by the objects contained within the collection.
- IteratorTool
- A convenience tool to use with #foreach loops. It wraps a list to let the designer specify a condition to terminate the loop, and reuse the same list in different loops.
EscapeTool
- It provides methods to escape outputs for Java, JavaScript, HTML, HTTP, XML and SQL. Also provides methods to render VTL characters that otherwise needs escaping.
을 이용하여 테스트 해보면...
toolbox.xml 파일을 하나 만들어서
<toolbox>
<tool>
<key>esc</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.EscapeTool</class>
</tool>
</toolbox>
genericTool의 EscapeTool을 사용하는데 velocity에서 esc라는 키워드를 이용해서 쓰겠다~
스프링에서 Properties파일을 따로 정의했다고 하면(따로 빼 놓았다면)
velocity.resourceLoaderPath=/WEB-INF/velocity/
velocity.toolboxConfigLocation=/WEB-INF/spring/toolbox.xml
velocity.properties=/WEB-INF/spring/velocity.properties
velocity.overrideLogging=false
velocity.cache=false
velocity.prefix=
velocity.suffix=.vm
스프링의 설정파일에서
<property name="cache" value="${velocity.cache}" />
<property name="prefix" value="${velocity.prefix}" />
<property name="suffix" value="${velocity.suffix}" />
<property name="toolboxConfigLocation" value="${velocity.toolboxConfigLocation}" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
.vm라는 확장자의 velocity파일에서
결과는
참고 :
[Velocity] 스프링 프레임워크에서 사용하기
http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/EscapeTool.htmlhttp://forum.springframework.org/archive/index.php/t-10926.html
'나만의 작업 > Spring' 카테고리의 다른 글
| [Spring] @Autowired의 Before/After (2) | 2009/04/29 |
|---|---|
| [Spring] 스프링에서 VelocityTools 환경설정 (2) | 2008/03/20 |
| [Spring] 스프링 MVC를 이용한 웹 요청 처리 (4) | 2008/03/13 |
| [Spring] Bean과 BeanFactory의 후처리 (8) | 2008/02/12 |
| [Spring] 자동 묶기(Autowire) (2) | 2008/02/12 |
| [Spring] 세터 주입(Setter Injection)의 대안 (5) | 2008/02/05 |
| [Spring] 빈 묶기(Bean wiring) (2) | 2008/02/05 |
| [Spring] 스프링 컨테이너의 두 종류 (0) | 2008/02/05 |
| [Spring] Spring 환경설정 (4) | 2008/01/28 |
[Spring] Bean과 BeanFactory의 후처리
Posted at 2008/02/12 13:44// Posted in 나만의 작업/Spring빈의 후처리
스 프링은 빈의 생명주기에 끼어들어 빈의 설정을 재검토하거나 바꿀 수 있는 2개의 기회를 제공한다. 후처리는 어떤 이벤트가 발생한 후에 처리되는 것이라고 추측할 수 있다. 이벤트란 빈이 설정되거나 인스턴스화되는 것을 말한다. BeanPostProcessor 인터페이스는 빈이 생성되거나 묶인 후에 변경할 수 있는 두개의 기회를 제공
Interface BeanPostProcessor에서는 두개의 메소드를 제공한다
postProcessBeforeInitialization : 빈이 초기화(afterPropertiesSet(), 빈의 커스텀 init-method 호출)되기 직전에 호출
postProcessAfterInitialization : 빈이 초기화된 직후에 호출
빈 팩토리의 후처리
BeanFactoryPostProcesor는 빈 팩토리가 빈의 정의를 로딩한 후와 빈이 인스턴스화되기 전에 빈 팩토리에 대해 후처리를 수행한다.
Interface BeanFactoryPostProcessor에서는 한 개의 메소드를 제공한다.
postProcessBeanFactory : 모든 빈의 정의가 로딩된 다음, BeanPostProcessor빈을 포함한 어떤 빈이라도 인스턴스화되기 이전에 스프링 컨테이너에 의해 호출된다.
BeanFactoryPostProcessor의 유용한 구현 클래스 두 개
PropertyPlaceholderConfigurer
CustomEditorConfigurer
'나만의 작업 > Spring' 카테고리의 다른 글
| [Spring] @Autowired의 Before/After (2) | 2009/04/29 |
|---|---|
| [Spring] 스프링에서 VelocityTools 환경설정 (2) | 2008/03/20 |
| [Spring] 스프링 MVC를 이용한 웹 요청 처리 (4) | 2008/03/13 |
| [Spring] Bean과 BeanFactory의 후처리 (8) | 2008/02/12 |
| [Spring] 자동 묶기(Autowire) (2) | 2008/02/12 |
| [Spring] 세터 주입(Setter Injection)의 대안 (5) | 2008/02/05 |
| [Spring] 빈 묶기(Bean wiring) (2) | 2008/02/05 |
| [Spring] 스프링 컨테이너의 두 종류 (0) | 2008/02/05 |
| [Spring] Spring 환경설정 (4) | 2008/01/28 |
[Spring] 자동 묶기(Autowire)
Posted at 2008/02/12 13:44// Posted in 나만의 작업/Spring자동 묶기(Autowire)
자동 묶기의 네 종류
byName : 묶고자 하는 특성의 이름과 동일한 이름이나 ID를 가진 빈을 컨테이너에서 찾는다. 빈을 찾지 못하면 그 특성을 묶이지 않은 채로 남는다.
byType : 묶 고자 하는 특성의 타입과 동일한 타입을 가진 빈을 컨테이너에서 찾는다. 빈을 찾지 못하면 그 특성은 묶이지 않은 채로 남고, 하나 이상의 빈을 찾게 되면 org.springframework.beans.factory.UnsatisfiedDependencyException을 던진다.
Constructor : 묶고자 하는 빈의 생성자 중 하나의 파라미터와 맞는 하나 이상의 빈을 컨테이너에서 찾는다. 모호한 빈이나 생성자가 발견될 경우 org.springframework.beans.factory.UnsatisfiedDependencyException을 던진다.
Autodetect : constructor에 의한 자동 묶기를 먼저 시도한 다음, byType을 이용한다. 모호함이 발견될 경우 constructor와 byType 경우와 동일한 방법으로 처리한다.
자동 묶기의 모호성 다루기
byType 이나 constructor를 사용하여 자동 묶기를 하는 경우에는 컨테이너가 특성의 타입이나 생성자 인자의 타입에 부합하는 둘 이상의 빈을 찾는 것이 가능하다.
이렇듯 자동 묶기에 사용될 수 있는 모호한 빈들이 존재 할 경우 스프링은 모호성을 분별할 능력이 없으며, 묶고자 하는 빈을 추측하는 대신 예외를 던지는 방법을 선택한다.
자동 묶기를 할 때 그 같은 모호성을 만나게 된다면, 자동 묶기를 하지 않는 것이 가장 단순하면서도 좋은 해결책이다.
'나만의 작업 > Spring' 카테고리의 다른 글
| [Spring] @Autowired의 Before/After (2) | 2009/04/29 |
|---|---|
| [Spring] 스프링에서 VelocityTools 환경설정 (2) | 2008/03/20 |
| [Spring] 스프링 MVC를 이용한 웹 요청 처리 (4) | 2008/03/13 |
| [Spring] Bean과 BeanFactory의 후처리 (8) | 2008/02/12 |
| [Spring] 자동 묶기(Autowire) (2) | 2008/02/12 |
| [Spring] 세터 주입(Setter Injection)의 대안 (5) | 2008/02/05 |
| [Spring] 빈 묶기(Bean wiring) (2) | 2008/02/05 |
| [Spring] 스프링 컨테이너의 두 종류 (0) | 2008/02/05 |
| [Spring] Spring 환경설정 (4) | 2008/01/28 |
[Spring] 세터 주입(Setter Injection)의 대안
Posted at 2008/02/05 14:28// Posted in 나만의 작업/Spring세터 주입(Setter Injection)의 대안 - 생성자 주입(Constructor Injection)
세터 주입은 빈 특성을 설정하고 묶을 수 있는 직관적인 방법이지만, 한 가지 단점은
변경될 수 있는 모든 특성이 세터 메소드를 통해서 사용할 수 있다고 가정하는 것에 있다. 하지만 빈이 이와 같은 방식으로 작동하기를 원하지 않을 때,
이런 유형의 빈이 인스턴스화될 때에는 어떤 특성도 설정될 수 없으며, 따라서 빈이 유효하지 않은 상태로 있을 가능성이 있다.
어떤 특성들은 빈이 생성될 때 한 번만 설정되고 그 이후에는 변경될 수 없도록 만들고 싶은 경우도 있다.
이는 세터를 통해 모든 특성을 공개하는 경우에는 곤란해진다.
대안은 일부 특성들은 생성자 를 통해 설정될 수 있도록 빈을 설계하는 것이다.
DAO의 DataSource와 같이 반드시 설정돼야 하고 변경되어서는 안 되는 특성이 있는 경우에 특히 유용
생성자 주입은, 어떤 특성을 설정하지 않고서는 인스턴스를 만들 수 없을 때,
'나만의 작업 > Spring' 카테고리의 다른 글
| [Spring] 스프링에서 VelocityTools 환경설정 (2) | 2008/03/20 |
|---|---|
| [Spring] 스프링 MVC를 이용한 웹 요청 처리 (4) | 2008/03/13 |
| [Spring] Bean과 BeanFactory의 후처리 (8) | 2008/02/12 |
| [Spring] 자동 묶기(Autowire) (2) | 2008/02/12 |
| [Spring] 세터 주입(Setter Injection)의 대안 (5) | 2008/02/05 |
| [Spring] 빈 묶기(Bean wiring) (2) | 2008/02/05 |
| [Spring] 스프링 컨테이너의 두 종류 (0) | 2008/02/05 |
| [Spring] Spring 환경설정 (4) | 2008/01/28 |
| [Spring] Hitting the database (8) | 2007/11/04 |
[Spring] 빈 묶기(Bean wiring)
Posted at 2008/02/05 14:22// Posted in 나만의 작업/Spring빈 묶기(Bean wiring)
XML로 묶기 : 다음을 이용해 스프링 컨테이너가 XML을 통한 빈 묶기를 지원한다.
XmlBeanFactory
ClassPathXmlApplicaionContext
FileSystemXmlApplicationContext
XmlWebApplicationContext
프로토타입과 싱글톤 비교
스프링의 모든 빈은 싱글톤. getBean()의 호출에 의해서든 묶기를 통해서든 간에, 컨테이너가 빈을 배포할 때에는 항상 그 빈의 완전히 동일한 인스턴스를 내줄 것이다.
scope="singleton"
scope="prototype"
프로토타입 빈을 정의하는 것이 유리할 때
프로토타입을 정의한다는 것은 실제 하나의 빈을 정의하는 것이 아닌, 청사진을 정의한다는 의미다. 그 다음엔 그 청사진을 바탕으로 빈들이 생성될 것이다.
프로토타입 빈은, 빈을 요청할 때마다 컨테이너가 빈의 유일한 인스턴스를 제공하기 원하면서도 그 빈의 하나 이상의 특성을 스프링을 통해 설정하고자 할 경우에 유용
프로토타입으로 정의
빈 의 이름을 사용해 getBean()을 호출할 때마다 프로토타입의 새로운 인스턴스가 생성된다. 이로 인해 데이터베이스나 네트워크 연결과 같은 제한된 자원을 사용하는 빈의 경우에는 적합하지 않다. 최소한 새로운 인스턴스가 생성될 때마다 약간의 성능 저하를 겪을 수 있기 때문에, 절대적으로 필요한 경우에만 프로토타입으로 빈을 정의하자.
dW문서 'Spring 2.0: 요란스럽지 않은 진화의 흐름' 내용 중
Spring의 기반을 이루는 IoC 컨테이너에서는 앞서 언급한 XML 설정 이외에 빈(bean)의 스코프(scope)가 추가된 것이 주요 개선 사항이다.
Singleton과 prototype으로만 존재하던 스코프에 request, session 및 global session 스코프가 추가되었고, 사용자가 정의한 스코프를 사용할 수도 있다. 이러한 개선은 개발자가 직접 HTTP Request나 Session을 다루는 일을 줄여주고, 웹 어플리케이션의 커뮤니케이션 불연속성 문제를 다소나마 해결해줄 것으로 보인다.
| Scope | Description |
|---|---|
|
Scopes a single bean definition to a single object instance per Spring IoC container. | |
|
Scopes a single bean definition to any number of object instances. | |
|
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext. | |
|
Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext. | |
|
Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext. |
출처 : spring 2.0 reference 중
초기화와 소멸
빈의 정이 내에 커스텀 init-method를 선언함으로써 빈이 인스턴스화되는 즉시 호출될 메소드를 지정할 수 있다.
커스텀 destroy -method는 빈이 컨테이너로부터 제거되기 직전에 호출될 메소드를 지정한다.
Example) 커넥션 풀링(connection pooling) 빈
public class MyConnectionPool{
public void initialize(){
//커넥션 풀 초기화
}
}
public void close(){
//연결 해제
}
}
빈의 정의는,
MyConnectionPool이 인스턴스화되자마자 initialize() 메소드가 호출,
빈이 컨테이너로부터 제거되기 직전에 close() 메소드를 호출
스프링은 또 다른 방법으로, InitializingBean과 DisposableBean이라는 동일한 역할을 하는 두개의 인터페이스를 제공한다.
InitializingBean 인터페이스는 afterPropertiesSet() 메소드 : 빈의 모든 특성이 설정된 후에 한 번 호출.
DisposableBean 인터페이스는 destroy () : 빈이 컨테이너로부터 제거될 때 호출
이 방법은 인터페이스를 구현하는 빈을 스프링 컨테이너가 자동으로 탐지하며, 별다른 설정없이도 메소드를 호출해준다(XML 손대지 않고) 하지만, 인터페이스를 구현하게 되면, 빈은 스프링의 API에 묶이게 되어 버린다.
빈을 초기화하고 소멸시킬 때 가능하다면 init-method와 destroy-method 정의에 의존하는게 좋은 방법이다. 스프링의 인터페이스를 사용할만한 유일한 경우는 스프링 컨테이너 안에서 특별하게 사용될 프레임워크 빈을 개발할 때뿐이다.
'나만의 작업 > Spring' 카테고리의 다른 글
| [Spring] 스프링에서 VelocityTools 환경설정 (2) | 2008/03/20 |
|---|---|
| [Spring] 스프링 MVC를 이용한 웹 요청 처리 (4) | 2008/03/13 |
| [Spring] Bean과 BeanFactory의 후처리 (8) | 2008/02/12 |
| [Spring] 자동 묶기(Autowire) (2) | 2008/02/12 |
| [Spring] 세터 주입(Setter Injection)의 대안 (5) | 2008/02/05 |
| [Spring] 빈 묶기(Bean wiring) (2) | 2008/02/05 |
| [Spring] 스프링 컨테이너의 두 종류 (0) | 2008/02/05 |
| [Spring] Spring 환경설정 (4) | 2008/01/28 |
| [Spring] Hitting the database (8) | 2007/11/04 |
-
iolo2008/02/05 19:29 [Edit/Del] [Reply]singleton="false"라고 쓰는 방식은 deprecated임다.
scope="prototype"이라고 쓰는 방식으로 바뀌었습니당~
[Spring] 스프링 컨테이너의 두 종류
Posted at 2008/02/05 14:09// Posted in 나만의 작업/Spring스프링 컨테이너의 두 종류
컨 테이너는 스프링 프레임워크의 핵심이다. 스프링 컨테이너는 제어 역행(IoC)을 사용해 애플리케이션을 구성하는 컴포넌트들을 관리한다. 여기에는 협력하는 컴포넌트간의 연관관계를 생성하는 것이 포함된다. 그렇게 함으로써 객체는 좀더 명확하게 이해할 수 있고, 재사용이 가능해지며, 단위테스트가 쉬워진다.
스프링 컨테이너의 두 종류
org .springframework.beans.factory.BeanFactory 인터페이스로 정의되는 빈 팩토리
기본적인 의존성 주입을 지원하는 가장 간단한 형태의 컨테이너
org .springframework.context.ApplicationContext 인터페이스로 정의되는 어플리케이션 컨텍스트.
프로퍼티 파일의 텍스트 메시지를 해석하는 능력이나 애플리케이션 이벤트를 관련된 이벤트 리스너에 발행하는 능력 등과 같은 애플리케이션 프레임워크 서비스를 제공함으로써 빈팩토리의 개념 위에 구현된 것.
빈 팩토리(BeanFactory)
팩토리 디자인 패턴을 구현한 것. 빈 팩토리는 빈을 생성하고 분배하는 책임을 지는 클래스
커스텀 초기화 메소드(initialization method)와 소멸 메소드(destruction method)를 호출함으로써 빈의 생명주기에 개입할 수 있다.
org .springframework.beans.factory.xml.XmlBeanFactory 가 가장 유용한데, XML 파일에 기술돼있는 정의를 바탕으로 빈을 로딩한다.
BeanFactory factory = new XmlBeanFactory(new FileInputStream( “beans.xml ”));
MyBean myBean = (MyBean) factory.getBean( “myBean ”);
빈 팩토리에게 XML 파일로부터 빈에 대한 정의를 읽어오라고 알려준다 . 빈 팩토리가 빈 을인스턴스화하는 것은 아니고 빈은 빈 팩토리에 “늦게 ”로딩되는데, 이 말은 빈 팩토리가 빈의 정의는 즉시 로딩하는 반면, 빈 자체가 필요하게 되기 전까지는 인스턴스화 하지 않는다.
getBean()이 호출되면, 팩토리는 의존성 주입을 이용해 빈을 인스턴스화하고 빈의 특성을 설정 시작. 여기서 빈의 일생이 시작된다.
애플리케이션 컨텍스트
BeanFactory와 유사한 기능을 제공하지만 좀 더 많은 기능을 제공하는 ApplicationCont ext
국제화(I18N) 지원을 포함해 텍스트 메시지를 해석하는 수단을 제공
이미지 등과 같은 자원을 로딩하는 범용적인 방법을 제공
리스너로 등록돼있는 빈에 이벤트를 발행할 수 있다.
구현 클래스 중 일반적으로 사용되는 세 개 의 클래스
ClassPathXmlApplicationContext : 클래스 경로에 있는 XML 파일로부터 컨텍스트 정의를 로딩하며, 컨텍스트 정의를 클래스 경로에 있는 자원으로 취급한다.
FileSystemXmlApplicationContext : 파일 시스템에 있는 XML 파일로부터 컨텍스트 정의를 로딩.
XmlWebApplicationContext : 웹 애플리케이션에 포함돼 있는 XML 파일로부터 컨텍스트 정의를 로 딩
애플리케이션 컨텍스트와 빈 팩토리의 또 다른 차이점 은 싱글톤(singleton) 빈을 로딩하는 방법에 있다. 빈 팩토리는 getBean() 메소드가 호출될 때까지 빈의 생성을 미룬다. 즉 빈 팩토리는 모든 빈을 lazy loading한다.
'나만의 작업 > Spring' 카테고리의 다른 글
| [Spring] 스프링에서 VelocityTools 환경설정 (2) | 2008/03/20 |
|---|---|
| [Spring] 스프링 MVC를 이용한 웹 요청 처리 (4) | 2008/03/13 |
| [Spring] Bean과 BeanFactory의 후처리 (8) | 2008/02/12 |
| [Spring] 자동 묶기(Autowire) (2) | 2008/02/12 |
| [Spring] 세터 주입(Setter Injection)의 대안 (5) | 2008/02/05 |
| [Spring] 빈 묶기(Bean wiring) (2) | 2008/02/05 |
| [Spring] 스프링 컨테이너의 두 종류 (0) | 2008/02/05 |
| [Spring] Spring 환경설정 (4) | 2008/01/28 |
| [Spring] Hitting the database (8) | 2007/11/04 |


