How to inject Spring beans into Servlets

This can be achieved in 3 simple steps:

1. Implement HttpRequestHandler

First of all your servlet class must implement the org.springframework.web.HttpRequestHandlerinterface and provide an implementation for the handleRequest() method just like you would override doPost().

2. Declare the servlet as a Spring Bean

You can do this by either adding the @Component(“myServlet”) annotation to the class, or declaring a bean with a name myServlet in applicationContext.xml.

   public class MyServlet implements HttpRequestHandler {

3. Declare in web.xml a servlet named exactly as the Spring Bean

The last step is to declare a new servlet in web.xml that will have the same name as the previously declared Spring bean, in our case myServlet. The servlet class must be

The Generic DAO pattern in Java with Spring 3 and JPA 2.0

One thing that annoys me the most is code duplication. The DAO layer is the perfect candidate for this kind of situation. Often, developers forget about OOP, polymorphism and design patterns and just copy&paste code, change the name of the class and voila, we have a brand “new” BankDao class. I’ll present you how to implement a generic DAO pattern to avoid code duplication and preserve type safety in the same time. Why would you care about type safety and just don’t go use the EntityManager’s generic methods. Well, for various reasons:

  1. You know for sure which entity objects can be persisted
  2. You avoid a lot of explicit casts which are error prone
  3. The code is cleaner and  very easy to follow
  4.  You actually apply OOP principles like inheritance and polymorphism. 😉

The purpose of this article is not to get you familiar with the DAO pattern, but to show you a better way of using it. You can find a complete reference about DAO here: DataAccessobject.

The Generic DAO interface

Let’s get started. First of all this article assumes you are using Spring 3 (although this can be easily adapted to Spring 2.5) and JPA 2.0 in you project and the initial configuration is in place:  you already have a data sources declared, an entity manager factory, etc.  The application is basically up&running.

The foundation of using a Generic DAO is the CRUD operations that you can perform on each entity. And this is the least you can have. Additional generic methods can be defined like: count all objects of a specific type; execute generic queries based on some parameters, etc. You’ll see a sample bellow for countAll().

The core of this pattern will be an interface called, surprisingly, GenericDao and its implementation Continue reading “The Generic DAO pattern in Java with Spring 3 and JPA 2.0”

Creating a URL JLabel in Swing

So, you need a simple clickable URL JLabel in your Swing application? You can, of course, use third party libraries to do this, but why add another dependency for a single component when can easily write one in 5 minutes.

So what do we want for this JLabel:

  • to click on it and go to an URL
  • to have a hand cursor on mouse over
  • the text to be underlined just like a real link

We’ll write a class that extends JLabel and a mouse adapter that will take care of opening the link. As the mouse adapter will be used only for this label, we’ll add it as a private member class. Here is the code:

package insidecoding.swing

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;

 * @author ludovicianul
public class URLLabel extends JLabel {

    private String url;

    public URLLabel() {

    public URLLabel(String label, String url) {

        this.url = url;
                new Cursor(Cursor.HAND_CURSOR));
                new URLOpenAdapter());

    public void setURL(String url) {
        this.url = url;

    //this is used to underline the text
    protected void paintComponent(Graphics g) {


        Insets insets = getInsets();

        int left = insets.left;
        if (getIcon() != null) {
            left += getIcon().getIconWidth() + getIconTextGap();

        g.drawLine(left, getHeight() - 1 - insets.bottom, (int) getPreferredSize().getWidth()
                - insets.right, getHeight() - 1 - insets.bottom);

    private class URLOpenAdapter extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().browse(new URI(url));
                } catch (Throwable t) {

I think the code is pretty self-explanatory:

  • in the constructor we set the label color to a darker blue, we set the mouse over cursor to Hand Cursor and we register the mouse click listener
  • we use the paint() method to underline the label’s text
  • the mouse adapter uses the Continue reading “Creating a URL JLabel in Swing”