当前位置: 首页 > 知识库问答 >
问题:

httprequest在aspect中未自动连接

公西宏毅
2023-03-14

我正在使用以下方面拦截对某个类的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

在我的应用程序上下文中。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。根据这个答案:自动连线HttpServletRequestbean的Spring AOP和aspect线程安全

它应该工作。

编辑

我的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中定义我的方面bean。不在spring mvc servlet中的xml。xml

共有1个答案

谷翰飞
2023-03-14

您需要启用注释配置,可以使用

<context:annotation-config />

<context:component-scan ... />

根据以上内容,并假设您在bean上操作,Autowired字段永远不能为null。如果Spring不能注入它,它将抛出异常。

 类似资料:
  • 在Spring Boot应用程序中,我尝试设置多个数据库连接。我已经开始构建主数据源,但是在mySqlEntityManagerFactory方法上出现以下错误。 无法自动连线。没有EntityManagerFactoryBuilder的bean 如何自动连接EntityManagerFactoryBuilder? 我正试图遵循这个博客上的代码https://raymondhlee.wordpre

  • 出于某种原因,我使用外部属性源,其中一个外部属性源没有自动连接,在创建身份验证bean时接收空指针 原因: org.springframework.beans.BeanInstantiationException: Failed to instanceiate [com.filechecker.check.Authenticator]: Constructor threw exception;ne

  • 代码如下: 我从文件中了解到: 如果您使用的是@SpringBootTest注释,则TestRestTemplate是自动可用的,并且可以@AutoWired到您的测试中。 问题是我确实使用了SpringBootTest注释,但当我运行测试时,TestRestTemplate总是为空。也许我错过了什么。 编辑:我在添加@RunWith(SpringRunner.class)注释时遇到了完全相同的问

  • 我创建了自己的库(com.custom.mylib),它返回一个字符串,如下所示。 我创建了一个将使用上述库的项目。我已将lib作为pom依赖项包含在内。但是当我尝试从我的应用程序调用库方法时。我得到了下面的错误。如何解决它? 请考虑在您的配置中定义一个“com.custom.mylog.MyLibrary”类型的bean。 我在application.properties文件中也有下面的内容,这

  • 我是Spring的新手。我正面临Spring-Boot的问题。我正在尝试将外部配置文件中的字段自动装配到自动装配的bean中。我有以下类 应用程序。Java语言 AppConfig。Java语言 服务接口 服务1 我无法在App类的postconstruct方法中显示服务名称变量。我这样做对吗?