Skip to content

Polish Spring Session auto-configuration #6648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

vpavic
Copy link
Contributor

@vpavic vpavic commented Aug 15, 2016

This PR replaces random @Autowired methods in Boot's subclasses of Session's configuration classes with constructor injection, therefore removing the need for @Autowired. In addition, configuration classes have been made protected to be consistent with most other equivalent inner @Configuration classes.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 15, 2016
@snicoll
Copy link
Member

snicoll commented Aug 15, 2016

Thanks for the PR but I don't like that. Such business shouldn't go in the constructor and @Autowired is not banned :) We just removed it when it wasn't needed, it's no reason to start moving stuff to the constructor that shouldn't go there in the first place.

@snicoll snicoll closed this Aug 15, 2016
@snicoll snicoll added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 15, 2016
@vpavic
Copy link
Contributor Author

vpavic commented Aug 15, 2016

OK, maybe property mapping shouldn't go in the constructor.

But for the sake of reference, shouldn't constructor injection be used to reflect the class's dependencies? Effectively SessionProperties is a dependency of Boot specific subclasses of Session's configuration.

Perhaps constructor + @PostConstruct would be the best approach for the situation?

@snicoll
Copy link
Member

snicoll commented Aug 15, 2016

method injection is not a bad thing either. If you had a single bean that uses SessionProperties you'd have a @Bean method that use method parameter injection rather than a constructor. This case isn't different and your proposal adds more lines with no benefit IMO.

@vpavic
Copy link
Contributor Author

vpavic commented Aug 15, 2016

I respect your opinion but I don't quite agree with it.

@Bean gives a method completely different semantics so it's IMO not the best comparison here.

SpringBootRedisHttpSessionConfiguration is already using @PostConstruct to perform additional logic that relies on SessionProperties (that is injected using a method named customize, which is inappropriate). If #6649 gets merged SpringBootJdbcHttpSessionConfiguration will also use the same approach.

Benefit is IMO a cleaner design that's more consistent with the rest of the code base (meaning I didn't find a similar usage of @Autowired in the entire spring-boot-autoconfigure module). A few more lines won't harm anyone :)

@snicoll
Copy link
Member

snicoll commented Aug 15, 2016

I think you're stuck on the "customize" part (an @Autowired method that does not start with "set") and there's really nothing wrong with that IMO. @Bean was just an example of a configuration class that has a single method: you're not going to inject the dependency in a constructor if only one method needs it and can get it from a method parameter.

SpringBootRedisHttpSessionConfiguration is doing the right thing: I need a callback to customize the configuration class I am extending from (that's not very typical in Spring Boot and I wouldn't have done that at all if I had the choice). The postconstruct is applying some validation once all the customizations on the configuration class haven been applied and that's IMO the right way to do that.

@vpavic vpavic deleted the polish-session-autoconfig branch August 15, 2016 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants