.yml 文件
cassandra:
keyspaceApp:junit
solr:
keyspaceApp:xyz
豆
@Component
@ConfigurationProperties(prefix="cassandra")
public class CassandraClientNew {
@Value("${keyspaceApp:@null}") private String keyspaceApp;
主要方法文件
@EnableAutoConfiguration
@ComponentScan
@PropertySource("application.yml")
public class CommonDataApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(CommonDataApplication.class)
.web(false).headless(true).main(CommonDataApplication.class).run(args);
}
}
测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = CommonDataApplication.class)
@IntegrationTest
@EnableConfigurationProperties
public class CassandraClientTest {
@Autowired
CassandraClientNew cassandraClientNew;
@Test
public void test(){
cassandraClientNew.getSession();
System.out.println(" **** done ****");
}
}
它没有将 junit 设置为 keyspaceApp,而是设置了 xyz。
看起来 prefix=“cassandra” 不工作
原文由 plzdontkillme 发布,翻译遵循 CC BY-SA 4.0 许可协议
看起来您正在尝试使用 Spring Boot Typesafe Configuration Properties 功能。
因此,为了让它正常工作,您必须对代码添加一些更改:
首先,你的
CommonDataApplication
类应该有@EnableConfigurationProperties
注释例如I do not believe you need
@PropertySource("application.yml")
annotation asapplication.yml
(as well asapplication.properties
andapplication.xml
) is a default configuration file used by Spring启动。您的
CassandraClientNew
类不需要@Value
注释前缀keyspaceApp
属性。你的keyspaceApp
必须有一个 setter 方法。顺便说一句,如果您正在使用
List
或Set
s 并且您初始化集合(例如List<String> values = new ArrayList<>();
),则只需要 get-ter 33e如果一个集合没有被初始化,那么你也需要提供一个 setter 方法(否则会抛出异常)。我希望这会有所帮助。