0

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

Community
  • 1
  • 1
hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141

1 Answers1

0

You need to enable annotation configuration, either with

<context:annotation-config />

or

<context:component-scan ... />

With the above and assuming you're operating on a bean, the @Autowired field can never be null. If Spring can't inject it, it will throw exceptions.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks for all the answers @Sotirios! I have added the details. I have context: component-scan, but there are some exclude filter tags in it. I dont really understand them well. – hrishikeshp19 Apr 08 '14 at 22:08
  • @ris The exclude filters won't affect this situation. Are you saying it is still null? Do you have two context files? One for the dispatcher servlet and one for the contextLoaderListener? – Sotirios Delimanolis Apr 08 '14 at 22:42
  • It is still null. I already had component-scan in place. Yes I have two context files one for dispatcher servlet and other for contextLoaderListener – hrishikeshp19 Apr 08 '14 at 22:53
  • Hi Sotitios, sorry for delay, I have updated the post with web.xml – hrishikeshp19 Apr 10 '14 at 17:59
  • @riship89 Ok, let's just be complete. I don't want to guess lol. Post the full `applicationContext.xml` and the full servlet context configuration. Does your `@Aspect` also have any `@Component` annotation? – Sotirios Delimanolis Apr 10 '14 at 18:04
  • Sotirios, I need to strip those files down a lot before uploading. My aspect does not have any @Component annotation. – hrishikeshp19 Apr 10 '14 at 18:16
  • @riship89 Ok wait. It's not possible for an injection target, ie. an `@Autowired` field to be `null` if you have the elements in my answer above. Does your servlet context by any chance also declare a `` for the aspect? If so, does it have the `` or `` elements? – Sotirios Delimanolis Apr 10 '14 at 18:17
  • my servlet context does not have a bean for that aspect, but it has element. My application context has bean for my aspect (as mentioned in the original question). my application context also has – hrishikeshp19 Apr 10 '14 at 18:33
  • @riship89 I seriously don't see how it could be `null`. Can you confirm that the injected `internationalizationService` is not `null`? – Sotirios Delimanolis Apr 10 '14 at 18:36
  • internationalizationService is null as well. – hrishikeshp19 Apr 10 '14 at 19:01
  • @riship89 Seems like your `component-scan` is messing with it. Refactor your packages and remove the `exclude-filter` elements. – Sotirios Delimanolis Apr 10 '14 at 19:03
  • Are you kidding me? Have a look at this: http://stackoverflow.com/a/19327318/929701. That worked. – hrishikeshp19 Apr 10 '14 at 19:19
  • @riship89 That's a workaround though. The injection should work too, not necessarily for aspects. If you have the patience, you should look into your `component-scan` situation. If you want to debug, `AutowiredAnnotationBeanPostProcessor` is what handles injecting beans into `@Autowired` fields. – Sotirios Delimanolis Apr 10 '14 at 19:22
  • yes, I will spend some time debugging my annotations. Thanks you so much for helping me out here. – hrishikeshp19 Apr 10 '14 at 19:25
  • Hi Sotirios, please see this: http://stackoverflow.com/a/9658819/929701. This worked as well. Do you know what does factory method aspectOf do? – hrishikeshp19 Apr 10 '14 at 19:39
  • @riship89 That's a bunch of AspectJ configuration elements. Are you using AspectJ? That's very possibly involved. I'd have to see more of your context configuration. – Sotirios Delimanolis Apr 10 '14 at 21:06