JSP에서 aspectj의 사용에 관해

186 views
Skip to first unread message

windykc

unread,
Jan 30, 2012, 12:28:10 AM1/30/12
to Korea Spring User Group
안녕하세요. 그룹에 처음 가입하고 드리는 글이 질문이라서 죄송합니다.

이 질문의 Spring Framework과의 직접적인 관련성에 대해 약간의 의문이 들지만.....

제가 지금 하고자 하는 것은 AOP를 이용해 HttpServletRequest의 getParamter 류의 함수 호출 전후에
process를 추가하려고 합니다.

SpringAOP를 사용하여 Controller상의 getParameter는 적용하였으나 JSP에서 사용하는
getParameter 함수에 적용을 못하고 있습니다.

JSP는 Spring에서 관리하는 빈이 아니기에 aspectj를 사용하여 적용하기 위해 LTW를 사용하였으나 적용이 안됩니다.

aop.xml 은

<!DOCTYPE aspectj PUBLIC
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/
aspectj.dtd">
<aspectj>
<weaver options="-verbose -debug -showWeaveInfo -
Xset:weaveJavaxPackages=true">
<exclude within="org.springframework..*"/>
</weaver>
<aspects>
<aspect name="com.test.aspectj.ServletMonitor"/>
<aspect name="com.test.aspectj.LoggingAspect"/>
</aspects>
</aspectj>

와 같이 사용하였고

aspector는

public aspect LoggingAspect {
public pointcut allcall() : call(* *.getParameter(..)) && !
within(LoggingAspect);

Object around() : allcall() {
Signature sig = thisJoinPointStaticPart.getSignature();
System.out.println("Start2 [" + sig.getDeclaringType().getName() +
"."
+ sig.getName() + "]");

Object ret = proceed();

System.out.println("End2 [" + sig.getDeclaringType().getName() + "."
+ sig.getName() + "]");
return (ret instanceof String) ? ((String) ret).toUpperCase() : ret;
}

}

Controller상에서는

@Controller
public class HomeController {
private static final Logger logger =
LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model, HttpServletRequest
request) {
String param = request.getParameter("param");
System.out.println(param);
Date date = new Date();
DateFormat dateFormat =
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG,
locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
와 같이 테스트 코드를 사용하여 적용이 되었습니다.

하지만

아래와 같은 JSP코드에서는 적용이 되지 않습니다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=request.getParameter("param") %>
</body>
</html>

톰캣의 web.xml에서 JSP 컴파일러를 변경해보았습니다.

<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-
class>
<init-param>
<param-name>compiler</param-name>
<param-
value>org.aspectj.tools.ant.taskdefs.Ajc11CompilerAdapter</param-
value>
</init-param>

하지만 역시 적용이 안되었습니다.

AspectJ FAQ에서 JSP 에 관해서 아래와 같이 한줄 나오고 더 이상의 설명은 없었습니다.

JSP: It is possible to use AspectJ to affect code in JSPs by
precompiling them into Java sources and compiling these with ajc. This
can be used, e.g., to customize displays by turning on and off custom
JSP taglibs. The mapping from a given jsp source to java package and
class name is not standardized, which means doing this imposes
dependencies on specific container versions.

질문은 precompile 하지 않고 JSP에 aspectj를 적용할수 있는 방법은 없는가 하는 것입니다.

읽어주셔서 감사합니다.



windykc

unread,
Jan 30, 2012, 6:51:34 AM1/30/12
to Korea Spring User Group
원인을 찾지 못해 헤매이다가 찾았습니다.

제가 LTW에 대한 이해가 부족했나봅니다.

VM 옵션에 -javaagent:/path/to/aspectjweaver.jar 를 추가하니 제대로 동작합니다.

그룹스를 어지럽힌 점 죄송합니다.
Reply all
Reply to author
Forward
0 new messages