I have a configuration file called proxyconfig.java
that reads from a yaml file and injects it into ProxyConfig.java
file. I need to use the properties from my config to inject it into Httputil class to set up a proxy but i'm getting a null pointer exception as it doesn't inject the properties host and port to set up the proxy. I'm using the StaticContextAccessor to access the proxy config class and get the host and port values. Please could someone assist in what i'm currently doing wrong?
@Configuration
public class ProxyConfig {
@Value("${proxy.host}")
private String host;
@Value("${proxy.port}")
private int port;
public int getPort() { return this.port; }
public int getHost() { return this.host; }
}
StaticContextAccessor class:
public class StaticContextAccessor {
private static StaticContextAccessor instance;
@Autowired
private ApplicationContext applicationContext;
@PostConstruct()
public void registerInstance() {
instance = this;
}
public static <T> T getBean(Class<T> clazz) {
return instance.applicationContext.getBean(clazz);
}
}
My HttpUtil class:
public class Httputil {
private static RequestConfig defaultRequestConfig = RequestConfig.custom()
.setConnectTimeout(1000)
.setSocketTimeout(10000)
.setConnectionRequestTimeout(10000)
// setting the proxy
//causes null pointer as it cannot read host and port
.setProxy(new HttpHost(StaticContextAccessor.getBean(ProxyConfig.class).getHost(), StaticContextAccessor.getBean(ProxyConfig.class).getPort())
}