【问题标题】:Could not resolve placeholder 'kafka.bootstrap-servers' in string value "${kafka.bootstrap-servers}"无法解析字符串值“${kafka.bootstrap-servers}”中的占位符“kafka.bootstrap-servers”
【发布时间】:2019-06-19 12:44:42
【问题描述】:

我必须继续其他人的项目,但是当我在 Intellij 上运行 spring 应用程序时,我遇到了错误。我是春天和行家的新手。 我在 ressource 文件夹中有一个 application.properties,没有任何用于配置的 xml 文件。

这是构建部分pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>
<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
        <!--
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--
                <configuration>
                    <mainClass>kobdig.SimulationApplication</mainClass>
                </configuration>
                -->
            </plugin>
        </plugins>
    </build>
</code>

SimulationApplication.java

@SpringBootApplication
public class SimulationApplication {

    public static void main(String[] args){
        SpringApplication.run(SimulationApplication.class, args);
    }
}

SenderConfig.java

@Configuration
@PropertySource(ignoreResourceNotFound = true, value = "file:application.properties")
public class SenderConfig {

    @Value("${kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Bean
    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        // list of host:port pairs used for establishing the initial connections to the Kakfa cluster
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
                bootstrapServers);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
                StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
                StringSerializer.class);

        return props;
    }

application.properties

server.port=9090
kafka.bootstrap-servers = localhost:2020

Sender.java

public class Sender {

    private static final Logger LOGGER =
            LoggerFactory.getLogger(Sender.class);

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void send(String topic, String payload) {
        LOGGER.info("sending payload='{}' to topic='{}'", payload, topic);
        kafkaTemplate.send(topic, payload);
    }
}

我正面临这个错误:

嵌套异常是 java.lang.IllegalArgumentException: 不能 在字符串值中解析占位符“kafka.bootstrap-servers” "${kafka.bootstrap-servers}"

已经尝试了很多东西,我在stackOverflow上看到了很多类似的主题但找不到问题。 @PropertySource 没有帮助。 你能帮帮我吗?

【问题讨论】:

  • 你试过去掉空格吗? kafka.bootstrap-servers=localhost:2020
  • 是的,不是解决方案:(
  • 服务器端口属性有效吗?
  • 感谢您的回复。我把 2020 端口改成 9092 是我的 kafka 服务器的端口,但是错误仍然在这里,所以我认为它来自代码本身...

标签: java maven spring-boot properties


【解决方案1】:

确保你的 application.properties 文件在你的类路径中。如果你的属性文件在另一个包中,那么它不会被 spring 自动占用。

如果属性文件在您的类路径中,请尝试使用如下属性源

@PropertySource("classpath:application.properties")

如果你把application.properties放在src/main/resources下面,就不用@PropertySource注解了。Spring应该会自动占用它。

验证您的 application.properties 中的其他值是否被拾取,如果没有,可能是因为 bean PropertySourcesPlaceholderConfigurer,请尝试创建一个 PropertySourcesPlaceholderConfigurer 的 bean。

【讨论】:

  • 谢谢!我用intellij在类路径中添加了application.properties,它适用于应用程序,但是当我用maven运行时,错误仍然存​​在,你有我的解决方案吗?
  • 您将 application.properties 文件放在哪里。您必须使用 maven 重建项目,然后启动服务器以反映更改。
猜你喜欢
  • 2021-04-22
  • 2017-07-23
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 2018-06-17
相关资源
最近更新 更多