0

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())
    }
EI-01
  • 1,075
  • 3
  • 24
  • 42
  • You have to create object of type `StaticContextAccessor` to be able to use `this` keyword in `registerInstance` method. In `HttpUtil` you call `getBean` method statically therefore no object. Try to dynamically register `StaticContextAccessor ` as spring bean. Read this https://stackoverflow.com/questions/15328904/dynamically-declare-beans-at-runtime-in-spring – Jay Smith Jun 28 '17 at 05:22
  • You are totally violating the entire point of injection by using statics. Why is `Httputil` static, and not a Spring-managed bean, injected with the `ProxyConfig`? – Andreas Jun 28 '17 at 05:30
  • @Andreas HttpUtil class is not static but RequestConfig is. – EI-01 Jun 28 '17 at 05:33
  • @JaySmith How do i dynamically register the StaticContextAccessor – EI-01 Jun 28 '17 at 06:02
  • @user3497437 You're right, I misphrased that. The class is not static (it's not even a nested class), but it has a static field, and presumable some static methods (since field is private), so it's a class of static members, which is what I was trying to say. A class of all static members, is a class you never instantiate, and this is against the purpose of injection. The class should be *instantiated* as a singleton Spring bean, and should not have any static members. – Andreas Jun 28 '17 at 14:17

1 Answers1

0
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())

In this code defaultRequestConfig is static so it is initialized when the class is loaded. At that point:

  1. Spring is not intialized
  2. If StaticContextAccessor is actually a bean, it's not initialized so instance is null.

You need to re-think this whole design. Why not have everything as Spring managed bean? Why not have defaultRequestConfig as bean? Why all these statics?

Strelok
  • 50,229
  • 9
  • 102
  • 115