2011-10-07 24 views
10

Aspectのサービスレイヤインスタンス@Autowireにアクセスできません。 Aspectでは、@Autowired Beanへの参照はNULLで、それはNullPointerExceptionを投げます。どんな助けでも大歓迎です。私は設定を混乱させたと思う。続きSpring MVCのAspectにAutowired依存関係が挿入されていません

は私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 /> 

は私の側面です:私は次のようになっています

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

:ここ

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

@Autowired 
private LoginService loginService; 

    //.... 
} 

は、上記で定義され@Authenticationアノテーションを使用して、私のコントローラであり、例外:

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) 

答えて

17

this piece of the documentation参照:春のIoCを使ってAspectJのアスペクトを設定

7.8.3

SpringアプリケーションにAspectJのアスペクトを使用している場合、それが欲しいの両方に自然であり、そのような設定できることを期待してい アスペクトはSpringを使用します。 AspectJランタイム自体がアスペクトの作成を担当し、AspectJで作成されたAspectJのアスペクトをAspectJインスタンス化モデル( '-xxx'節)によって設定する方法は、アスペクトで使用されます。

AspectJの大部分は、シングルトンの側面です。これらのアスペクトの設定は非常に簡単です。単純にアスペクトタイプを参照するbean定義を作成し、bean属性 'factory-method = "aspectOf"'を含めるだけです。これにより、インスタンス自体を作成しようとするのではなく、AspectJにアスペクトインスタンスを要求することによってAspectインスタンスを取得することが保証されます。例:

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

docは大量ですが、王様です。 – lwpro2

+0

factory-method = "aspectOf"を追加すると、私の問題を解決しました。ありがとう!! – sjaiswal

+0

こんにちは、Javaの設定に変換する方法を知っていますか? – Sofiane