0

我有3种。

ServiceAffaire.java:

package ws.magicnew.sync;

...

@Transactional
@Service("serviceAffaire")
public class ServiceAffaire implements IServiceAffaire {

    private static Log log = LogFactory.getLog(ServiceAffaire.class);

    //private static SimpleDateFormat sdf = new SimpleDateFormat(MagicFacade.WS_DATE_FORMAT);

    @Autowired
    @Qualifier("manifestationService")
    protected IManifestationService manifestationService;

    @Autowired
    @Qualifier("typeDeManifestationService")
    protected ITypeDeManifestationService typeService;

    @Autowired
    @Qualifier("espaceManifestationService")
    protected IEspaceManifestationService espaceManifService;

    @Autowired
    @Qualifier("siteService")
    protected ISiteService siteService;

    @Autowired
    @Qualifier("natureService")
    protected INatureService natureService;

    @Autowired
    @Qualifier("facadeGetAffaire")
    protected MagicFacade facadeGetAffaire;

    @Autowired
    @Qualifier("compteurManifestation")
    private Compteur compteurManifestation;

    @Autowired
    @Qualifier("compteurContenus")
    protected Compteur compteurContenus;

        public synchronized void synchronize(boolean setFlag) throws Exception {
            ...
        }
}

IServiceAffaire.java:

package ws.magicnew.sync;

public interface IServiceAffaire {

    public void synchronize(boolean setFlag) throws Exception;

}

和 CacheAction.java:

package ws.magicnew.sync;

...

@Configurable
@Transactional
public class CacheAction extends DispatchAction {

    private static Log log = LogFactory.getLog(CacheAction.class);

    @Autowired
    @Qualifier("serviceAffaire")
    private IServiceAffaire serviceAffaire;

    public ActionForward getAffaire(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        boolean setFlag = Boolean.parseBoolean(CmsProperties.SYNCRO_AFFAIRES_FLAG);
        log.info("getAffaire debut " +setFlag);
        serviceAffaire.synchronize(setFlag); // NullPointerException here: serviceAffaire is null
        log.info("getAffaire fin " +setFlag);
        request.setAttribute("message", "Le service get affaire a été lancé.");

        return mapping.findForward("cache");
    }
}

接线是在 applicationContext-scanpackages.xml 文件中进行的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xmlns:cxf="http://cxf.apache.org/core" 
    xsi:schemaLocation="
            http://www.springframework.org/schema/aop 
                http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/jee 
                http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
            http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/util 
                http://www.springframework.org/schema/util/spring-util-3.0.xsd
            http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
                http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd 
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
    <context:annotation-config />
    <context:component-scan base-package="ws.magicnew.sync" />
    <context:component-scan base-package="ws.magicnew.facade" />
</beans>

这对我来说似乎没问题,但是NullPointerException当我调用 CacheAction.getAffaire() 方法时我仍然得到一个。我不知道为什么,这让我发疯。有什么线索吗?

我之前在注入 ServiceAffaire 的属性时遇到了一些问题(我解决了),所以 Spring 实际上是自动装配它们。但是由于某种原因,它无法将 ServiceAffaire 注入到 CacheAction 中。

如代码中的注释所示,调用 ServiceAffaire.synchronize 时 serviceAffaire 属性为空,正如我在调试模式下执行时看到的那样。

4

2 回答 2

2

你的问题可能是两件事:

首先,您需要在 applicationContext-scanpackages.xml 中添加“context:spring-configured”以启用 AspectJ。

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-atconfigurable

其次,您将 CacheAction 设置为 @Configurable,我想您这样做是因为您或某些框架使用经典的“new CacheAction()”创建了 CacheAction 的实例,而不是让 Spring 创建它。

如果是这种情况,如果您在 Spring 初始化 IServiceAffaire bean 之前通过代码创建 CacheAction 的实例,则可能会发生 NPE。我的意思是,当您使用“new”创建 CacheAction 实例时,您必须确保 Spring 已经完成初始化 CacheAction 所需的所有 bean。否则 Spring 将无法注入依赖项。

如果某些框架创建 CacheAction 的新实例并且您无法控制这些实例的创建时间,这可能会很棘手。是否添加诸如“depends-on”之类的注释并不重要,因为 Spring 将无法保持该实例创建,直到它完成创建需要注入的 bean。

解决这个问题的一种方法是让 Spring 初始化一些 bean,该 bean 将触发任何创建 CacheAction 新实例的初始化,并在那里添加一个“依赖于”IServiceAffaire bean。

无论如何,正确的解决方案取决于您的应用程序是如何初始化的。

于 2012-11-02T15:23:46.850 回答
0

您可以尝试将 IServiceAffaire 的依赖注入注释更改为 @Resource 并检查是否有效

@Autowired
@Qualifier("serviceAffaire")
private IServiceAffaire serviceAffaire; 

改成

@Resource(name ="serviceAffaire")
private IServiceAffaire serviceAffaire;
于 2012-11-02T16:25:38.840 回答