有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java CDI@Alternative注释和@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 { ... }
}

豆子。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注释是否有任何干扰


共 (1) 个答案

  1. # 1 楼答案

    它一直在发挥作用。我正在更改beans中的设置。xml,重建并重新启动服务器,但问题出在浏览器上。我使用的是同一个选项卡,每次服务器重启后只需点击刷新。我假设服务器缓存了该会话的类。在新选项卡中打开解决了问题