java - 读取 Java/Spring 中的属性值
问题描述
我使用 Spring 项目并获得提供的错误堆栈,
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'riskEngineController': Unsatisfied dependency expressed through field 'ellaService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ellaService': Unsatisfied dependency expressed through field 'configHolder'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ellaFilterConfigHolder' defined in class path resource [com/xyz/iris/ella/config/EllaServiceConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xyz.iris.ella.config.EllaFilterConfigHolder]: Factory method 'ellaFilterConfigHolder' threw exception; nested exception is java.lang.IllegalStateException: Required key 'ella.internal.buyer.filter' not found
当我尝试从名为的配置文件中读取一些属性时产生的问题settings.property
,
### ELLA
ella.internal.buyer.filter=false
ella.external.buyer.filter=false
ella.shop.id.filter=123456,56789,545334
ella.uri=localhost:${tomcat.port}/ella-web/api/v1/score
我的服务类是在我用来@PostConstruct
读取属性的地方提供的,
@Service
@EnableAsync
public class EllaService {
@Autowired
@Qualifier( ELLA_CONNECTOR_BEAN_NAME )
private EntityServiceConnectable<EllaResponseDto> connector;
@Autowired
@Getter
private EllaFilterConfigHolder configHolder;
@Autowired
@Getter
private EllaConfiguration config;
@Autowired
private EllaServiceConfiguration ellaServiceConfiguration;
@Autowired
private Environment environment;
@PostConstruct
public void initialize() {
this.configHolder = ellaServiceConfiguration.ellaFilterConfigHolder( environment );
}
/**
* Asynchronously call Ella. Determine if traffic is applicable for Ella and if yes forward to Ella.
*
* @param irisBo
*/
// @Async( "ellaThreadPoolTaskExecutor" )
@Async( ELLA_THREAD_POOL_TASK_EXECUTOR )
public void invokeEllaAsync( final IrisBo irisBo ) {
if( isTrafficAllowed( irisBo ) ) {
callEllaService( irisBo );
}
}
// some code
}
提供EllaServiceConfiguration
课程,
@Configuration
@Import( EllaConfiguration.class )
public class EllaServiceConfiguration {
private static final String SHOP_ID_STRING_SEPARATOR = ",";
public static final String ELLA_SHOP_ID_FILTER_ENABLED_KEY = "ella.shop.id.filter";
public static final String ELLA_INTERNAL_BUYER_FILTER_ENABLED_KEY = "ella.internal.buyer.filter";
public static final String ELLA_EXTERNAL_BUYER_FILTER_ENABLED_KEY = "ella.external.buyer.filter";
@Bean
public EllaFilterConfigHolder ellaFilterConfigHolder( final Environment environment ) {
final EllaFilterConfigHolder configHolder = new EllaFilterConfigHolder();
configHolder.setInternalBuyerFilterEnabled( Boolean.getBoolean( environment
.getRequiredProperty( ELLA_INTERNAL_BUYER_FILTER_ENABLED_KEY ) ) );
configHolder.setExternalBuyerFilterEnabled( Boolean.getBoolean( environment
.getRequiredProperty( ELLA_EXTERNAL_BUYER_FILTER_ENABLED_KEY ) ) );
Set<Integer> set = new HashSet<>( getShopIdsToFilterAsList( environment ) );
configHolder.getShopIdsToFilterSet().addAll( set );
return configHolder;
}
private List<Integer> getShopIdsToFilterAsList( final Environment env ) {
String shopIdListStr = env.getRequiredProperty( ELLA_SHOP_ID_FILTER_ENABLED_KEY );
return Arrays.asList( shopIdListStr.split( SHOP_ID_STRING_SEPARATOR ) ).stream().map( Integer::parseInt )
.collect( Collectors.toList() );
}
}
EllaFilterConfigHolder
班级,
@Getter
@Setter
@Component
public class EllaFilterConfigHolder {
private boolean isInternalBuyerFilterEnabled;
private boolean isExternalBuyerFilterEnabled;
@Setter( AccessLevel.NONE )
private final Set<Integer> shopIdsToFilterSet = new HashSet<>();
}
如何正确读取EllaFilterConfigHolder
我在方法中尝试过的类 的字段值EllaServiceConfiguration::ellaFilterConfigHolder
?
解决方案
推荐阅读
- c++ - TD[N] 是否总是声明数组类型的对象?
- java - 在maven插件中获取mojo参数
- swift - 获取错误的默认值
- excel - Excel如何在一列中循环所有数据
- python - 返回一年中特定日期的上个月
- swift - 不确定如何打开 uilabel 可选以计算我的 uilabels
- java - 如何仅对字符串值设置异常?
- node.js - 由于 MongoDB,Nodejs 无法调用多个 Web 应用程序实例
- node.js - 为什么当我没有调用时 console.log 会显示 CREATE TABLE 语句
- excel - 将 excel 中的许多列从 MS 转换为 HH:MM:SS 的宏