Category Archives: java

Is Spring Python a framework or a library?

I was tracking some comments, and recently noticed a thread of discussion (in English) on a python mailing list. This is a follow-up to someone else posting about his discovery of Spring Python through the news announcement from SpringSource. (I appreciated that the person also caught on to my article defending the usage of AOP in python.)

They concluded their response by indicating:

Aunque para convencerme de usarlo todavía le falta compararse con otros frameworks de python.

(Run this through Google Translate if you need to.) Apparently, this person, while slightly impressed that I wasn’t coding in horrible java paradigms, didn’t see enough to use this instead of other python frameworks.  I knew that he was comparing it to other python web frameworks.

History Lesson

I started thinking about the basis of all this, and realized a little bit of a history lesson is in order. Anybody who has ever heard of Spring, knows it by its full name, The Spring Framework. This should help illuminate why Spring is referred to as a framework, and why Spring Python is often thought of as a framework itself.

Starting around 1999, the first EJB spec came out. This was the technology everyone in the java industry started using, and people were throwing together any and all java web apps using EJBs. It caused a resurgance in the value of java, and arguably catapulted java into the enterprise space. EJBs promised much. The application servers that would run your EJB-based app had many features, like transactions, security, failover, messaging, and web services to name a few. All you had to do was extend a few interfaces, write a few configuration files, and do a little configuration to the app server.

Well, that was how it was supposed to be, right? The truth was, EJB apps required lots of XML file configuration. Each container had deviations, so your app tended to be container-specific. People usually had too many services turned on, and unit testing was next to impossible. You couldn’t test your app outside the container, so turn around time to see your changes was a nightmare. That is what generated the flurry of frameworks, like Struts, to help make EJB app development simpler. By extending these framework’s classes, you had a little bit less to extend, less to write, and less to configure. But you still had to do it anyway. And that turn around time to test your app? No better. And you had to make your WHOLE app do this. There was no such thing as, “Part of my app is using Struts, another part is using Wicket.”

Then in 2002, Rod Johnson, a java software consultant, wrote a book Expert One-on-One J2EE Development without EJB. When Rod’s book hit the shelves, the message he was sending was completely unorthodox. He showed tactics of code development that quickly made these container-based services obsolete. You didn’t have to extend anyone’s classes. And…you didn’t have to adopt any framework lock-stock-and-barrel. Instead, your app could be 99% based on POJOs. The code he was writing for examples in the book was very pragmatic, and was just like the other frameworks, only it was a framework based on POJOs. Another consultant approached Rod about releasing that software as an open source project, and thus was born The Spring Framework.

This code, while acting similar in output as other frameworks, had a peculiar difference: it did not require actually extending anybody’s classes. In fact, you could write your code 99% framework-free, and Rod & Co. encouraged this.

“So…is Spring a framework or a library?”

I argue: both!

Considering that a library is a set of functions and classes you download and install in your software development toolbox, Spring fits this perfectly. It was inspired by the need to overcome the lethargic development process of the EJB spec. Not only did they improve over EJB’s way of doing things, they also enhanced JDBC, JDNI, JMX, and other things.

  • Ben Alex decided to develop a Security library based on the pragmatic concepts behind Spring, which has since become the most widely adopted security solution for java.
  • Mark Pollack was also watching Spring cut through the java industry and coded a similar solution for C#/.NET and called it Spring .NET.
  • …and the list goes on of other widely adopted libraries/tools developed for the java industry based on Spring’s approach to solving problems

So, yes, Spring is a library. But it is ALSO a framework, in that its original intention was to faciliate building enterprise-grade web applications inside EJB containers. They simply showed that the servlet container was enough. In fact, many shops started fresh adopted the concept of using Apache Tomcat or Jetty as the servlet container, which has been challening the commerical app server market. Combined with the fact that your app could run inside and outside an EJB container, and also be easily JUnit-tested without the slow make-deploy cycle made it a hit.

And…its NOT a framework!

“But, you just SAID Spring is a framework!”

Yeah, I know I did, but many people interpret framework as code meant to be extended. Spring doesn’t require this. It may accomplish the same end result as these other EJB frameworks, but through simpler means. I know this first hand, because I have developed thick Java client apps using Swing. No EJB container to be found! And yet, I have found Spring provided much leverage and functionality. I didn’t have to extend anything. Okay…I had a unique situation involving security where I needed to extend a Spring Security interface, but 99% of my code doesn’t do that. And that’s the point.

“Enough Java already! I thought this was a Python blog!”

Okay, if you’re still reading, it means you are really a cross technology buff. Or maybe you just read blogs instead of sword fighting while waiting while waiting for your code to compile. Otherwise, you freaked that I spent so much time talking about Java, XML, EJBS, and fled the scene. Well, I’m a polyglot programmer. I use all kinds of stuff: C/C++, Java, Python, Tcl/Tk/Expect, PERL, bash/csh, jython, scala, groovy, clojure, anything! Actually, I had never heard the term until I saw Russ’s Skills Matter presentation where he presented himself as an “self professed, unapologetic, polyglot programmer”. But I immediately connected with it.

Anyway, the point I’m trying to make is that the concepts Rod presented are of incredible value. Its not the fact that he was working in Java, but instead the idea that pragmatism is valuable. Instead of always resting comfortably against specs, we need to always be on the lookout for places and paradigms where we are wasting countless cycles in coding and re-coding the same thing. I contend that repetition is one of the top ranking source of bugs, if not the highest.

