Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello Ninjas, Welcome back. You must have heard about Grails and Spring. Do you want to learn more about it? Don't worry we have you covered. In this article, we will learn about Grails and Spring.
Let's start off by learning about grails first.
What is Grails?
The web framework Grails, based on Groovy, enables rapid creation. And deployment of production-level applications. Under the hood. As a DMS, it merges several Java Framework, including Spring MVC, Hibernate, Ehcache, and Gradle.
Applications built using Grails can use third-party plugins, which greatly aid in solving a variety of problems. The Grails Plugins Portal is where you may find the plugins above.
What is Spring Boot?
In the past, setting up the project required a lot of effort when a developer needed to use Spring to create an application. Developers of frameworks were aware of this problem and had designed Spring Boot as a solution.
The bulk of the setting process rests on the shoulders of this project. It uses the class path libraries to find what and how things should operate. If the app developer is not okay with these settings, it is pretty simple to modify it here to meet the project's needs.
You may create web applications by including a few dependencies in the pom.xml/build.Gradle file. And add a public static void main() function to the app's main class.
If you are looking to learn advanced front-end web development, You can also consider our Spring Boot Course to give your career an edge over others.
Foundation of Grails
In reality, Grails covert Spring MVC application. The built-in MVC web application framework for the Spring framework is called Spring MVC. Although Spring MVC has the same issues with frameworks like Struts, it is well-planned, making it the ideal framework for Grails to build another framework on top.
In the following areas, Grails uses Spring MVC.
Grails subclasses Spring's Dispatcher Servlet:- and utilises it to delegate to Grails controllers for basic controller logic.
Data Binding and Validation:- Spring's validation and data binding features provide the foundation for Grails.
Runtime configuration:- A Spring Application Context connects the various components of Grails' runtime convention-based framework.
Transactions:- Grails makes advantage of GORM's Spring transaction management.
In other words, Spring is integrated across the entirety of Grails.
Configuring Additional Beans
BY Spring Bean DSL
Beans are defined inside a bean's property. And you can quickly register new beans by defining them in the Grails Spring DSL-based file groovy.
beans = {
// Now here, you may add beans.
}
For a simple example, configure a bean using the following syntax.
import my.company.MyBeanImpl
beans = {
myBean(MyBeanImpl) {
someProperty = “any number you would like to assign”
otherProperty = "any colour you would like to assign"
}
}
By declaring a public field with the name of your bean, the bean can be automatically wired into Grails. And other classes that support dependency injection once it has been configured:
class ExampleController {
def myBean
...
}
The benefit of using the DSL is that bean declarations and logic can be combined, for instance, dependent on the environment:
import grails.util.Environment
import my.company.mock.MockImpl
import my.company.MyBeanImpl
beans = {
switch(Environment.current) {
case Environment.PRODUCTION:
myBean(MyBeanImpl) {
someProperty = any number you would like to assign
otherProperty = "any colour you would like to assign"
}
break
case Environment.DEVELOPMENT:
myBean(MockImpl) {
someProperty = any number you would like to assign
otherProperty = "any colour you would like to assign"
}
break
}
}
With the application variable, the Grails Application object can be accessed and used, among other things, to access the Grails configuration:
import grails.util.Environment
import my.company.mock.MockImpl
import my.company.MyBeanImpl
beans = {
if (application.config.my.company.mockService) {
myBean(MockImpl) {
someProperty = any number you would like to assign
otherProperty = "any colour you would like to assign"
}
} else {
myBean(MyBeanImpl) {
someProperty = any number you would like to assign
otherProperty = "any colour you would like to assign"
}
}
}
Using XML
Also, beans may be set up using a grails-app/conf/spring/resources.xml file. In old versions of Grails, the run-app script automatically created this file for you. Still, since the DSL in resources.groovy is now the better method. It is no longer generated automatically. But it is still supported - you need to make it yourself.
The Spring documentation gives a great reference for configuring Spring beans. And this file is a standard Spring XML file.
The syntax used to configure the myBean bean using the DSL would be in the XML file.
It may be automatically plugged into any class that supports dependency injection, much as the other bean.
class ExampleController {
def myBean
}
Runtime Spring with the Beans DSL
With Spring at its foundation, this Bean builder for Grails seeks to offer a more simple method of connecting dependencies.
Aside from programmatic XML creation, which is laborious and prone to errors. Spring's standard method of configuration is static and challenging to change. And adjust at runtime. With the ability to connect components at runtime, Grails' Bean Builder alters all that by enabling you to modify the logic in response to system characteristics or environmental variables.
This prevents needless code duplication and allows the programme to adapt to its environment.
BeanBuilder class
A class is called grails.spring.BeanBuilder from Grails allows you to create bean definitions using dynamic Groovy. These are the fundamentals:
The configuration of Hibernate with a data source using the BeanBuilder class is demonstrated in this example.
Each method call in this example corresponds to a bean name in Spring. The method's class is passed as the first argument. And a block is passed as the final argument. Standard Groovy syntax can be used to set the bean's properties within the block's body.
With the help of the bean's name, bean references are immediately resolved. This is evident in how the session Factory bean resolves the data Source reference in the above example.
Also, as seen in the following code, the builder can set specific special properties associated with bean management:
sessionFactory(ConfigurableLocalSessionFactoryBean) { bean ->
/* Autowiring behaviour. The other option is 'byType'. <<autowire>>. */
bean.autowire = 'byName'
/* Sets the initialisation method to 'init'. [init-method].*/
bean.initMethod = 'init'
/* Sets the destruction method to 'destroy'. [destroy-method].*/
bean.destroyMethod = 'destroy'
/* Sets the scope of the bean. <<scope>>.*/
bean.scope = 'request'
dataSource = ref('dataSource')
hibernateProperties = ["hibernate.hbm2ddl.auto": "create-drop","hibernate.show_sql": "true"]
}
The strings in square brackets in Spring's XML declaration stand in for the names of the comparable bean attributes.
Frequently Asked Questions
What is the use of Grails?
Web applications can be quickly created with Grails thanks to its scaffolding features. And enables quick project creation. Grails is based on the convention over setting idea, allowing the programme to auto-wire based on naming schemes.
Which is preferable Grails or Spring?
Reviewers found Grails to be generally easier to use, set up, and conduct business with when comparing the two options. Reviewers, however, preferred Spring Framework's simplicity of management. Reviewers believed that Spring Framework and Grails both better fit the needs of their businesses.
Does grails comes with Groovy?
Grails is a Java-based web application framework that uses the Apache Groovy programming language. The Groovy scripting language uses dot-separated notation to make coding simpler.
How do you build a grails project?
Use the Grails Application Forge to create your Grails project by going to start.grails.org. After selecting your project type, Grails version, and Profile, click "Generate Project" to receive a ZIP file. There is no need to install Grails!
Do grails uses Spring?
The usage of Spring's transaction management by Grails is arguably the most significant. In addition, many plugins exploit the Spring integration offered by Java libraries, as I've already hinted at. Grails applications are actually Spring applications, therefore.
Conclusion
In this article, we have learnt about the Grails and Spring Framework. And Configuring Additional Beans and understanding Runtime Spring with the Beans DSL. If you want to learn spring boot click the below links.