Integrating grails into an existing spring application? - java

What if you don't want to start a separate project for grails but instead sneak it into an existing webapp?
I have to build an admin interface/crud for some new entities and thought it would be a perfect way to learn grails.

I'm trying to make one application with a Grails app and a Spring app.
I've tried to sneak the Grails App into the Spring one, but this is "impossible". It's easier to sneak the Spring app into the Grails app. Grails knows what Spring is, but Spring has no idea of what Grails is.
In this article you can find useful information about how to use your hibernate mapping files or annotations in Grails, so you don't have to remap everything. Also you can use all your java clases (put them into src/java). You can put the beans defined in the ApplicationContext.xml in conf/spring/resources.xml. You can leave them in ApplicationContext, but I've had some problems.
I don't have ended the job (almost) and it looks good.

It would be hard to "sneak it in" unless the existing app has the correct dir structure that maps exactly to how grails likes it - after all, convention over config is where the power of grails comes from.
You can try doing the admin interface as a "seperate" app to the original/existing spring app, and map the existing database to the grails domain objects. though i m not sure how you would run them side by side easily without more information on the existing app. It is possible definitely though.

I agree that building your admin interface is a good exercise to learn Grails, and also agree with the previous answer that Grails is difficult if not impossible to integrate with an existing Spring application. You could probably get it done, but the headache would not be worth it.
Grails is built on top of Hibernate for its ORM, so if you're already using Hibernate with this Spring app you can work this to your advantage. It's not too difficult to configure a Grails app to use pre-existing Hibernate models, and this is explained well in Grails documentation.
So, I'd recommend building up your admin console as an independent Grails app but make use of the Hibernate models you already have, if in fact you've used Hibernate.

Related

Spring Data in non-Spring Project

I want to use Spring Data Cassandra in a non-Spring Project for Object Mapping. The project is not using Spring Boot or any other Spring component. Is this a good practice or I am doing it the wrong way?
P.S-Things are working fine but just wanna know If I'm on right track.
The usage of Spring Data in a non Spring project is frequent (mostly in old projects). The real question is, how to integrate it properly in your current architecture.
It's not a good or bad practice, it's a technical chose which must be in adequacy with your functional and technical requirements.

What are the disadvantages of using spring boot in production? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Spring boot is not adopted in our organisation.
The disadvantages cited are bloated jars, less control and flexibility.
I have tried to investigate above points but so far they dont seem valid.
bloated jars - The spring blog states that a simple web server
can be launched with 10 MB of heap.
less control and flexibility - Netflix uses spring
boot.Moreover,when I did some poc's, I didnt face flexibility issues.
I wanted to know if there are any mistakes in assesment.
1)Any examples on the loss of control/flexibility will be helpfull.
2)What are the anti patterns we need to be aware so the bloated jars issue dosent happen.
It is often a question of concern and responsability. With Spring boot, the developper team has full control of the deployed environment, including the servlet container configuration. And the production team has nothing to tweak.
Without Spring boot, the developper team has only control over the web application. And the production can/has to tweak the servlet container configuration. When you have a large number of different web apps, it can make sense to let the knowledge on that part only in the production team. In that case, you would not use Spring boot.
On the other end, if the hosting is externalized, Spring boot allows to give a full package.
So both approaches can be used but they target different organizations.
Bloated Jars - I think this can be fixed by properly maintaining the jars using a build tool like maven (But, yes there are some instances where you may need to add a lot of jars just because spring needs it)
Less control and flexiblity - I think this is usually with control freaks who want to control each and every piece of code they write. If you are okay with what spring provides already, this shouldn't be an issue.
I believe that the biggest disadvantage that you might encounter using spring is using it without understanding what value it might add to your project.
It might be completely not aligned with your requirements and it is possible that you will configure everything by yourself at some point, when you think, you should not have started with spring itself.
Ask your self a few questions,
You can create standalone java application? Why Spring at the first place? What value it adds to your project?
Spring has embedded tomcat, jetty, so no need to build a war. But, what if you have to build war anyway? Little config will do the trick but it's not any major advantage. Also, when you start it as a java service, what happens to your service if somehow the java process got killed?
What if you have many legacy spring modules? What if you need to patch it up? This will increase the jar count as well as legacy classes count. Are you sure want to convert this into a spring-boot application?
What if spring autoconfig configuration is not aligning with your requirements
And, I myself used spring-boot for a few of our production applications, which are mostly standalone REST API services and, haven't faced any issues with it.
Some best practices will be,
Use spring-boot mostly for microservices instead of a single complete (MVC) web application (I always use it to build the standalone REST API and build the UI with ReactJS and NodeJS).
Build your spring-boot app as a docker image and deploy using some kubernates cluster (kubernates will take care of failed docker containers and deploying to new containers) for maximum uptime.
Always keep only the jars required by your project and remove unwanted jars.
Any examples on the loss of control/flexibility will be helpfull.
You need familiarity on the life cycle of a bean in a Spring Boot application. Not knowing this might hinder your experience in spring boot. This will force you to structure your beans to prevent circular dependencies and other problems.
In Spring Security, configuring your other login methods is not really that intuitive.
In Spring Data JPA, repo classes requires you to name the methods in a specific way to perform queries. Locking/releasing data rows is also not straightforward.
etc...
What are the anti patterns we need to be aware so the bloated jars issue dosent happen.
Review the dependency tree of your jars. Make sure to add dependencies only in the modules they are needed. Also, remove dependencies you don't need.

