在Java / Spring中读取属性值

时间:2019-06-14 10:49:58

标签: java spring spring-boot

我与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类的字段值?

0 个答案:

没有答案