4.2 The Application Class - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.1.6
Table of Contents
4.2 The Application Class
Every new Grails application features anApplication
class witin the the grails-app/init
directory.The Application
class subclasses the GrailsAutoConfiguration class and features a static void main
method, meaning it can be run as a regular application.
4.2.1 Executing the Application Class
There are several ways to execute theApplication
class, if you are using an IDE then you can simply right click on the class and run it directly from your IDE which will start your Grails application.This is also useful for debugging since you can debug directly from the IDE without having to connect a remote debugger when using the run-app --debug-jvm
command from the command line.You can also package your application into a runnable WAR file, for example:$ grails package
$ java -jar build/libs/myapp-0.1.war
4.2.2 Customizing the Application Class
There are several ways in which you can customize theApplication
class.Customizing Scanning
By default Grails will scan all known source directories for controllers, domain class etc., however if there are packages in other JAR files you wish to scan you can do so by overriding thepackageNames()
method of the Application
class:class Application extends GrailsAutoConfiguration { @Override Collection<String> packageNames() { super.packageNames() + ['my.additional.package'] } … }
Registering Additional Beans
TheApplication
class can also be used as a source for Spring bean definitions, simply define a method annotated with the Bean and the returned object will become a Spring bean. The name of the method is used as the bean name:class Application extends GrailsAutoConfiguration { @Bean MyType myBean() { return new MyType() } … }
4.2.3 The Application LifeCycle
TheApplication
class also implements the GrailsApplicationLifeCycle interface which all plugins implement.This means that the Application
class can be used to perform the same functions as a plugin. You can override the regular plugins hooks such as doWithSpring
, doWithApplicationContext
and so on by overriding the appropriate method:class Application extends GrailsAutoConfiguration {
@Override
Closure doWithSpring() {
{->
mySpringBean(MyType)
}
} …
}