在Aspect中将注入的Bean重置为NULL [英] Injected bean reset to NULL in the Aspect

查看:292
本文介绍了在Aspect中将注入的Bean重置为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Spring AOP和Aspectj .我已经看到了与null方面的注入bean相关的各种帖子,并且遇到了类似的问题.我仍然不清楚应该如何解决当前遇到的问题.

I am new Spring AOP and Aspectj. I have seen various posts related to injected bean in an aspect being null and I have run into a similar problem. I am still not clear how I should proceed to get past the problem I am currently encountering.

问题:当前,我们正在使用Spring 3.2.3,并且所有注入都是通过注释进行的.在我的情况下,Spring正确注入了依赖的bean,但是在执行时注入的bean为NULL.顺便说一句,这并不是一直都发生,但是我可以说的是,当失败和成功时的堆栈跟踪略有不同.当注入的bean不为空时(我可以成功使用注入的bean服务),对before建议(在方面)的调用总是在目标方法被调用之前发生.当注入的bean为NULL时,调用方面是从目标方法的第一个语句开始的.在这一点上,我认为另一个方面已实例化,并且没有引用注入的bean.这是我创建的方面:

Issue: Currently we are using Spring 3.2.3 and all injection is through Annotation. In my case, the dependent bean is injected properly by Spring but at the point of execution the injected bean is NULL. BTW, this doesn't happen all the time but what I can say is the stack trace when it fails and when it succeeds is slightly different. When the injected bean is not null (I can successfully use the injected bean service), the call to the before advice (in the aspect) always happens before the target method is called as it should.When the injected bean is NULL, the call to the aspect is from the first statement of the target method. At this point, I think another aspect is instantiated and has no reference to the injected bean. Here is the aspect I have created:

@Component
@Aspect
public class Enable{

       private NameService nameService;

       @Autowired
       public void SetNameService(NameSerice service){          
            // service is injected properly
            this.nameSerice = service;
       }

       @Before("* *.*(..)")      
       public void callBefore(JoinPoint jp){    
            //sometimes nameService is null and sometimes it not not    
            this.nameService.lookup(...);    
       }
}

研究各种文章,一种解决此问题的方法(如文章中所建议)是在XML配置文件中配置方面,并使用factory-method ="aspectOf",并在配置中注入对NameService bean作为属性.我们的整个项目都使用基于注释的注入(如前所述).假设我仍然可以在XML配置文件中配置上述方面,那么如何获取引用NameService bean Id,以便可以将其添加到配置中.我还看到了与使用可配置批注有关的帖子,但我认为那是针对在Spring IOC外部创建的对象的.

Examining the various posts, one way to get around this (as suggested in the post) is to configure the aspect in the XML configuration file and use the factory-method ="aspectOf" and in the configuration inject the reference to the NameService bean as a property. Our whole project uses Annotation based injection (as stated earlier). Assuming I can still configure the above aspect in an XML configuration file, how can I get the reference NameService bean Id so that I can add it to the configuration. I also saw a post related to using Configurable annotation but I assume that is for objects created outside the Spring IOC.

当前,使用pom.xml中的Aspectj编译选项来编织方面.我们的root-context.xml包含条目context:annotation-config,并且方面已注入到Spring IOC中,因为已为方面所在的文件夹打开了组件扫描.任何帮助将不胜感激

Currently, the aspects are woven using Aspectj compile option in pom.xml. Our root-context.xml contains the entry context:annotation-config and the aspect is injected into Spring IOC because component-scan is turned on for the folder where the aspect resides. Any help will be appreciated

推荐答案

回答这个问题可能为时已晚.但是我遇到了相同的情况,并按如下所示进行了修复.

It might be too late to answer this question. But i have come across the same situation and i fixed it as below.

1)在您的方面类中有一个用于"NameService"的setter和getter. 2)用@Component标记"NameService"("nameService") 3)使用setter注入在xml配置中配置"nameService". 4)进行更改后,重新启动服务器.

1) Have a setter and getter for "NameService" in your aspect class. 2) Mark "NameService" with @Component ("nameService") 3) Configure "nameService" in xml configuration using setter injection. 4) Re-Start your server after making changes.

这应该解决方面的"NameService"为空的问题.

This should resolve the problem of getting null for "NameService" in aspect.

这篇关于在Aspect中将注入的Bean重置为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