Java 通过JEE应用程序中的注释触发弹簧配置
war应用程序使用JEE/CDI/JAX-RS堆栈,由Wildfly管理/部署 我想简化应用程序的某些部分,例如让Spring处理属性文件中的配置。所以我需要把JEE和Spring结合起来 以下是Spring配置:Java 通过JEE应用程序中的注释触发弹簧配置,java,spring,jakarta-ee,configuration,Java,Spring,Jakarta Ee,Configuration,war应用程序使用JEE/CDI/JAX-RS堆栈,由Wildfly管理/部署 我想简化应用程序的某些部分,例如让Spring处理属性文件中的配置。所以我需要把JEE和Spring结合起来 以下是Spring配置: import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframe
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:test.properties")
public class Config {
@Value( "${hostName}" )
private String hostName;
public String getHostName() {
return hostName;
}
}
Config
通过CDI注释注入:
import javax.inject.Inject;
public class Consumer {
@Inject
Config config;
public String test(){
return "test +" + config.getHostName();
}
}
Config
已成功注入,但其getHostName
方法始终返回null。在Maven依赖项中,我将SpringCore
,SpringContext
,Springbeans
添加到依赖项中
现在类路径中不存在test.properties
文件
如何触发Spring加载文件并引发文件不存在的异常。确保它工作。这不起作用。您将需要Spring来管理注入和依赖项。仅仅添加那些注释和在几个依赖项中插入将不起作用。如果您想读取配置frmo属性,您可以使用。或者编写一个bean来读取配置并通过producer方法公开值。混合使用Spring可能是一种过分的做法。@NikosParaskevopoulos,编写读取配置的bean不是问题。我计划将Spring不仅用于配置,还用于其他一些事情。最初的问题是,spring与jee混合,实际上spring现在也是jee规范的提供者,Wildfly使用weld作为cdi的提供者。所以我需要一个结合这些背景的解决方案。