简体   繁体   English

未设置Spring Boot配置属性

[英]Spring Boot Configuration Properties not being set

I am trying to set up a clamav virus scanner in a Spring Boot environment. 我正在尝试在Spring Boot环境中设置clamav病毒扫描程序。 So I want to set the host and port in a properties file, clamav.properties, located in my resources directory along with the application.properties file. 因此,我想在属性文件clamav.properties中设置主机和端口,该文件位于我的资源目录中,以及application.properties文件。 it looks like this: 它看起来像这样:

clamav.host=localhost
clamav.port=3310
clamav.timeout=1000

I have this class: 我有这个课:

@ConfigurationProperties("clamav.properties")
public class ClamAvClient {

static final Logger logger = LoggerFactory.getLogger(ClamAvClient.class);

@Value("${clamav.host}")
private String clamHost;

@Value("${clamav.port}")
private int clamPort;

@Value("${clamav.timeout}")
private int clamTimeout;

 public boolean ping() throws IOException {
     logger.debug("Host:"+clamHost+" Port:"+clamPort);
     blah.....
 }

private static byte[] asBytes(String s) {
    return s.getBytes(StandardCharsets.US_ASCII);
}   

public String getClamHost() {
    return clamHost;
}

public void setClamHost(String clamHost) {
    this.clamHost = clamHost;
}

public int getClamPort() {
    return clamPort;
}

public void setClamPort(int clamPort) {
    this.clamPort = clamPort;
}

public int getClamTimeout() {
    return clamTimeout;
}

public void setClamTimeout(int clamTimeout) {
    this.clamTimeout = clamTimeout;
}

}

It's not connecting and in the logs I get this: 它没有连接,在日志中我得到以下信息:

2017-09-23 20:39:45.947 DEBUG 28857 --- [http-nio-8080-exec-2] xxx.ClamAvClient  : Host:null Port:0

So those values are clearly not getting set. 因此,这些值显然没有设置。 What am I doing wrong? 我究竟做错了什么? I am using the managed version of spring-boot-starter-web, which my Eclipse is saying is 1.4.3-RELEASE 我正在使用spring-boot-starter-web的托管版本,我的Eclipse说的是1.4.3-RELEASE

Any Ideas? 有任何想法吗?

Either use @ConfigurationProperties to map group of properties to a Class using Configuration Processor. 使用@ConfigurationProperties使用配置处理器将属性组映射到类。

Using @Value inside @ConfigurationProperties doesn`t look right. @ConfigurationProperties使用@Value看起来不正确。

All you need to map your properties to the class is : 您需要将属性映射到该类的所有操作是:

@Configuration
@ConfigurationProperties(prefix="clamav")
public class ClamAvClient {

static final Logger logger = LoggerFactory.getLogger(ClamAvClient.class);


private String host;


private int port;


private int timeout;

//getters and setters

}

prefix ="clamav" matches your prefixes in the properties file. prefix ="clamav"与属性文件中的前缀匹配。

host,port,timeout matches the properties of the class. host,port,timeout匹配类的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM