0

I have a config class which is creating the beans for my application. I am seeing that although I set the bean properties while creating adapter bean, somehow these properties are getting cleared and set to null in my controller class. After 2 hours of debugging, I am drawing a blank. Any pointers please.

@RequiredArgsConstructor//lombok annotation to generate the constructor.
public class MyAdapter {//Trying to create a bean of this type

    @NonNull private final MyPropertyObj prop;
    @NonNull private final Integer timeout;
}

@Configuration
@Profile("!test")
class MyConfigClass{

  @Bean
  public MyAdapter adapter(){
    MyPropertyObj prop= new MyPropertyObj();
    return new MyAdapter(prop, 10);//Here I am setting prop and 10, but when I check auto wired adapter they are null.
  }

}

public class MyController {

    @Autowired private MyAdapter adapter;
//adapter gets injected, but adapter.prop and the adapter.timeout are null.
}
learningtocode
  • 755
  • 2
  • 13
  • 27
  • have you tried http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null – Augustas Sep 21 '16 at 06:05
  • Where is the constructor in your class `myAdapter`? – Jens Sep 21 '16 at 06:05
  • @Augustas I looked at that question. That question is talking about creating a new object by hand and expecting the property inside it to be auto wired. What I am trying to do is different. My bean created during instantiation is somehow changing state and clearing its attributes. – learningtocode Sep 21 '16 at 06:14
  • @Jens RequiredArgsConstructor generates the constructor – learningtocode Sep 21 '16 at 06:15
  • RequiredArgsConstructor comes from Lombok lib you probably should add lombok tag and try there https://projectlombok.org/features/Constructor.html – Augustas Sep 21 '16 at 06:26
  • Not sure if the answer is obvious to you guys. To me its not. Down voting without pointing the issue or reading the question properly is not very helpful. – learningtocode Sep 21 '16 at 06:29
  • @Augustas The issue is not lombok, I verified by inserting my own constructor and verified that constructor is executed. – learningtocode Sep 21 '16 at 06:31
  • well I checked and code runs fine in spring boot with manually created constructor, properties are set ok. Just try creating constructor on your own without annotation it should work. – Augustas Sep 21 '16 at 06:42
  • Oh Ok..yeah it does not look straightforward..I am missing something, pulling my hair out on what it is. Thanks for confirming that its supposed to work though. – learningtocode Sep 21 '16 at 14:29

2 Answers2

0

You should annotate your controller with

@Controller

annotation

Janar
  • 2,623
  • 1
  • 22
  • 32
  • Hi Jana, Thank you for replying, but my controller is also a bean which i create along with adapter bean. I see other properties in controller being set ok. – learningtocode Sep 21 '16 at 14:17
  • Perhaps it has something to do with @Profile("!test") then, have you tried it without the annotation? – Janar Sep 21 '16 at 15:07
0

I have similar issue:

@Controller
public class MyController {

    @Autowired
    private LogService  logService;

    @RequestMapping(value = "/logs/view", method = RequestMethod.POST)
    private String getLogList(SearchLogBean searcher, BindingResult bindingResult, Model model) {

        List<LogActions> logs = logService.findAllByActionDateBetween(searcher.getFrom(), searcher.getTo());
        model.addAttribute("items", logs);
        return "logPage";
    }

}

In some other package i have the service

@Service
public class LogServiceImpl implements LogService {

   @Override
   public List<LogActions>  findAllByActionDateBetween(Date from, Date to) {
    ...

}

In controller getLogList autowired logService == null, in test it have been autowired successfully. Error is that the controller method is PRIVATE. This method have to be public. (Spring will bind to url annotated @RequestMapping private controller methods - https://github.com/spring-projects/spring-framework/issues/21417; but from the private methods we cannot use controller private fields (not only autowired, but, for example private Integer some_value = 12; will be null in such methods))