JAVA CAFE

Annotation(@Aspect, @Component)을 이용해 AOP를 적용시켜본 예제임.

SpringFramework에서 제공하는 Annotation(@Aspect, @Component)을 이용하기 위해 XML에 기술

<context:component-scan base-package="com.test.aop" />

AOP에선 AOP가 적용될 클래스 및 메소드의 패턴이 표현되는 Pointcut이 가장 중요하다고 함.

spring-test.jar를 이용해 테스트를 해보면 성공!

예외 정리


1. @ContextConfiguration에서 Locations속성을 설정하지 않으면 같은 패키지에 GreetingTest-context.xml을 디폴트값으로 참조함. 없을 경우 IO예외가 발생함.(FileNotFoundException)

2. @ContextConfiguration에서 정의한 GreetingTest-context.xml의 classpath 경로가 틀리거나 다른 이름일 경우(예-Beans.xml) java.lang.NoSuchMethodError: org.junit.internal.runners.MethodValidator.<init>(Lorg/junit/internal/runners/TestClass;) 발생함.

3. Eclipse3.3에 포함되어있는 JUnit의 경우 버전이 4.1이고 이를 사용하면 "@RunWith(SpringJUnit4ClassRunner.class)"부분에서 컴파일 에러가 발생함.
에러 내용 : Type mismatch: cannot convert from Classto Class

4.
Pointcut이 적용이 안될 경우
- AspectJ문법이 틀렸거나, JDK버전문제(이 부분이 조금 애매하긴 함. 구글링중 JDK6이 문제를 일으킨다는 글이 많았는데 결국 JDK6에서도 테스트 성공!)
@Before("greetingInterceptor()"), @AfterReturning("greetingInterceptor()") 부분을
@Before("execution(* * ..Greeting.*(..))"),
@AfterReturning("execution(* * ..Greeting.*(..))")
로 수정 후 테스트 시도.
에러 내용 : Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut greeting

5. @Autowired 이용시 Interface 타입을 이용.
구현 클래스를 타입으로 이용했을 경우 에러 내용
Unsatisfied dependency of type [class com.test.aop.GreetingImpl]: expected at least 1 matching bean

소스 다운 받기 및 설치.
http://kekedie.tistory.com/68


다운로드 받을 파일을 압축을 풀고 Eclipse의 workspace에 넣은 후 메뉴탭에서
File > Import > General > Existing Projects into Workspace 하면 됨.





 

Beans(POJOs)를 XML에 기술하는데신 Spring Framework 2.x 에서 제공하는 Annotation을 이용.

솔직히 코딩하는데 있어서는 Annotation이 편할 것 같은데 XML에 기술하는게 나중에 관리면에서 좋을 것 같다는 생각이 조금 든다.


/**
* 출처 - http://jjaeko.tistory.com에 원문을 참고해서 작성함.
* 이클립스에서 소스 작성시 발생할 수 있는 예외 정리
* Springframework2.5 이용시 발생할 수 있는 예외 정리
*/
package com.test;

//
AbstractDependencyInjectionSpringContextTests를 확장하지 않을 경우 아래 두줄
// 주석 해제 필요.

// import static org.junit.Assert.assertNotNull;
// import static org.junit.Assert.assertEquals;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/com/test/Beans.xml" })
public class ScanTest extends AbstractDependencyInjectionSpringContextTests {
/**
* Logger for this class
*/
private static final Log logger = LogFactory.getLog(ScanTest.class);

@Autowired
private Foo foo;

/**
*
junit으로 메소드를 테스트 할 경우 test메소드명이지만 @Test annotation을 붙이
* 면 test메소드가 됨.

*/
@Test
public void fooTest() {
if (logger.isDebugEnabled()) {
logger.debug("fooTest() - start");
}

assertNotNull(foo.getBar());
assertEquals("Hello World", foo.greeting());

if (logger.isDebugEnabled()) {
logger.debug("fooTest() - end");
}
}
}

예외 정리

1. @ContextConfiguration에서 Locations속성을 설정하지 않으면 같은 패키지에 ScanTest-context.xml을 디폴트값으로 참조함. 없을 경우 IO예외가 발생함.

2. @ContextConfiguration에서 정의한 ScanTest-context.xml의 classpath 경로가 틀리거나 다른 이름일 경우(예-Beans.xml) java.lang.NoSuchMethodError: org.junit.internal.runners.MethodValidator.<init>(Lorg/junit/internal/runners/TestClass;) 발생함.

3. Eclipse3.3에 포함되어있는 JUnit의 경우 버전이 4.1이고 이를 사용하면 "@RunWith(SpringJUnit4ClassRunner.class)"부분에서 컴파일 에러가 발생함.
에러 내용 : Type mismatch: cannot convert from Class<SpringJUnit4ClassRunner> to Class<? extends Runner>



출처 : http://kekedie.tistory.com

 

빌드 패스의 spring.jar 마우스 우클릭해서 Properties(Alt + Enter) 클릭.

Javadoc Location 메뉴의 Javadoc URL을 선택후 Javadoc location path에 api 경로를 입력.


API Document를 보고 싶은 메소드를 클릭후 F2를 누름.



출처 : http://kekedie.tistory.com

 

Spring Framework 이용해 2개 이상의 웹 프로젝트를 배포할 경우 Log4jConfigListener에 대한 예외가 발생할 수 있다.
(org.springframework.web.util.Log4jConfigListener를 이용해 log4j.properties를 로딩할 경우)


Log4jConfigListener
컨텍스트 초기화 예외 발생

예외 내용:

java.lang.IllegalStateException
Web app root system property already set to different value:
- Choose unique values for the 'webAppRootKey' context-param in your web.xml files!


해결 방법:
컨텍스트 파라미터webAppRootKey의 값을 설정해준다. webAppRootKey의 디폴트 값은 webapp.root임
예)<context-param>
<param-name>webAppRootKey</param-name>
<param-value>myProject_name.root</param-value>
</context-param>


출처 : http://kekedie.tistory.com

 



공부해야되겠네...