, ,

Polyglot Programming

Is the practice of programming in multiple programming languages. According to Wikipedia it is –

In computing, a polyglot is a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independent of the programming language used to compile or interpret it.

In writing the code for a recent blog post on Spring Integration, I thought it would be fun to write some of the code in Groovy. I’ve been writing in Groovy for about 7 years. I’m as fluent in Groovy as I am in Java, so its no problem for me to bounce between the two languages. If you been following my blog, you’ve probably seen me use Spock for my unit tests. Spock is a great tool. A Groovy testing framework for testing Java code.

Many Java developers who are not familiar with Groovy, view it as just a scripting language. Groovy is not just a scripting language. It is an Object Oriented Programming language, just like Java is. Some may argue that Groovy is a pure object oriented language and Java is not because of Java’s support of Primitive Types. Because unlike Java, Groovy does autoboxing of primitive types.

Its also important to understand, Groovy was never intended to be a replacement of Java. It’s written to supplement Java. As such, its very easy to mix and match code. Ultimately both Groovy and Java compile down to JVM byte code, and the compiled code is compatible.

Polyglot Programming in the Spring Framework

The Spring Framework is no stranger to Polyglot programming. The Grails community really spearheaded things with the Groovy language. In version 4 of the Spring Framework, we’re seeing a lot more support of polyglot programming around the Groovy language. But in the various Spring Framework projects we’re seeing more polyglot programming support around the Scala programming language. With Rod Johnson involved with Typesafe, I think it’s a safe bet we will see additional support of Scala in Spring in the future.

Groovy Spring Beans

It is possible to write Spring Beans in Groovy. Your application will compile and run just fine. Whenever programming for Dependency Injection, I recommend developing your code to an interface.

AddressService

When doing polyglot programming, I prefer to write the interface in Java. Now any class, written in Java or Groovy can implement the interface. You probably could write the interface in Groovy and use it in Java just fine, but there are some pitfalls of using Groovy from Java you need to be aware of. For example if you use ‘ def ‘ as a type, Java treats it as a Object  data type. Sometimes the strong typing of Java is not a bad thing. This also seems more appropriate to me when defining the interface to use.

AddressServiceImpl.groovy

You can see my Groovy class simply implements the AddressService  interface. I mark the class with the @Service("addressService")  annotation as I would a normal Java Spring Bean. 

Using a link below, you can checkout the full project. You will see the Groovy class compiles with the Java code and runs in the Spring context like any other Spring Bean.

Conclusion

In enterprise Java / Spring shops, you probably will not see much Polyglot programming. But, the Grails team uses Groovy for Spring Beans all the time. I’ve demonstrated its easy to use Groovy Spring beans outside of the Grails environment. This is not a technology issue to overcome. Culturally, I suspect it may be some time before you see polyglot programming in large scale enterprise applications. But doing polyglot programming like this is something fun to do in blog posts!

Get The Code

I’ve committed the source code for this post to github. It is a Maven project which you can download and build. If you wish to learn more about the Spring Framework, I have a free introduction to Spring tutorial. You can sign up for this tutorial in the section below.

Source Code

The source code for this post is available on github. You can download it here.

0
Share
,

There are several ways of defining Spring Beans in the Spring Framework. Each method has its own advantages and disadvantages. You may be wondering which one should you use. The answer is, it depends. The reality is in a large enterprise Spring project, you will likely encounter a combination of methods in defining Spring Beans. Neither is wrong, nor necessarily better. In legacy Spring applications, you’re likely to encounter XML based Spring Configuration. In new Spring applications, you’re likely to encounter more annotation based configurations and Java based configurations.

In this post, I’ll step you through common ways of defining Spring Beans. At the end of the post, I’ll show you how the different methods work seamlessly.

Say Hello Spring Bean

All the examples in this post will implement the following Interface. We will use simple Hello World type beans which will print a message to the system console.

Spring Configuration

XML Based Configuration

XML based Spring Configuration has been around since the beginning of Spring. It is the oldest and most mature method of defining Spring Beans.

Hello Spring Bean

Here is the Spring Bean we will use for this example.

Spring XML Configuration

Application

The following code can be used to execute the example Spring Bean.

Output

Java Based Configuration

In Spring 3, Java based configuration was released. The Spring community is gradually converting over to prefer the use of Java based configuration over XML based configuration. It is somewhat easier to use than XML. Since the configuration is in Java, you do have a tremendous amount of flexibility in defining beans. You’re no longer bound by the constraints of describing beans in XML, and have the full power of the Java language at your disposal.

Hello Spring Bean

Java Configuration Class

The following class is a Spring Configuration class. The annotation @Configuration  at the top of the class defines the class as a configuration class. The class will be picked up in a Spring component scan. It can also be imported into other configuration classes too. Objects returned  @Bean  annotation will be registered as Spring Beans. By convention the name of the registered Spring Bean will be set equal to the name of the method.  

Application

The following code can be used to execute the example Spring Bean.

Output

Annotation Based Configuration

Spring also can detect beans based on class level annotations. Any class annotated with with @Component , @Service , @Controller , or @Repository  will be considered a Spring Bean. The class must reside in a package which had been identified for a component scan. Spring will scan the package for annotated classes. Any class detected will be managed by the Spring Context at run time.

Hello Spring Bean

Application

The following code can be used to execute the example Spring Bean.

Output

Using Spring Bean Configurations Together

While there are different ways of defining Spring Beans, its important to remember the different methods can be used together. Its very common to use multiple methods of defining Spring Beans in a single application.

Spring Bean Application

We can use the beans previously defined to demonstrate how this works seamlessly. The Spring component scan functionality will detect classes annotated as Spring Beans, and classes marked as configuration classes which also define Spring Beans. In the following code example, the annotation @ComponentScan("guru.springframework") tells Spring to perform a component scan on the package guru.springframework.

This technique will work for annotated classes, but it not pick up XML configuration files. The annotation @ImportResource("classpath*:spring/spring-config.xml") will bring the specified xml file into the Spring context.

The class below is annotated to perform the component scan, which will pick up Spring Beans defined in Java configuration classes, Spring Beans defined with annotations, and Spring Beans defined in XML configuration files.

Output

When you run the above class, you will see the following output.

Get The Code

I’ve committed the source code for this post to github. It is a Maven project which you can download and build. If you wish to learn more about the Spring Framework, I have a free introduction to Spring tutorial. You can sign up for this tutorial in the section below.

Source Code

The source code for this post is available on github. You can download it here.

Video Code Review

I’ve posted a code review of this post on YouTube. In the review I step through the classes we used in this post.

2
Share