Spring @Value:具有动态arraylist

时间:2019-01-10 03:54:48

标签: java spring spring-boot

我的情况是从数据库中读取动态值列表,并将此ArrayList传递给@Value。

我的定义如下,

@Value("#{getListOExpressions.split(',')}")
private List<String> secondSchedule;

getListOExpressions是返回arrayList的bean。运行时,我收到错误消息,说评估逗号分隔的不是java.lang.string类型的表达式。

如何将arraylist传递给@value?而且我不是从属性文件中读取的。

我的确切骆驼代码是

@Value("#{getListOfExpressions}")
private List<String> secondSchedule;

@Override
public void configure() throws Exception {


    from("quartz2://fraudIngestion/ruleExecuteSecondSequence?cron=" + secondSchedule + "")
    .log("Start executing secondSequence Rule")
            .bean(RulesExecutor.class, "getExecuteRuleWithSecondSequence(" + secondSchedule + ")")
            .log("Completed executing secondSequence Rule").end();



            }

2 个答案:

答案 0 :(得分:0)

getListOExpressions()应该返回用逗号分隔的项目的字符串,而不是任何数组列表

答案 1 :(得分:0)

以下示例可以正常工作:

package com.stackoverflow.q54121739;

import static java.util.stream.Collectors.*;
import static org.junit.Assert.*;

import java.util.List;
import java.util.stream.Stream;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;

@SuppressWarnings("javadoc")
public class Answer {

    /** The Constant SPRING_CLASS_RULE. */
    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

    /** The spring method rule. */
    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    static final List<String> EXPECTED = Stream.of("first", "second")
        .collect(toList());

    @Value("#{listOfExpressions}")
    List<String> secondSchedule;

    @Value("#{listOfExpressionsAsCommaDelimitedString}")
    List<String> secondScheduleFromCommaDelimitedString;

    @Test
    public void test() {
        System.out.println(this.secondSchedule);
        System.out.println(this.secondScheduleFromCommaDelimitedString);
        assertEquals(2, this.secondSchedule.size());
        assertEquals(2, this.secondScheduleFromCommaDelimitedString.size());
        assertEquals(EXPECTED, this.secondSchedule);
        assertEquals(EXPECTED, this.secondScheduleFromCommaDelimitedString);

    }

    @Configuration
    static class Config {

        Config(ConfigurableApplicationContext configurableApplicationContext,
                DefaultConversionService conversionService) {
            super();
            configurableApplicationContext.getBeanFactory()
                .setConversionService(conversionService);
        }

        @Bean
        public static DefaultConversionService conversionService() {
            return new DefaultConversionService();
        }

        @Bean
        List<String> listOfExpressions() {
            return EXPECTED;
        }

        @Bean
        String listOfExpressionsAsCommaDelimitedString() {
            return EXPECTED.stream()
                .collect(joining(","));
        }
    }
}

示例中的依赖项:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
</dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <scope>test</scope>
</dependency>