1

I'm trying to use an Autowired in this class, but the variable config is always null ... In other classes Autowired works.

This project was generated by jhipster, I do not know if there is a relation

@Component
@WebService(endpointInterface = "SignaturePortTypeV2")
public class Signature extends SpringBeanAutowiringSupport implements SignaturePortTypeV2 {
    @Autowired
    ConfigServiceBean config;


    @Override
    public ExecuteTokenCmdRespType executeTokenCmd(ExecuteTokenCmdReqType tokenCmdReq) throws ICPMException {
        config.getValue(CommonConfigKey.COMPANY_IDENTIFIER);
        return null
    }
}
@Service
public class ConfigServiceBean implements ConfigServiceLocal {

    @Autowired
    private Environment env;

    @SuppressWarnings("unchecked")
    @Override
    public <T> T getValue(ConfigKey configKey) {
        switch (configKey.getType()) {
            case STRING:
                return (T) env.getProperty(configKey.getKey(), String.class, configKey.getDefaultValue());
            case INT:
                return (T) env.getProperty(configKey.getKey(), Integer.class, configKey.getDefaultValue());
            case LONG:
                return (T) env.getProperty(configKey.getKey(), Long.class, configKey.getDefaultValue());
            case DOUBLE:
                return (T) env.getProperty(configKey.getKey(), Double.class, configKey.getDefaultValue());
            case BOOLEAN:
                return (T) env.getProperty(configKey.getKey(), Boolean.class, configKey.getDefaultValue());
            default:
                throw new IllegalStateException("Type not expected: " + configKey.getType());
        }
    }
}
  • 1
    **1)** How do you know? **2)** How are you obtaining your `Signature` instance? **3)** When is the `executeTokenCmd` method executed? – Morfic Jan 31 '19 at 00:09
  • Well, I publishe a interface with endpoint, like this: `Endpoint.publish("http://127.0.0.1:9876/ws", new Signature());`, and the `executeTokenCmd` is executed when I call this interface, through SOAPUI, for example. – Eduardo Scartezini Jan 31 '19 at 03:09
  • Well you did not answer my first question, but based on the fact that you're "_manually_" doing `new Signature()` instead of getting the instance from the Spring context, it's rather normal since Spring would not know about your instance and thus the injection does not occur "_magically_". This is most likely a duplicate of [Why is my Spring @Autowired field null?](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – Morfic Jan 31 '19 at 09:29
  • One more reason to have constructor injection as opposed to field/setter :-) – Morfic Jan 31 '19 at 12:02

1 Answers1

2

I see some strange things:

  1. You say that config variable is null, however, it's annotated with @Autowired. It should fail in the Spring Context Load since the injection is required (@Autowired by default has the attribute required = true). So the first question is: Is the bean Signature being created? It could be that your @Configuration annotated classes (or however you create your spring context) that is looking into another packages.
  2. method executeTokenCmd is always returning null. It just retrieves a value from the env and then return null. This really doesn't make sense. Could this be the reason of your error?

It would help if you could paste the error trace.

Jose Luis
  • 254
  • 2
  • 11
  • Thank you for taking the time to reply and trying to help. However, this does not provide a direct solution to the question, and according to the SO philosophy, adding suggestions or requesting clarifications and further details should be done via [comments](https://stackoverflow.com/help/privileges/comment) rather than answers. – Morfic Jan 31 '19 at 09:38
  • Thanks for letting me know. I'm brand new on this regard. I will be more careful next time – Jose Luis Feb 01 '19 at 10:33