4.2 The Application Class 6.0.0

(Quick Reference) # 4.2 The Application Class Version: 6.0.0 ## Table of Contents ## 4.2 The Application Class Every new Grails application features an `Application` class within 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 the `Application` 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
This is useful if you plan to deploy your application using a container-less approach. ## 4.2.2 Customizing the Application Class There are several ways in which you can customize the `Application` 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 the `packageNames()` method of the `Application` class:
    class Application extends GrailsAutoConfiguration {
        @Override
        Collection<String> packageNames() {
            super.packageNames() + ['my.additional.package']
        }

        ...
    }
### Registering Additional Beans The `Application` 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 The `Application` 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)
            }
        }

        ...
    }
| Quick Reference (hide)