Showing posts from April, 2020

Spring DI with different styles

Spring as a dependency injection (DI) framework allows us to define our application components' dependency tree in three different ways: XML Annotation Java Config I've written a simple app, bookstore, with the three styles and they're available in the following repository. You can take a look and see how each style would look like. It also has a version that uses no Spring beans for comparison. Different styles have different pros/cons. Spring lets you mix the styles; so you can change it as you go. Here is my observations on each style. XML-based configuration Pros Weak/loose coupling with Spring framework in your app code. Good for keeping the option of removing Spring later Class dependency information is centralised. Fine-grained control on the dependency definition. Changing only the dependency information doesn't require the recompilation of your app code. Cons Unless you have a good IDE

Using JavaScript eco-system from Clojure/Scala/Kotlin/PureScript/TypeScript

I've been playing with Clojure for close to two months now. One thing I like about Clojure is that it seems like I'm typing fewer characters to get what I want. Less code to read and maintain. Once I rewrote one of my recent js script with Clojure and its size shrunk down to 2/3. Another nice thing is that it can leverage the large collection of software asset (i.e. libraries) that people have written for Java (JVM). But I noticed that I was reading more characters when checking API docs or library code for checking the use of a library. Wondered if I can save some time if use Clojure with JavaScript libraries. I also wondered how the experience would differ if I use other languages to do the same thing; so after I did it with Clojure, I did it with Scala, then PureScript (which works only with js), ... I've put them in one repository so that you can compare them yourselves, and see what can be involved if you choose one to run your code on JavaScript engines. There is

Clojure Way

Do I have to use partial to do currying? Yes. cf. Rich Hickey's reason for not auto-currying Clojure functions? Do I have to do an explicit null check? e.g. html-node below can be null . (-> html-node (.getTextNode) (.getLiteral)) Seems so. (if html-node (-> html-node (.getTextNode) (.getLiteral)) "") ; or (#(if % (-> % (.getTextNode) (.getLiteral)) "") html-node) cf. in Scala:"") Can I compose a function from a Java object method with only its method name? No. You may wrap with a lambda. cf. Usage of java methods as a function argument in Clojure (comp #(.getBar %) #(.getFoo %))

Use Blogger API from its Java client

Google Blogger provides an API to create and edit blog posts on Blogger. See their Introduction here. It also provides API client libraries for various languages including Java, JS, Python, etc. As I haven't found a sample app for the Java client, I'm writing this with the hope that this can save someone's time. In this post, I'm going to create a new blog post with a title "My Test Post" . Prerequisite You have a blog on Blogger (in this post, it's ) You have a GCP project with Billing enabled. You have gcloud command installed. cf. here . Steps Enable Blogger API Create OAuth 2.0 credentials Create OAuth consent screen Create a Java app that update your blog Get a new OAuth token Run the Java app to create a new post on your blog Step 1. Enable Blogger API Go to Blogger API page, make sure the correct project is selected, then enable the API.