Despite the fact that this blog named "Software Development" and intended to give information about programming tips and tricks most of the posts are about PostgreSQL, mainly because there were just scarce articles for the latter and heaps of articles in the Internet related to what I usually do. Now it's time to fix it! Let me share with you some knowledge I got creating my own project glueing its pieces with Groovy and Guice and I hope you might find something useful here.
Let me first describe in brief what are the technologies we are going to work with. Groovy is a dynamic JVM based programming language which can be easily integrated with any Java project, mostly for the reason it almost completely supports Java syntax. Simply changing file extension from *.java to *.groovy whenever we want we're able to start using Groovy way to code - without a lot of boilerplate we have to write programming on Java. In other words Groovy allows us to express programming ideas using more concise and descriptive code and, at the same time, to rely on the mature JVM platform with uncountable amount of useful libraries, one them we're going to look at. Guice is a Google's lightweight framework to bring dependency injection into your project. Despite Spring which is comprehensive library with lots of ideas involved Guice gives gives pure dependency injection in a concise, explicit and maintainable way.
Let me first describe in brief what are the technologies we are going to work with. Groovy is a dynamic JVM based programming language which can be easily integrated with any Java project, mostly for the reason it almost completely supports Java syntax. Simply changing file extension from *.java to *.groovy whenever we want we're able to start using Groovy way to code - without a lot of boilerplate we have to write programming on Java. In other words Groovy allows us to express programming ideas using more concise and descriptive code and, at the same time, to rely on the mature JVM platform with uncountable amount of useful libraries, one them we're going to look at. Guice is a Google's lightweight framework to bring dependency injection into your project. Despite Spring which is comprehensive library with lots of ideas involved Guice gives gives pure dependency injection in a concise, explicit and maintainable way.
Well, let's take an example from Guice documentation and go through it removing unnecessary boilerplate.
// skipped imports public class MyModule extends AbstractModule { public static void main(String[] args) { Injector injector = Guice.createInjector(new MyModule()); BillingService billingService = injector.getInstance(BillingService.class); } protected void configure() { bind(Service.class).to(ServiceImpl.class).in(Singleton.class); bind(CreditCardPaymentService.class); bind(PaymentService.class).to(CreditCardPaymentService.class); bindConstant().annotatedWith(Names.named("port")).to(8080); } }For most of Java developers this code looks familiar and something we're are used to. Although with the help of Groovy we can make it brief but still comprehensive.
// skipped imports class MyModule extends AbstractModule { static main(args) { def injector = Guice.createInjector(new MyModule()) def billingService = injector.getInstance BillingService } protected void configure() { bind Service to ServiceImpl in Singleton bind CreditCardPaymentService bind PaymentService to CreditCardPaymentService bindConstant() annotatedWith Names.named("port") to 8080 } }Now we have code that just expresses what we want to do without excessive syntax. There are still dots and parentheses we can't get rid of, however, it is not that much of them. As every dynamic language Groovy has pros and cons and let's talk about cons we have it using it for dependency injection configuration. What brings attention in first regard is that we can plainly misspell our function name or, more likely, class name and get something like 'bind Servic' without any warning or error and get aware of it once we start our application. What can we do to decrease such an error-prone behavior which is a big deal in terms of configuration. Luckily, Groovy has two answers for it - @CompileStatic and @TypeChecked annotations.
@CompileStatic @TypeChecked class MyModule extends AbstractModule {}It's enough just to put either of them before class declaration. @TypeChecked annotation performs only static type checking during compilation but still keeps dynamic behavior in place. @CompileStatic annotation is more stricter, it forces Groovy compiler using static type checking during bytecode generation, which drastically improves performance eliminating dynamic behavior though. Since Guice is positioned as fast framework it is better use @CompileStatic in injector modules defined as Groovy classes.
No comments:
Post a Comment