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.