首页 > 解决方案 > 读取 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

标签: javaspringspring-boot

解决方案


推荐阅读