I am intercepting calls to getDescription method of a certain class using following aspect.
@Aspect
public class InternationalizationAspect {
private static final String LANGUAGE_CODE_ENGLISH = "en";
private static Logger log = LoggerFactory.getLogger(InternationalizationAspect.class);
@Autowired
InternationalizationService internationalizationService;
@Autowired
private HttpServletRequest httpServletRequest;
@Around("execution(* com.***.***.***.***.getDescription(..))")
public Object getInternationalizedTitleDescription(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("title getdescription intercepted");
if (httpServletRequest == null) {
log.info("http servlet request was not autowired correctly");
return joinPoint.proceed();
} else {
Locale locale = httpServletRequest.getLocale();
log.info("locale is = " + locale);
if (locale.getLanguage().equals(new Locale(LANGUAGE_CODE_ENGLISH).getLanguage())) {
return joinPoint.proceed();
} else {
log.info("getting internationalized description");
Title t = (Title) joinPoint.getTarget();
return internationalizationService.getTitleDescriptionFromTitleAndLocale(t, locale);
}
}
}
}
For the above aspect, I get following output:
title getdescription intercepted
http servlet request was not autowired correctly
title getdescription intercepted
http servlet request was not autowired correctly
title getdescription intercepted
http servlet request was not autowired correctly
title getdescription intercepted
http servlet request was not autowired correctly
In my applicationContext.xml, I have:
<bean id="internationalizationAspect" class="com.***.***.***.InternationalizationAspect" />
<context:component-scan base-package="com.***">
<context:exclude-filter expression=".*_Roo_.*" type="regex"/>
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
Why is spring not autowiring httpservletrequest in my aspect. According to this answer: Spring AOP and aspect thread safety for an autowired HTTPServletRequest bean
it should work.
EDIT
My web.xml resembles following:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Spring Web Application example</display-name>
<!-- Configurations for the root application context (parent context) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/META-INF/spring/applicationContext.xml
</param-value>
</context-param>
<!-- Configurations for the DispatcherServlet application context (child context) -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/mvc/spring-mvc-servlet.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/admin/*</url-pattern>
</servlet-mapping>
</web-app>
I am defining my aspect bean in applicationcontext.xml which is not in spring-mvc-servlet.xml