본문 바로가기

Spring

스프링 First class 2010 .07 .03 (토)

<< 수업내용 정리 >> 

http://www.springsource.com/

 // 기본 반복되는 처리 : Logging  Transaction  Security

 Logging Transaction 처리 계속 반복 되나  è AOP 형태로 처리 가능

 LoggingAdvice  , TransactionAdvice , SecurityAdvice

Container 가 설정파일에서 서로의 연결관계를 담당

 

객체끼리 의존 관계 – 어떤객체를 어떤 객체에 넣고 하는 부분을 설정파일에서 처리하겠다.

 *JEE  Email 기능

 

D:\dev_Spring\spring-framework-2.5.4\dist\ spring.jar  기본구성  // MVC Frame work 불포함.

작업하면서 필요한 기능 추가 D:\dev_Spring\spring-framework-2.5.4\dist\modules

 쓰는 기능 자체의 외부 라이브러리   : D:\dev_Spring\spring-framework-2.5.4\lib  폴더형태로 구분되어 있음.

 제어의 역행 : 직접 객체를 생성하지 않고 설정파일에서 객체를 생성 담당 ( Container Bean 관리 담당 )

 공통관심사항 Cross Concern :  Logging,  Transaction,  Security  를 이름.

 

이 클래스에 이 클래스 주입하고 설정 -- > 직접객체를 생성할 필요가 없음 (Container 가 직접 해줌)

tight coupling --- -----> loose coupling

Spring 은 통합프레임워크다 ( 외부 에서 제공되는 모든 기능, 라이브러리등을 모두 붙여서 사용할 수 있다. )

 

 

Constructor Injection

 

<constructor-arg>

<value> hello </value>

</constructor-arg>

 

<bean id="helloBean" class="test3.HelloBeanBoy">

            <constructor-arg>

                  <value> arg-boy by Constructor</value>

            </constructor-arg>

            <constructor-arg>

                  <ref bean = "helloBoy"></ref>

            </constructor-arg>           

      </bean>

 

동일하며 아래와 같은 형태로 구성가능

 

      <bean id="helloBean" class="test3.HelloBeanBoy">

            <constructor-arg type="java.lang.String" value="boy"></constructor-arg>

            <constructor-arg type="test3.Boy" ref="helloBoy"></constructor-arg>

      </bean>

 

      <bean id="helloBoy" class="test3.Boy">

      <constructor-arg type="java.lang.String" value="aaa"></constructor-arg>

</bean>

 

 

Setter Injection

 

 

 

AOP Aspect Oriented Programming 의 용어정리

Joinpoint

     메소드 호출, 객체 생성 등 공통관심 사항을 적용할 수 있는 특정 시점.

Advice  ( 구체적인 )

     공통관심 사항을 특정 시점에 적용하는 코드( 로깅, 보안 등). – 집어 넣을 코드

Pointcut  ( 패턴으로 지정하는 것을 의미 )

     공통관심 사항을 적용할 JoinPoint 를 여러 개 지정한 것.

Advisor  ( pointcut   Advice 를 연결 )

     Pointcut Advice를 하나의 실행단위로 묶은 것.

Weaving 

     공통관심 사항을 비즈니스 로직에 적용 하는 것.

Target

     핵심 로직을 구현한 클래스. Advice 가 적용될 클래스.

Aspect ( 개념적인 )

     여러 모듈에 공통으로 적용될 공통 관심사항.

 

Advice 만들기

org.springframework.aop.MethodBeforeAdvice

     객체의 메소드 실행전 공통관심사항 실행.

org.springframework.aop.AfterReturningAdvice

     객체의 메소드 실행 후 공통관심사항 실행.

org.springframework.aop.ThrowsAdvice

     객체의 메소드 실행 시 예외를 발생했을 경우 공통관심사항 실행.

org.aopalliance.intercept.MethodInterceptor

     위의 세가지 advice 기능을 하나로 묶은 advice

 

 

 

예제 시작 Target 생성

 

<bean id="targetBean" class="test2.HelloBeanBoy">

</bean>

 

package test2;

 

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

public class LogingTest implements MethodInterceptor {

  public Object invoke(MethodInvocation invocation) throws Throwable{

  String methodName = invocation.getMethod().getName();

  System.out.println(methodName + "실행전");

  Object obj =  invocation.proceed();

  System.out.println(methodName + "실행후");

  return obj;

  }

}

<bean id="logingAdvice" class="test2.LogingTest"/>

Ø  Pointcut

     Pointcut 설정은 org.springframework.aop.support.JdkRegexpMethodPointcut

     java 1.4 버전 이후 사용.

 

     org.springframework.aop.support.Perl5RegexpMethodPointcut

     java 1.3 버전 이전 사용.

 

     org.springframework.aop.aspectj.AspectJExpressionPointcut

     AspectJ의 표현식 이용.

 

<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">

<property name="pattern">

<value>.*printHello.*</value>

</property>

</bean>

 

 

스프링에서는 "메소드" 를 호출할 때 의 Aop   제공

AspectJ 는 다양하게 주입 제공

 

 

ProxyFactoryBean 을 이용하여 Advice 적용하기

 

 

스프링의 메시지 국제화 지원

     org.springframework.context.MessageSource

 

지원 메소드

     String getMessage(String code,Object[] args,Locale locale)

     throws NoSuchMessageException;

     

     String getMessage(String code,Object[] args,String defaultMessage,Locale locale);

 

     String getMessage(MessageSourceResolvable resolvable,Locale locale)

     throws NoSuchMessageException;

 

ApplicationContext MessageSource 인터페이스를 상속 받고 있으므로 알맞은 메시지를 가져올 수 있다.

 

 

 

<bean id=messageSource class=org.springframework.context.support.ResourceBundleMessageSource>

                   <propery name=basename>

            <value>message.hello</value>

                   </property>

</bean>        

 

리소스 파일을 여러 개 설정할 경우

        <property name=basenames>

                   <list>

                   <value>.</value>

            <value>.</value>

                   </list>

        </property>

 

 

JDK 자체에서 지원하는 기능   D:\>native2ascii hello_kr.properties hello_ko_properties

Locale Resource 를 찾을 때   hello_ko_KR.properties 의 정확한 이름이 필요함.   hello_ko 에서 hello_ko_KR 로 변경후 정상실행.

 

 

AbstractController  아무 기능도 없음.

 

실행되면서 HandleRequestInternal 자동 호출 됨

 

protected ModelAndView handleRequestInternal(HttpServletRequest arg

 

mav.addObject("greeting", getGreeting());

return mav;

 

${greeting}  -- jsp 에서 표출 해당 값.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'Spring' 카테고리의 다른 글

Spring cglib  (0) 2011.09.12
Spring fw AOP and Paypal soap  (0) 2011.09.04
자바 - Log4j 사용법 ( 설명 자세히 잘 되어 있음 )  (0) 2011.08.26
Java 연습 2008-1 Banking  (0) 2011.08.24
Spring 과 iBatis 연동  (0) 2011.08.23