A great way to catch your reader’s attention is to tell a story. Everything you consider writing can be told as a story.
There are better ways of doing things, which provides learning opportunities whether it is technology, design pattern or different problem-solving mindsets.
Spring was an easy choice because it caters for these requirements, and has a large user base with constant update and fixes. AngularJS on the other hand has a fairly small user base according to statistics. I chose it because of the design philosophy, the MVC model for the front-end, working with modules, but the deciding factor is that Google is maintaining it (the next big thing?).
When you add the @Configuration annotation to the Java class, Spring automatically identifies it as a configuration file, furthermore you can add@Profile(“name”) to define a different profiles with specific configuration, such as a testing or development with different properties such as datasource, testing configuration, test data etc.
The ServletContextConfig.java tells Spring that this is a Web MVC architecture via the @EnableWebMvc and scans for the controller layer. Other web security component can also be declared in here via the extension of theWebMvcConfigureAdapter.java.
Here I used a RESTful web services, by declaring the @Controller annotation, the application will look for the @RequestMapping annotation as it is requires by theDispatcherServlet in Spring Web MVC Framework. The value inside the@RequestMapping(“/stock”) with determine which controller it should go to and the request header will indicate the method to call.
What I like: Look at the code below and ask yourself the question: “How do you pass a POJO through to a RESTful web service?” This was the question I had, usually this is done via passing the object through the HttpServletRequest via theHttpSession object, but here we are using JSON which gets converted to POJO and vice versa. When the front-end (ajax in this case) sends the content type as “application/json”, Spring picks it up with the @RequestBody annotation and converts it into a POJO and it works the other way around with @ResponseBodywhich convert the POJO back into JSON. Just like that, no extra work, it is that simple.
In short, I enjoy learning and developing with the framework and it feels really solid. The development time is extremely fast, without having to declare and initialize all my service and component before use. Due to its modular design, debugging is really easy, code is easy to scale and maintain; it really allows me to focus on developing business requirements and leave the plumbing to Spring. It took less than a week to learn and migrate Stock Monitor to Spring MVC Framework, Angular on the other hand took much longer. I admit my implementation and requirements are fairly simple, and there are many concept that I have not dealt with, but with the huge supporting documentations and user base, I have confidence that it is fairly easy to learn.
In order for the application to know where to go when a link or a button is pressed, routing is required. This tells the the site what component to load in the directive, as opposed to loading a whole HTML page. Angular-ui-router module is used to achieve this, by defining different state and its dependencies in the module configuration. This is really nice as oppose to the JSF configuration file or a database driven routing or a html static link. The ui-router also hides the URL with the hashtag instead of a static link.
At a high level, directives are markers on a DOM element (such as an attribute, element name, comment or CSS class) that tell AngularJS’s HTML compiler($compile) to attach a specified behavior to that DOM element (e.g. via event listeners), or even to transform the DOM element and its children.
So, back to the topic of a single page application. Here the header and the side bar only get loaded once, only the main frame get loaded with new content when navigating between component, here it loads the stock.html. When you click on the Add Button, the HTML calls the Angular controller to insert a new row of inputs. Once you have inserted all the values and click on the Save Button, angular controller goes and validate the values then compiles them into JSON and send them off to the service layer. The service layer then sends the JSON data through to the back-end for processing. All this happens in real time with ajax, there are no page refreshes or loading during this process, making the application fluid and user friendly. This is achieved via the Angular directives inside the HTML tags.
With regards to migrating to AngularJS, my answer is a definite “No”, not even for small projects. My reasoning is that you will need to breakdown the entire existing front-end to use a Angular controller and directives. The process is inefficient so I would not recommend migrating.
In the end, I had great experiences working with both Spring and AngularJS. Spring really surprises me and exceeded my expectations Angular, on the other hand, was interesting to work with but I am not sold on it. I believe it all depends on the business requirements, then finding the appropriate tools to solve it. AngularJS feels too complicated for my requirements, but I do believe there is a place for it in a more complex environment.