I believe Spring Python is a library

My first entry into the world of Spring was through their fabulous JdbcTemplate. Writing SQL queries without leaking cursors and connections was of high important to me. When I decided to code  a carputer application, I picked Python because reading Dive Into Python had sold me on the beauty of it. However, I quickly realized I was missing my lean-and-mean JdbcTemplate. I started coding a DatabaseTemplate to help me, and realized I was starting to do something just like Rod. Well, I decided then and there to share my library with anyone who was interested. Hope it helps you like it has helped me.

I think, and it’s not a mockery, that the Java world is rather a framework type of environment which leads usually to have as many frameworks as you have developers for the very reason I described above. Python has also its share of frameworks but the language is flexible enough to encourage libraries. I *strongly* believe that Python Spring[sic] has both functionalities that could be attractive to many developers while having the chance of doing so as a library rather than a framework. –Sylvain while commenting about Spring Python w/CherryPy 3

And that’s why I call it Spring Python. It takes the cool concepts of Spring, and write them in a slick-and-quick, pythonic library. And yes, it is a library.

Spring Python isn’t a simple port of the Spring Framework

While surfing across Google, I spotted a conversation where a couple guys were commenting on Spring Python. The only commentary was based on the Wikipedia entry, and showed no delving into official documentation or our website. Well, I can’t help but respond to the some of the assumptions that were made.

Spring-Python. A port of the Spring framework to Python. It’s like people haven’t read

Uhh, I HAVE read Python is Not Java. In fact, I found it soon after I started this project, and took many of the points they were making to heart. If you look at the code, you will notice that a) there aren’t getters and setters all over the place typically found in Java, and b) I cleaned up the code base to better conform with PEP-0008. I have had other pythonistas look at my code and give me feedback so I can make this project as pythonic as possible.

That is, of course, a little unfair. They might be writing it in a Pythonic style. But still, aren’t there enough web frameworks?

Thanks for sticking up for me. I certainly appreciate that. And to clarify, yes, I am trying to writing this in a pythonic fashion. That is why I welcome other pythonistas to visit my code and seeing what I’m doing. Please send me patches or comments. However, please understand that Spring Python is NOT purely a web framework! This must one of the most frustrating things for the Spring folks. Spring MVC and Spring Web Flow may be web frameworks, but the whole framework isn’t just for web apps. In my daytime job, I use Spring all the time while NOT writing a web app! The stuff they have written is universal. Web app or not, there is good leverage inside their code, and I wanted the same power when writing python apps.

And reading the definitive source (Wikipedia, natch), I don’t see anything in there that isn’t done better in, say, Django.

Okay, how do I say this? Wikipedia is NOT the definitive source. While I admit that I wrote most of what’s in there, it is a NPOV, quick description of things. The definitive source is I’m not trying to compete with Django or replace it. Again, Spring Python is NOT a web framework. I have written samples of how it can enhance your CherryPy app. I want to study the other python web frameworks, and see if Spring Python can help out.

Sure, but doesn’t everyone already have an ORM layer with a better description than “the SQL query and row-handling function”?

I have supported a particular application for several years, and it now has 200+ queries. I’m not about to migrate it to an ORM solution because I don’t have the time. I didn’t know that ORMs were the final answer to database access. It seems people are still debating the impedance mismatch between objects and relational databases. Sometimes, the technology-neutral language of SQL is the most portable, expressive way to communicate. Got an ORM? Do you enjoy using ORMs like SQLObject or SqlAlchemy? Great! Spring Python won’t get in your way. Frankly, I haven’t spotted ANYTHING where we can help them, either. But do you use pure SQL queries? We DO have something for you.

Reading from the database hasn’t required “a monotonous cycle of opening cursors”, etc. in a long time.

Again, if you are using an ORM, then Spring Python doesn’t have anything today. But if you are writing some pure SQL, you need to manage your cursors correctly. Have you read PEP-249 Python Database API Specification v2.0? The spec defines two method calls, one for connection objects and one for cursor objects: close. It clearly indicates that you can use this to close the connection before the object itself goes out of scope. If you are writing a tiny python app that exits after a bit, your connections and cursors will be garbage collected and these things will close automatically. But if you want to write any type of enterprise app that stays up ALL THE TIME, then you need to be correctly closing result sets.

I have worked on enterprise level Java apps, and one of the first pitfalls is failure to close cursors and connections when you are done. JdbcTemplate solves this problem for you. Python has the same issue to deal with, and DatabaseTemplate provides the same solution.

And I fully admit I’m being unfair. (Although having said that, XML config files are another thing I’m more than happy to drop.)

Thanks for being honest. I don’t mind someone putting in their $0.02. Regarding XML configuration files, I have two angles of approach: 1) make it easy for existing Java developers who have used Spring to migrate to Python and 2) support new Python users. The XML configuration option is to help support the Java developers. But I totally grasp the concept of avoiding XML situps. It’s why I rapidly wrote a pure python, decorator based IoC container after I had a working XML container. BTW, the key reason I started with XML was the fact that someone had already created one, and it helped me jump start things. Once again, in all tradition of Spring, the choice is yours.

XML may not be the most tasteful thing to pythonistas, but we still have to deal with it. For that, I gladly recommend Amara. We used it to rapidly rewrite our XML parsers. It works wonders.

I just ask that you visit our website, read our documentation, read the source code, and learn what this project is trying to accomplish before you make too many assumptions.