This is the second in a short series of blogs I’m presenting that looks into implementing dependency injection using annotations. If you’re familiar with Spring or EJB3, you’ll know the sort of thing I’m talking about: you write you class; add a few annotations, for example @Autowired or @Ejb; deploy it to your web-server and then et volia it’s all glued together and works, as if by magic...
This blog isn’t going to write a fully formed DI factory using annotations, but it will give you a few hints in to the kinds of techniques used by the Guys at Spring and the EJB3 Team.
Java tips, observations, bugs and problems from the world of Spring, Weblogic, Oracle, MySQL and many other technologies...
Friday, 30 September 2011
Thursday, 29 September 2011
Looking into the Magic of Dependency Injection Using Annotations - Part 1
One of the things that every Spring and EJB3 developer does on a daily basis is to use annotations to inject instance variables into your objects, which, it seems, has become something we do without thinking about.
You know the type of thing I’m talking about: you write a couple of classes annotating one with @Component and the other with @Autowired and when you run your killer app hey presto everything’s happily glued together.
You know the type of thing I’m talking about: you write a couple of classes annotating one with @Component and the other with @Autowired and when you run your killer app hey presto everything’s happily glued together.
Labels:
Annotations,
ClassLoader,
Dependency Injection,
Java,
Spring
Wednesday, 28 September 2011
Searching Maven Central
It seems that no matter how much you read about software and how much you try and learn, you always seem to miss something pretty useful. I’ve been using Maven for at least three years and only found out a few weeks ago that you can search the Maven repository for artifacts... Duh! It seems the obvious thing to do, but if you’ve never seen something...
Labels:
Maven
Tuesday, 27 September 2011
Using Spring Interceptors in your MVC Webapp
I thought that it was time to take a look at Spring’s MVC interceptor mechanism, which has been around for a good number of years and is a really useful tool.
A Spring Interceptor does what it says on the tin: intercepts an incoming HTTP request before it reaches your Spring MVC controller class, or conversely, intercepts the outgoing HTTP response after it leaves your controller, but before it’s fed back to the browser.
A Spring Interceptor does what it says on the tin: intercepts an incoming HTTP request before it reaches your Spring MVC controller class, or conversely, intercepts the outgoing HTTP response after it leaves your controller, but before it’s fed back to the browser.
Labels:
Interceptor,
Java,
MVC,
Spring
Friday, 23 September 2011
The Benefits and Dangers of using Opensource Java Libraries and Frameworks
Everyone in the Java world seems to use various opensource libraries and frameworks... and why not, there are hundreds available covering virtually every type of programming problem you’re likely to come across in today’s programming landscape. This blog takes a quick look at the reasons for using opensource artifacts and examines what could go wrong...
Labels:
Opensource
Thursday, 22 September 2011
Adding SLF4J to your Maven Project
In yesterday’s blog, I documented the various bits of the Maven POM file created for a ‘Spring MVC Project’ using one of Spring’s project templates. In that POM file you may have noticed a reference to an slf4j-api artefact and comment to the effect that the Guy’s at Spring were excluding “Commons Logging in favor of SLF4j”. This blog takes a look at SLF4j, or to give it its full title: “Simple Logging Facade for Java” and demonstrates how to add it to a project.
Labels:
Apache Commons,
Java,
Log4j,
Maven,
SLF4j
Wednesday, 21 September 2011
Dissecting Spring's MVC Project POM
One of the good things about Spring’s STS is that is provides a whole bunch of useful Spring project templates that you can use to generate empty or stub projects from which to start writing some code. The list of projects includes a ‘Simple Spring Utility Project’ and a ‘Spring Batch Admin WebApp’.
Tuesday, 20 September 2011
When Inheriting a Codebase, there are more questions than answers...
There will be that time in your life when you inherit someone else's source tree or codebase and you’ll need to think of a plan to deal with this situation. Now, there are codebases and there are codebases, none are ever perfect, but some are just unbelievable...
The idea of this blog is to go through some of the question’s you’ll need to ask in order to figure out what you can do and whether or not you’ll be able to make a difference or just end up picking away at the edges going nowhere.
The idea of this blog is to go through some of the question’s you’ll need to ask in order to figure out what you can do and whether or not you’ll be able to make a difference or just end up picking away at the edges going nowhere.
Labels:
Agile,
Team Management
Monday, 19 September 2011
Deploying Applications to Weblogic Using Maven
When developing JEE applications as part of the development cycle, it’s important to deploy to a development server before running end to end or integration tests. This blog demonstrates how to deploy your applications to your Weblogic server using Maven and, it transpires that there are at least two ways of doing this.
Friday, 16 September 2011
Using AspectJ’s @After Advice in your Spring App
Today’s blog demonstrates how to implement a simple after advice in your Spring application using the @After annotation. An after advice is where your advice code gets called after the join point in your point cut. I’ve previously blogged on the definitions AOP/AspectJ’s cryptic terms, but putting it simply, your advice code gets to run when a method whose signature meets some pre-defined pattern has finished running. Sets of methods whose signatures meet some pre-defined patterns are the point cuts.
Labels:
Annotations,
AOP,
AspectJ,
Java,
Spring
Thursday, 15 September 2011
Defining Spring's AspectJ Advice Types
My blog from a couple of days ago defined the AspectJ, AOP’s term ‘advice’ as the code that runs when your application execution reaches a join point in a point cut. What I neglected to mention is that there are several different types of advice all applied to method executions and these are summarized in the table below:
The Guy’s at Spring have documented how to use these annotations in this document.
Annotation | Description |
@Before | The advice method gets called before the method defined in your point cut. |
@After | The advice code gets called after the method defined by your point cut has finished executing. |
@Around | An advice that combines the @Before and @After, executing some code before the method in the point cut and some after. |
@AfterThrowing | An advice that executes after an exception have been thrown, and before it’s caught. |
The Guy’s at Spring have documented how to use these annotations in this document.
Wednesday, 14 September 2011
Using RESTful URLs on your Spring 3 MVC Webapp
This blog comes as an answer to a question from a reader. Now, as a rule, I don’t do request blogs, but I thought that this sounded like an interesting topic. The question refers to my blog from July 11 on accessing request parameters using Spring 3 MVC and was “Do you know how to encode the uuid so it is not visible to the user for security concerns?”. The answer is “Yes”, but that would make for a very short blog.
Labels:
Base64,
Encryption,
Java,
Javascript,
JSP,
MVC,
Spring
Tuesday, 13 September 2011
AOP and AspectJ Terminology
In my recent blog on 10 minute concepts, I mentioned some of the terms relating to AspectJ and AOP, which I never defined. This blog puts that right and explains some of these terms below...
AspectJ Term | Description |
Aspect | A feature of a program that is separate from, and not related to any specific part of a program, but is woven throughout the fabric of the program so as to be used by all or many parts of the program.... and this sounds really vague. You can think of a program as a bunch of events, for example: methods being called, objects being constructed etc. An aspect oriented approach to programming, such as ApsectJ, really means creating some mechanism where by you can call your method before, after or around some other event, so as run some of your code, without the need to reference this code from the event. |
Join Point | A join point is the name given to one of the programmatic events as described above, eg calling a method or creating an object. In AspectJ, a joint point is really the specification of where/when in your program, your aspect code is running. For example, if your aspect code runs before a given method call, then the join point describes the signature and arguments of that method. |
Pointcut | A set of related joint points. When program execution reaches one of the join points in the pointcut, then your aspect code will run. |
Advice | This is the code that runs when program execution reaches a joint point in your pointcut. |
Monday, 12 September 2011
Weasel Words
The other day I had to look into a project’s source code and shocked by the state it was in. I can, hand on heart, say I haven’t seen code like it for 10 years... which made it interesting.
Labels:
Anti-Pattern,
Object Orientation,
OO,
Pattern
Friday, 9 September 2011
Using Spring's AspectJ Support and the @Before Annotation
You may have wondered how the Guys at Spring do all that jiggery-pokery with the annotations that you add to your Spring beans. I’m not an expert here, but I suspect that somewhere along the way, they do a little Aspect Oriented Programming using AspectJ - after all, they do have comprehensive support for it. This of course is total supposition - I’ve not been trawling through their source code. This blog demonstrates how you can use AspectJ to intercept your class's annotated methods and to do that,I’m going to demonstrate the @Before annotation by writing a Before Advice class that takes a peak at the attributes of a method’s annotations. This blogs builds on my previous AspectJ blog that demonstrates how to write an After Throwing advice.
Labels:
Annotations,
AOP,
AspectJ,
Java,
Spring
Thursday, 8 September 2011
The Ten Minute Concept Rule
This is something really simple that occurred to me the other day. If you’ve read any of my blog content over the past few months, you’ll realise that I’ve covered a lot of ground, talking about Java, Spring, Weblogic, EJB3 and other topics, and in talking about these APIs it struck me that the most popular ones have one thing in common: their basic concepts can be easily understood within a few minutes of reading about them.
Take for example Spring and dependency injection. Spring is BIG, there’s a lot to it, but the concept is simple: use a factory of some sort to build your application by injecting all its dependencies and do it in the simplest way possible.
Take for example Spring and dependency injection. Spring is BIG, there’s a lot to it, but the concept is simple: use a factory of some sort to build your application by injecting all its dependencies and do it in the simplest way possible.
Wednesday, 7 September 2011
Using AspectJ’s @AfterThrowing Advice in your Spring App
This may not be strictly true, but it seems to me that the Guy’s at Spring are always banging on about AspectJ and Aspect Oriented Programming (AOP), to the point where I suspect that it’s used widely under the hood and is an integral part of Spring and I say “widely used under the hood”, because I haven’t come across too many projects that do use AspectJ or AOP in general.
I suspect that part of the reason for this is possibly down to the fact that AOP different is a concept to Object Oriented Programming (OOP). AOP, they say, is all to do with an application’s cross-cutting concerns, which translates to mean stuff that’s common to all classes within your application. The usual example given here is logging and example code usually demonstrates logging all method entry and exit details, which is something that I’ve never found that useful. The other reason that I’ve not seen it used is that the AspectJ documentation is a bit ropey.
I suspect that part of the reason for this is possibly down to the fact that AOP different is a concept to Object Oriented Programming (OOP). AOP, they say, is all to do with an application’s cross-cutting concerns, which translates to mean stuff that’s common to all classes within your application. The usual example given here is logging and example code usually demonstrates logging all method entry and exit details, which is something that I’ve never found that useful. The other reason that I’ve not seen it used is that the AspectJ documentation is a bit ropey.
Tuesday, 6 September 2011
Using Spring’s @Required Annotation
When your application needs a bean class, it’s not unusual for certain attributes to be mandatory, which if missed from your Spring config file will cause you problems at some undetermined future time. The Guys at Spring thought of this and came up with the @Required annotation that you can apply to your bean’s setter method:
// Snippet from a plain old bean...
private int id;
private String street1;
@Required
public void setId(int id) {
this.id = id;
}
@Required
public void setStreet1(String street1) {
this.street1 = street1;
}
Labels:
Annotations,
Java,
Spring
Monday, 5 September 2011
Integrating SureAssert with Existing Projects
I recently blogged about SureAssert’s ability to do automatic, continuous, declaration driven testing using annotations, which could make JUnit test classes obsolete. When picking up any new and useful development tool, the first question asked is usually “how can it help my existing project?” The answer here is that SureAssert integrates with existing JUnit test code, which gives you the benefit of having your existing JUnit tests continuously monitored and executed.
Labels:
Annotations,
Java,
JUnit,
SureAssert,
Unit Tests
Friday, 2 September 2011
Adding Sitemesh to your Spring webapp
The other day I came across a company that was recommending the use of Sitemesh as a way of creating re-useable JSP templates and this worried me slightly. There’s nothing particularly wrong with Sitemesh, it’s a tool for segmenting JSP pages in a similar way to Struts Tiles and it’s rather neat. The downside of using it is that it was written by the now defunct OpenSymphony, which means that no one’s supporting it and bugs, if there still are any, won’t get fixed. Furthermore, is the source code still available so that you can build it yourself? I also, wondered whether this company’s very prestigious clients know that this firm was using unsupported, vintage software and would they care?
Thursday, 1 September 2011
A Mention On SpringSource
I normally reserve my blog for demonstrating various Java bits and pieces that I’m personally interested in, whether it’s something that I need to know that’s related to work or something I’m just curious about.
Today’s blog is an exception, as it’s a thank you to the Guys at Spring - Josh Long - for the mention in This week in Spring, and Guys, I currently have 53 Spring related blogs including another half dozen yet to be published.
Today’s blog is an exception, as it’s a thank you to the Guys at Spring - Josh Long - for the mention in This week in Spring, and Guys, I currently have 53 Spring related blogs including another half dozen yet to be published.
Labels:
Spring
Subscribe to:
Posts (Atom)