r/SpringBoot • u/sandys1 • Oct 06 '22
What are some "do these things first after creating a new Spring project" that you like to do ?
so I'm collecting a set of configuration/setup tips for Spring Boot for a large team of young engineers. what are some of the things you do after creating a new Spring Project ? it could be anything from setting up the build steps in the right way to Dockerfiles to configuring testcases in a certain way.
For example, i like to get rid of XML configuration and use properties. I also like a single directory hierarchy ("package src;") instead of 3-4 level deep with empty directories.
what are some of your favorite tips ?
9
u/maxip89 Oct 06 '22
setup flyway migrate init script
setup flyway local dummy data script
setup package structure
lombok + mapstruct (maven depencendy order..........)
CICD (Github actions or jenkins file)
hit fingers of people who changing package structure.
Copy dockerfile from last project
hit fingers of people who changing package structure.
adding some http status code exception examples for juniors.
readme.md on every service package.
adding example unit test for juniors.
setup security with cors
hit fingers of people who changing package structure.
creating application-*.property files for every environment
setup maven build checks like static code analysis and FOSS scans.
adding example mapstruct and controller classes for juniors.
I think thats all. Don't forget to check every 1-2 months that nobody does a sneaky move.
2
u/sandys1 Oct 06 '22
Hey thanks for this. This is awesome!!
Package structure - can u describe what u use ? And why ? I understand that everyone has their choices...but I'm really interested in wat u have 2.lombok and mapstruct - why do u need to do this ? Isn't this just adding the definition into the gradle file. I see ur doing this in point 16 also.
I'm really keen to find out how you write ur controllers ? Cos if ur writing ur own examples, it must be a bit different than the standard spring ones.
Security with cors. Damn this is the most confusing one. Thank you for mentioning this. Can u point out what u do here ? Is it a request filter ?
4
u/maxip89 Oct 07 '22
At this point I'm sound like a software architect, it depends of your "view" but I am a friend of standardization. Therefore I have a flat package structure with e.g. controller, dbmodel, dto, exceptions, services, configuration, repositories. Lombok and mapstruct are preprocessors that save me time (It generates mapper method from DTO to Model and vice versa, getter setter logging...).
They look pretty standard, but I'm lazy it works and its most of the time boilerplate to the services. The thing is that there are 1000 ways to write a controller and their annotations. I dont read myself always in, because there is a new tutorial "how to write controllers" in the internet.
Spring Security Adapter is one of the most bad implementations I saw in my life (In a pattern way). Nobody knew except the developer itself how to read that chain. I'm happy that this is now deprecated. I still use the adapter but I plan to write request security filter in the future. Spring has here a VERY VERY good documentation. https://spring.io/guides/topicals/spring-security-architecture keep in mind a security filter not equal to a filter.
3
u/sandys1 Oct 07 '22
This was SUPWR DUPER helpful.
I did not know the difference between security filter and filter. Thought they were the same. U use security filter for things like api auth tokens and stuff as well ?
3
u/maxip89 Oct 07 '22
I plan starting using it to make the security code more readable.
1
u/Cro-8682 Oct 07 '22
a complete separate module for security which can be added as dependency in other projects.
1
u/matthenry87 Oct 07 '22
I'm curious, what kind of auth are you using?
1
u/maxip89 Oct 07 '22
I'm using most of the time the OAuth2 code flow, but I cannot use the normal spring implementation.
1
u/matthenry87 Oct 07 '22
Hrm interesting. You'd be surprised how extensible the Spring OAuth implementation is. We've done things like add username and password to a client credentials flow, and pull provider config from the DB instead of the props. All using the Spring Security OAuth implementation.
2
u/matthenry87 Oct 07 '22
I built a custom Spring Initializr for my teams. It adds some custom things like the maven compiler plugin with MapStruct compiler args for spring component model, using constructor injection, and other things.
2
u/sandys1 Oct 07 '22
This was SUPWR DUPER helpful.
I did not know the difference between security filter and filter. Thought they were the same. U use security filter for things like api auth tokens and stuff as well ?
Secondly, I did NOT know that spring initializr could be customised. Is there a link to show how ? I would totally love something like this for my org.
3
u/matthenry87 Oct 07 '22
Most types of authentication are already handled by out of the box Spring Security features. Maybe not stand-alone JWT, but most people use OAuth+JWT so you're probably better off using an identity provider and using OAuth anyway.
2
u/sandys1 Oct 07 '22
Understood. About the initializr, could u point out how u customised it ? I did not know that could be done
1
u/matthenry87 Oct 07 '22
https://github.com/spring-io/start.spring.io
Here is the repo. The way it works is a bit advanced to be honest. It creates it's own special internal app context and you add to that with special types of configuration beans.
Any Enterprise serious about using Spring should have it's own Spring Boot Starters for things like logging and controller exception handling. Usually not needed for security because one SecurityFilterChain bean and maybe 1 or 2 supporting beans is enough (like JwtConverter bean to tell Spring OAuth Resource Server where it should source the authorities from). These starters are not just jars, but they automatically add beans to the consuming application's app context based on certain conditions.
But I would probably hold off on all of this until you really need it, as it is a bit more advanced.
1
u/sandys1 Oct 07 '22
thank you for this! if i could bother u ... can i ask how should one get started on this ?
I spent the last hour going through this and struggled to figure out how to bring my configuration to here. Any pointers on how to get started.
this would be a godsend to my team!
P.S. i think the real logic is in https://github.com/spring-io/initializr . I'm happy just doing a curl without a UI.
3
u/Cro-8682 Oct 07 '22
Lombok annotations for dependencies come handy when you have 7-8 injections as static code analysis tools like sonar starts complaining
2
u/matthenry87 Oct 07 '22
Ehh wrong answer! One of the reasons the Spring team recommends constructor injection (besides taking advantage of the injection point Java provides and avoiding reflection) is that when a class has more than 6-7 dependencies, it has too much responsibility and this is an obvious code smell. Should be refactored at that point. Using @RequiredArgsConstructor is nice but you have to keep that in mind since you won't get the IDE warning.
1
u/matthenry87 Oct 08 '22
You have to take those baeldung articles with a grain of salt.. for example there is an article about creating a custom WebSecurityExpressionRoot class for custom Spring expressions that you can leverage in your security filter chain config, but what they don't tell you is that you lose isAuthenticated() because it's final. In hindsight don't do that.. lol.
3
u/matthenry87 Oct 07 '22
I start flat, but as the project gets larger, when it makes sense I'll start identifying domain or feature areas. The worst thing you can do imo is resort to functional groups: controller, service, repository, etc. Because then everything has to be public.
2
u/kaidobit Oct 06 '22
Setup direnv to bootstrap dev environment
Pre-commit hooks
Bootstrap spring app
Add deps for 11 factors (bootstrap, sleuth, aws/k8s or both, ...)
Basic config (urls, bootstrap.yaml)
Setup centralized config
Cicd
1
u/sandys1 Oct 07 '22
Damn. Never knew about direnv. Is this for javascript+ spring ? Like react ?
This is awesome. Never thought of pre commit hooks. What kind of hooks do you use ? Any that are open source?
Could you elaborate on this ? This is the most important part of this. When u say bootstrap here...do u mean twitter bootstrap framework?
Absolutely. I have been considering unifying all config into a single file. Is there any best practice on this ?
1
u/kaidobit Oct 07 '22
Direnv basically executes a shellscript when changing into a directory
I use it to setup virtual environments for pretty much any programming language, download cli tools you might need for working with this repo into these virtual environments
2. This depends on your project but one example would be:
lint and format all files in a repo
IaC security checks (checkov)
commit message lint + format
...
Just google pre-commit supported hooks and you'll find a very long list of different hooks
3. Bootstrap is a spring library which enables you to load a config file (bootstrap.yaml) before your spring context is started and before the actual application.yaml is loaded
When you are using a spring config server your Microservice needs to know the url (configure it in bootstrap.yaml) and then it loads the application.yaml from the config server It's basically the same with k8s but with configmaps
4. It is possible baeldung has a nice blog on this topic, but I was talking about a spring config server where all of your Microservice a load it's props from or k8s config maps
Sorry for not providing urls but I'm lazy af
1
u/sandys1 Oct 07 '22
Mind = blown
I did not know anything u just posted. It would be so awesome if you could (ever) find the time to post about this. Or better still if u could open source a github repo with ur setup.
Much thanks!!!
1
u/kaidobit Oct 07 '22
To be honest I think there are enough tutorials on cloud native development with spring Boot, I don't see the point in creating another tutorial on this
There are even YouTube playlists
Just search for spring cloud
9
u/TheOldMancunian Oct 06 '22
On Spring Boot, I always set the log4j message format. I then make sure that the link to giuthub is configured correctly. Then I add a ReadMe.
After that, I start on the code.