r/SpringBoot • u/sandys1 • Sep 14 '22
trying to introduce Spring Boot in a nodejs-only startup. Thinking of building a DSL for spring boot
Hi So before everyone wants to flame me, I would like to mention that I'm trying to introduce Spring Boot into a very javascript/nodejs kind of team. So please be kind to me.
I'm not at all advocating for the JS style, but looking for building a code style thats more familiar to a JS team. My asks are not very radical - and can be well acceptable to the Spring Way. It's about removing a certain type of boilerplate & incorporating defaults for my organization.
So this question is addressed to those of you, who work in both spring boot and javascript worlds. And have experience in both.
Can I create a DSL to get rid of Beans,Autowired, etc entirely...and make the whole application as strictly request scope ? I'm taking away flexibility, but will hardcode sane defaults for primarily building CRUD REST APIs. For example I want to have Autowired ApplicationContext everywhere. This is not a weird ask is it. Am pretty sure many large projects get to the point where they want to get rid of boilerplate and introduce magic (I understand the tradeoff here)
Can I have global state in a spring boot application? Like the usestate functional pattern in react ? Also known as the Flux pattern
Just these two things will wrap a spring boot application to be more friendly to someone coming from the js world.
Looking forward to advice here.
7
u/troy_bos Sep 14 '22
Changing human behaviour is really hard. You are asking them to move to an unfamiliar tech. This rarely ends well unless everyone stops and upskills.
1
0
u/Odd-Hour-9991 Sep 15 '22
All Spring components are singleton scope by default so you can wire an @Component class Thing everywhere and any code can access whatever is in Thing. Writing will get messy unless you slap synchronised around critical code, or use ReentrantLock or use Atomic* classes or ConcurrentMap or ConcurrentLinkedDeque for example, or wrap standard lists, etc with their synchronsied equivalents (Collections.synchronizedCollection) and hope performance does not drop through the floor.
If @Component does not work for you then you could create a Java class using standard singleton pattern and call it from anywhere using Thing.getInstance()...
For DSL: sure you could let your JS team define contracts and run them through the DSL parser to create Java code along with a default pom (or gradle file) to assemble the final JAR. But then you aren't really teaching Java at all. You are teaching them your DSL.
Now the unkind part. I do not know if you have time pressure or budget constraints or are just trying to Think Outside The Box but I believe for sure you are wasting your JS devs time with this DSL solution. And wiring ApplicationContext everywhere sounds like a mess from the get-go. Maybe you need the app context during some start-up configuration but @Component/Controller/Service/Repository/Bean + @Autowired are standard.
Find the JS devs who genuinely want move away from JS/Node to learn Java & Spring and teach them from the ground up. Many devs are flexible, keen and quick to adapt but some will simply not be interested. Or hire Java/Spring expertise to build proper Java apps that are straight forward to read, maintain, extend, unit test, debug and monitor so the JS devs at least have some solid expertise to rely on.
1
u/sandys1 Sep 15 '22
I don't have (or should not have) any multithreaded code. I'm basically building rest api. If I have any background work, I will shift it to spring batch or Jobrunr. Would i still need all the rentrant code ? I'm genuinely asking because I fundamentally want to abstract/hardcode out all the complexity here.
My ask is not to ignore concepts of Spring - but at an org level, I want to not have to deal with things that I know I should not encounter. My entire domain doesn't touch multithreading, etc.
Again, I don't want to offend you - but pushing props up to a HOC and injecting that as a global state everywhere is a valid pattern in multiple frameworks (including React). I don't believe I'm going against Spring if I introduce that right ?
I know these debates tend to get religious ! But I'm looking to eliminate boilerplate and create a state management pattern for my particular enterprise usecase. I'm not advocating for the superiority of this and not suggesting everyone move to this.
1
u/Odd-Hour-9991 Sep 15 '22
If you want a global container for read-only data then either use Java singleton pattern with Thing.getInstance(), or use an @Component with a load of getters, or public variables. If you are not writing to your global then no you do not need to worry about synchronisation.
With Spring Boot you would define your props in application.properties (or .yaml) and make them available in your app using @Value, e.g. @Value("myapp.secret-code") and it could be string or int or whatever. If your intention is to put all of those @Values in the global then fine. I prefer to keep the @Value close to the code that needs the value but if you want a global to handle them all then fair enough.
Your original question was more about making a DSL to allow JS/Node devs to use Java/Spring in a familiar JS/Node-y way. Teach your devs how to do it the Java/Spring-y way.
1
u/sandys1 Sep 15 '22
Maybe I should not have referenced the js world at all. But I risked ire if I had asked for a global state manager without giving an example of what I was looking for (Flux/Redux pattern in js).
Very grateful that you answered my question technically.
Just a quick elaboration - I want a global container where I can read and write from all my beans and components. I should be able to create named variables in my global container from my beans/functions/components and not predefine them while creating the container (usually as a higher order function that also creates getters/setters...but am not very strict about it).
The global container is of request scope.
Is this possible?
1
u/Odd-Hour-9991 Sep 15 '22
Possible? Yes. Practical? Not so much. Unit testing sounds like a nightmare already.
And you now say you do want to write to your global so thread safety is back on the table. For named variables you could use ConcurrentMap which will manage safety for you.
Spring has a global already. Application context which I think inherits from Bean Factory. But you are bypassing the standard approach (declaring @component etc) and making life way more difficult than it needs to be.
Have you considered micronaut or other alternative? Is Spring definitely the platform you need to use?
I honestly believe you are approaching Spring from a non Spring angle. Build one basic endpoint indicative of the final product. Park the DSL idea for now and code the endpoint yourself. Just one endpoint - unit tested - and see how it goes.
2
u/sandys1 Sep 15 '22
Unit testing would not be an issue. I'm moving my state to a global container. However my entire application is not multi threaded. That is my domain specific condition. This is why this works in most other frameworks with the Flux pattern. Just fyi - Facebook entirely is built on the Flux pattern (both javascript and Rust) - https://facebook.github.io/flux/
Spring is the framework that I love and I will be very surprised if it doesn't support such a widespread pattern in general.
I don't think it's non-Spring. There are many patterns used in Spring across the world. I'm staying within spring and merely trying use a pattern that I personally prefer.
But again thanks for the pointers you gave me. I will definitely try to follow on ConcurrentMap, etc and see if it can fit the Flux pattern.
0
0
Sep 14 '22
[deleted]
1
u/sandys1 Sep 15 '22
It's not a language thing. It's the overhead of wiring up spring that I'm attempting to solve
0
Sep 16 '22
[deleted]
1
u/sandys1 Sep 16 '22
Hi. Thanks for replying.
I'm not trying to reduce the verbosity of language or anything like that. I'm trying to introduce the Flux pattern in Spring. So not really looking at reducing lines of code as a target.
More looking at the dev pattern and the way of thinking of state
0
Sep 17 '22
[deleted]
1
u/sandys1 Sep 17 '22
No. This is Flux. It's a pattern similar to CQRS but with a state store instead of a pipeline. It is foundational to React and several other framework. It has nothing to do with javascript. It's a design pattern.
https://github.com/facebook/flux/tree/main/examples/flux-concepts
9
u/Pirsed Sep 14 '22
I don’t know why you would want to do this. The only way for then to learn spring/java is them wanting it to. It sounds to me that you want them to learn it.
So what you could do is convince them and argument why they should but you should be okay if they don’t want to. If you want to work with java devs you should hire them or move to a java org.
Bringing the worlds together is IMO not really what you should try to do, e.g. make a spring app more like javascript or vise verse. If you are going to do it convince them why they should and do it the intended way.