【发布时间】:2021-03-18 22:28:30
【问题描述】:
我有以下@ApplicationScoped bean:
@ApplicationScoped
public class ServiceProducer {
private static final Logger logger = LoggerFactory.getLogger(ServiceProducer.class);
@Default
@Produces
@PersistenceContext(unitName = "nemo")
private EntityManager nemoEntityManager;
private CacheManager cacheManager;
@PostConstruct
void init() {
try {
cacheManager = Caching.getCachingProvider().getCacheManager(
Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("/ehcache.xml")).toURI(),
Thread.currentThread().getContextClassLoader());
} catch (URISyntaxException e) {
logger.error(e.getMessage());
}
}
@Produces
public CacheManager produceCacheManager() {
return cacheManager;
}
上述类包含在我的 Web 应用程序的公共/共享模块中。 这对生产有好处,但是我需要一个集成测试的替代方案,它是使用带有 wildspike 集成的 Cucumber 完成的。
通过配置beans.xml,不幸的是我无法禁用上面的bean,我尝试以这种方式配置beans.xml:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all">
<scan>
<exclude name="it.myapp.ecommerce.commons.resource.ServiceProducer"></exclude>
</scan>
但是没有任何改变。是因为该类属于依赖项吗?
【问题讨论】:
-
如果我没记错的话,
<exclude>过滤器必须在定义类的 bean 存档的 beans.xml 中工作。但是,alternatives+@Priority可能适合您的情况。 -
你能发一些sn-ps吗?我仍然不清楚如何使用资源提供者 bean 的 @alternative 注释,就像我发布的那样
-
为了清楚起见,我已经有了生成记录器和实体管理器的替代类,但问题是我不能只使用它。启动黄瓜时,由于服务生产者类,我有模棱两可的注入
-
您是否将生产者 method 注释为替代方法?
-
@NikosParaskevopoulos 是的,我做到了,然后我用替代配置 beans.xml,但我没有任何改进,即使排除公共模块的 resorces/test/META-INF/beans.xml 也不起作用
标签: java cdi weld jboss-weld java-ee-8