httprequest未在方面自动装配

时间:2014-04-08 21:02:00

标签: spring servlets aspectj

我正在使用以下方面拦截对某个类的getDescription方法的调用。

@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);
            }
        }
    }
}

对于上述方面,我得到以下输出:

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

在我的applicationContext.xml中,我有:

<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>

为什么Spring在我的方面没有自动装配httpservletrequest。根据这个答案:Spring AOP and aspect thread safety for an autowired HTTPServletRequest bean

它应该有用。

修改

我的web.xml类似于以下内容:

<?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>

我在applicationcontext.xml中定义了我的方面bean,它不在spring-mvc-servlet.xml中

1 个答案:

答案 0 :(得分:0)

您需要使用

启用注释配置
<context:annotation-config />

<context:component-scan ... />

通过上述内容并假设您正在操作bean,@Autowired字段永远不会是null。如果Spring无法注入它,它将抛出异常。