自动连接的依赖项未在Spring MVC中的Aspect中注入

时间:2011-10-07 22:46:07

标签: spring-mvc aop aspectj spring-aop

我无法在Aspect中@Autowire服务层实例。在Aspect中,对@Autowired bean的引用为NULL,并抛出NullPointerException。任何帮助都感激不尽。我想,我搞砸了配置。

以下是我的servlet-context.xml

<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<context:spring-configured />       

<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="xx.yy" />

<!--  an @AspectJ aspect will be interpreted as an aspect by Spring AOP and beans in the context will be advised accordingly -->
<aop:aspectj-autoproxy />

<beans:bean id="loggingAspect" class="xx.yy.aop.aspects.LoggingAspect" />
<beans:bean id="authenticationAspect" class="xx.yy.aop.aspects.AuthenticationAspect" />

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

以下是我的看点:

@Configurable
@Component
@Aspect
public class AuthenticationAspect {
private static final Logger logger = LoggerFactory.getLogger(AuthenticationAspect.class);

@Autowired
private LoginService loginService;

    //.... 
}

这是我的控制器使用上面定义的@Authentication注释:

@Controller
@RequestMapping("/user")
public class UsersController {

@Autowired
private UserService userService;

@Authenticate
@RequestMapping(value="/{userId}/profile", method=RequestMethod.GET)    
public String displayUser(WebRequest webRequest, @PathVariable("userId") String userId, Model model) {
    User user = userService.findUser(Long.valueOf(userId));  
    model.addAttribute("user", user);
    model.addAttribute("AccordionMenuTab","5");
    model.addAttribute("selectedLink","profile");
    return "profile";
}

我遇到以下异常:

Oct 8, 2011 3:12:48 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet appServlet threw exception
java.lang.NullPointerException
    at xx.yy.controller.UsersController.displayUser_aroundBody1$advice(UsersController.java:28)
    at xx.yy.controller.UsersController.displayUser(UsersController.java:1)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

2 个答案:

答案 0 :(得分:17)

请参阅this piece of the documentation

  

7.8.3使用Spring IoC配置AspectJ方面

     

在Spring应用程序中使用AspectJ方面时,很自然希望能够使用Spring配置这些方面。 AspectJ运行时本身负责方面创建,并且通过Spring配置AspectJ创建方面的方法取决于方面使用的AspectJ实例化模型('per-xxx'子句)。

     

AspectJ的大多数方面都是单身方面。这些方面的配置非常简单:只需创建一个正常引用方面类型的bean定义,并包含bean属性'factory-method =“aspectOf”'。这可以确保Spring通过向AspectJ请求它来获取方面实例,而不是尝试创建实例本身。例如:

<bean id="profiler" class="com.xyz.profiler.Profiler"
      factory-method="aspectOf" />

答案 1 :(得分:0)

对于正在寻找基于Java的Bean配置的任何人,使用Java反射,我都可以将其存档

@Bean
public ExceptionAspectHandler exceptionAspectHandler(){
    try
    {
        //noinspection JavaReflectionMemberAccess
        Method method = ExceptionAspectHandler.class.getMethod("aspectOf" );
        return (ExceptionAspectHandler) method.invoke(null);
    }
    catch( IllegalAccessException | InvocationTargetException | NoSuchMethodException e )
    {
       logger.log( Level.SEVERE, "Error creating bean : ", e );
    }
    return null;
}

由于aspectOf()方法在编译期间不可用,我们不能仅通过调用该方法来创建bean。这就是XML配置能够处理它的原因。

另一种简单的方法

@Bean
public ExceptionAspectHandler exceptionAspectHandler()
{
    return Aspects.aspectOf( ExceptionAspectHandler.class );
}

这也可以。