Simple embedded database with spring

How to setup a simple embedded database in a spring(data)+maven project?
I need to develop a simple graphical application that read some data files and display pretty stuff about it interactively. The data is very repetitive with a little hierarchical structure. However I still don't know how I will need to access it.
For these reasons, I want to store it in a database so that I can later use DB query to access the data with query filter. (it also seems a good idea to develop a persistent layer)
Because it is for a little application, I want to use an in-memory DB.
I am quite new to java (using proper dev framework) and database. But I worked on a project using spring, spring-data, JPA, etc... I did not really understand how it worked internally and would not be able to setup it up, but I found it very practical.
Now, I found lots of docs and tutorial on internet about that, but I didn't understand enough to know how to adapt them to my need. What (I think) I want is:
to use maven+spring
spring data (I guess) to use Entity, JpaRepository and Autowired stuff
an independent program, thus starting from a Application.main method
as little and simple dependencies as possible
an embedded DB (+fast+light if possible)
genericity is nice
What I fill to be lost with are:
where should I put what properties/xml-declaration
how are all the dependencies working together (spring, spring-data, h2, hsqldb, ...)
I found this project https://github.com/wrpinheiro/spring-jpa-embedded-db that looks to fit, but:
there are way too many dependencies that (I think) I don't need, thus don't want
I don't know how to start a program with it
I don't get the org.springframework.stereotype.Service thing
nor the javax.inject.Inject
I think that if you look at this project you can start building what you need
http://spring.io/guides/gs/accessing-data-rest/#initial
Its maven (or gradle), has enbeded db, spring-jpa and runs as a jar that starts its own tomcat server (you can change it into a war build if you want)
Also you can use this service(?) that spring provides to create the starting build for your project:
http://start.spring.io
You provide them with what you want to build and then the code and required files are generated :D
Pretty neat.

Reverse engineering a java rest based services from database

Problem
I have a relational database schema (in Oracle, but could be in Mysql or Postgres) and I need to expose basic CRUD operations on tables with REST services, all withtout a line of code from a developer.
Constraint of my problems are: java 6/7 only (not scala, groovy, etc..), maven based solution, possibly framework agnostic (could be separated from Spring, for example).
A mandatory requirement is all of this stuff must run on Tomcat (6 or 7).
Bonus: junit of similar automated test on CRUD operation using Mocks and a webpage to test services
Possible solutions
I have already investigated into Spring Roo, not finding a decent configuration or tutorial for my problem.
I have tried to reverse-engineering database using jboss hibernate tools and fits well. Using this tool I can manage to do a maven module (a persistence jar) with all entities mapped.
But I also need a code organization using DAO pattern (to handle entities) and a service layer (to setup REST services).. and this seems to be tricky.
Edit: I've found this solution, using maven hbm2dao, I'm on right path?
Thanks for your time!
p.s: I've found this solution, seems good, but is made with python :(

Spring Security for Grails app using Spring MVC backend

I have a web app running with Grails looking after the view and controller and calling into a spring-mvc backend via the service layer (Grails app using services from spring-mvc backend). Now I need to implement Spring Security (SS) for the app.
Its at this point I'm not sure how to marry both. The backend already has a fully functioning spring security implementation but to get the Grails app to work I needed to exclude all transient spring security dependencies coming from the spring-mvc app and go with a purely Grails solution (maybe this was the wrong decision?). The backend already has User and Role objects complete with a user_roles table modelled via a joinTable annotation in the User domain.
Questions:
Am I on the right track? Ignore everything from java backend in terms of SS bar using its data objects and go with Grails impl with SS plugin? I've seen posts saying I don't even need to use the Grails plugin but they're quite old now and I'm not sure how I'd annotate my controllers and services.
If Grails is the way, I'm not sure what I need from by backend and what I need to reimplement/extend in Grails.
I have an SS extended User and UserDetailsService in the backend app but cannot use them as I've completely excluded all the SS jars that they depend on so I presume I have to roll my own?
So I implement a UserDetails object, a custom UserDetailsService (should this implement the GrailsUserService?) and expose the latter as a bean and everything should work? Wishful thinking maybe.
Any guidance, even at a high level would be much appreciated as I'm struggling to determine my approach at this point, never mind implementation.
I have managed to implement what I need to get this working. I have gone with the purely Grails approach and so have my own implementation of userDetails, an instance of which is returned by my custom userDetailsService.
One thing to look out for, if getting a proof of concept working, is to make sure that you are reading back your password in the format you are storing it in. Spring Sec will hash your password to compare against what is in your database and so you will not successfully load a user if your storing your password as plain text. One way around this is to use the following config in resources.groovy.
passwordEncoder(PlaintextPasswordEncoder)
But of course I would not advocate this as being the long term solution from a security point of view.

Categories

Resources