1

I've got a problem with @Autowiring being null. I am looking for advice how to model it the spring-boot-way.

My Soap-Services get really big using lots of Repository classes. This gives me a large list of @Autowired already. Now when I want to call a helper-class like HeaderValidator.class I can't instantiate and call it like a POJO. This because everything annotated @Autowiring in my HeaderValidator is null. I can make it work when I add @Autowired at line (1) and remove the content of (2) in SoapServiceImpl. But this will end in a huge list of @Autowired annotated fields and this looks ugly. I want to prevent this even it works for now.

This Article mentions the @Configurable with AspectJ. But the Article is from 2013 and Spring-Boot has developed since. I tried the @Configurable solution but it didn't work in my case.

How can I inform my SpringBoot-Application of a class copy? Is the @Configurable-way still the only one? Or did I simply model the application wrong?

Application.class:

@SpringBootApplication
public class Application {

private static ApplicationContext ctx;

    public static void main(String... args) {
    ctx = SpringApplication.run(Application.class, args);
    publishSoapServices();
}

SoapService.class (gets published when calling publishSoapServices() in Application.class):

public class SoapServiceImpl implements SoapService {

@Autowired
ProjectRepository projectRepo;
(1) "@Autowired"
HeaderValidator headerValidator;

@Override
public EventReport send(@WebParam(name = "header") HeaderType headerType,
        @WebParam(name = "content") ContentType contentType) {
    return storeServiceData(headerType, messageType);
}


private EventReport storeServiceData(HeaderType headerType, ContentType contentType) {
    projectRepo.save(contentType);
    (2)  "HeaderValidator headerValidator= new HeaderValidator()"
    return headerValidator.validate(headerType);
}

My problem class:

@Service
public class HeaderValidator {

@Autowired
ValidFieldsRepository validFieldsRepo; //<-- always null!
Community
  • 1
  • 1
SWiggels
  • 2,159
  • 1
  • 21
  • 35
  • 1
    If you want spring to inject into non managed beans then you indeed need `@Configurable` there is no other way. Also if you have a lot of `@Autowired` dependencies you might want to rethink your architecture, if the amount of fields is making your head hurt you are doing something wrong (generally that is). At least that is my rule of thumb. – M. Deinum Sep 03 '15 at 10:38
  • Yes thats why I asked this question, It makes my head hurt :-) I retry the `@Configurable` then... – SWiggels Sep 03 '15 at 12:02

1 Answers1

1

I managed to solve my problem. It was simply due to bad design. I went trough the application and configured @Configurableit correctly. Now it works all fine. Thanks to M.Deinum!

SWiggels
  • 2,159
  • 1
  • 21
  • 35