【发布时间】: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