Montag, 9. Juni 2014

Hot Reload Support with Spring Loaded and JHipster

Yesterday I dry to get hot development support for my Spring Boot example application. For this I first tried Spring Loaded. Spring Loaded is a Java agent that transforms classes at loadtime to support a better 'hot code replacement'. Spring Loaded supports the following types of code changes to replace hot at runtime: add, modify and delete methods, fields or constructors. To use the Spring Loaded Java agent download the agent JAR from the project page. Then add following Java VM arguments to run your Spring Boot application:
-javaagent:${project_loc:name}/springloaded-1.2.0.RELEASE.jar -noverify
In Eclipse e.g. you can use ${project_loc:..} variable to get the absolute path of your project.

JHipster reload Controller

Spring Loaded e.g. does not support to add new Controller Class at runtime. For this I add the JHipster loaded core to my project. JHipster it self is a Framework that is build on Maven, Spring and AngularJS. JHipster promises to support full hot reload of Java and JavaScript code. The realod support of JHipster is based on Spring Loaded. To use the JHipster add-on for 'hot code replacement' the Spring Loaded agent must be setup. To activate the JHipster add-on I add the following Java VM parameters to start my Spring Boot example application:
-javaagent:${project_loc:simpleweb-springboot}/build/springloaded/springloaded-1.2.0.RELEASE.jar -noverify 
In my Spring Boot application I add the following Conditional spring configuration for the JHipster reload support:
With the JHipster extension now I could add new controller or service classes at runtime. But there are also restrictions I could not change the base class or the interface which a class implements. For this I must restart my application.

You find my Spring Boot demo application with the hot reload support on GitHub. I only tried the Demo with Eclipse but it should also work in other environments.

Sonntag, 16. März 2014

Spring Context the Grails Way

Since Spring Framework Version 4.0, there is a way to define a spring context in a groovy DSL, like the spring configuration can be done in Grails. A short description of this DSL can be found in the JavaDoc of the class GroovyBeanDefinitionReader and on the blog there is a good post that covers the DSL features.

Today I played around with the DSL, my context configuration covers a lot of the DSL feature and is shown in the listing.
If you like to use the groovy context in Spring JUnit test a ContextLoader must be defined, a simple test that uses the groovy context is show in the listing.
The whole demo project can be found on my github account here.

Montag, 10. März 2014

mapstruct - Java bean mappings

In the Java space there are a lot of tools for bean mappings, for example a common tool is Dozer. But the most tools I know, does a lot of magic, by using the Java reflection API, this is bad for debugging the bean mapping logic. Now there is a new and hot Java open source tool called  mapstruct. The project provides a simple way to map Java beans. By generating the mapping code from a Java interface with annotations. A simple mapping for a Car bean and a Engine bean  can be found in the code bellow.
Mapstruct also provides a simple way for own mappings, for this a mapper can be define by the uses attribute in the Mapper  annotation on the interface, the example use a mapper called PowerMapper. The implementation of this mapper  is shown in the code bellow.
How the generated bean mapper can be used is show in the following JUnit test.
More Details about mapstruct see the project documentation. I think mapstruct it is a very promising bean mapping tool, try it and leave some comments here.

Sonntag, 16. Februar 2014

Project Templates with Lazybones

Lazybones is a cool command line tool to manage project templates. The tool can be used for example to create a ratpack project. But you can also manage your own project templates, to create any kind of project with lazybones from your own template.

Install lazybones

Before you can use lazybones you should install it. To install lazybones you can use gvm the Groovy enVironment Manager.

gvm install lazybones

More options how to install lazybones can be found in the project README.

How to create your own template

It is easy to create an own project template. For your own templates project you should use the lazybones template. To create a simple templates project, the following steps should be done:
  1. Create a workspace folder for the template project
  2. Switch to the workspace folder
  3. Create the templates project from the lazybones template: 
    • lazybones create lazybones-project simple-templates-project
  4. Switch into the templates project: cd simple-templates-project
  5. Create a template in the templates folder:
    • mkdir templates/simple-groovy/
    • Add a VERSION file in the folder "templates/simple-groovy". The VERSION file should contain the template version.
    • Add a file in the same folder, the README file should contain the template project description.
    • Add a build.gradle file in the folder to build the template project.
  6. Now you can publish the template by running a gradle task. To install the template project local run: 
    • gradle installAllTemplate
  7. The new template now can be used from the lazybones command line tool:
    • lazybones create simple-groovy 0.0.1 my-app
More details how to publish the template project see the lazybones template development documentation.

Template Logic

If you need some user interaction or template logic add a "lazybones.groovy" file in the template project. Here simple "lazybones.groovy" script:

My simple templates demo project can be found here on github. More details about lazybones see the project documentation and the templates projects.

Dienstag, 15. Oktober 2013

How do you get the Bundle Class Loader?

The class loader of a bundle can be obtained through the BundleWiring interface. The BundleWiring interface has replaced the OSGi PackageAdmin service. The new interface can be used to get any information of the actual (dependencies) wiring of a bundle. And the new API also provides a method to get the class loader instance of a bundle.