CDI @Alternative 注释与 @ApplicationScoped

CDI @Alternative annotation with @ApplicationScoped

我有以下界面:

public interface StackConfigurationService {
    List<StackConfiguration> getStacksByAppId(String appId) throws ConfigurationException;
}

有两个实现:

@ApplicationScoped
public class StackConfigurationServiceImpl implements StackConfigurationService {
    public List<StackConfiguration> getStacksByAppId(String appId) throws ConfigurationException { ... }
}

@Alternative
@ApplicationScoped
public class StackConfigurationMockService implements StackConfigurationService {
    public List<StackConfiguration> getStacksByAppId(String appId) throws ConfigurationException { ... }
}

beans.xml 包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>
    <beans 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/beans_1_0.xsd">

    <alternatives>
        <class>my.app.StackConfigurationMockService</class>
    </alternatives>
</beans>

但是,CDI 从不使用服务的模拟实现。 @ApplicationScoped 注释是否以任何方式干扰?

它一直在工作。我正在更改 beans.xml 中的设置,重建并重新启动服务器,但问题出在浏览器中。我使用的是相同的选项卡,并在每次服务器重新启动后简单地点击刷新。我假设服务器为该会话缓存了 类。在新标签页中打开解决了问题。