Swift’s Nil Coalescing Operator AKA the “?? Operator” or “Double Question Mark Operator”

This last week I was introduced to quite a funky little operator in Swift called the Nil Coalescing operator. It is basically a shorthand for the ternary conditional operator when used to test for nil.

For example traditionally in many programming languages you could do something like this to check for nil/null…

stringVar != nil ? stringVar! : "a default string"

Here if the condition is not nil the value of stringVar will be returned otherwise the value “a default string” is returned.

This can be shortened in Swift by using the Nil Coalescing operator.

stringVar ?? "a default string"

This does exactly the same thing, if stringVar is not nil then it is returned, however if it is nil the value “a default string” is returned.

Love it.

Technorati Tags: , , , , , , , , ,

Long time no posts.

Woah just realised it’s been 4 years since I last did a techie post, and 2 since the last non-techie one.  I hope to start writing more regular posts again.

Well since 2010 I’ve been learning iOS development and so the blog will probably focus a bit more on that from now on.

How To Pass A Null Value To A Custom Tag Library

Today I found something very interesting.  I wanted to pass a java.math.BigDecimal to a custom JSP Tag Library and format it as a percentage, however if the BigDecimal was null I didn’t want to show it.

Easy, I thought. So I created a class similar to the following…

package name.kayley.blog.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.math.BigDecimal;

public class BigDecimalTagSupport extends TagSupport {

    private BigDecimal value;

    public void setValue(BigDecimal value) {
        this.value = value;
    }

    public int doEndTag() throws JspException {
        try {
            if (value != null) {
                pageContext
                        .getOut()
                        .print(formatAsPercentage(value));
            }
        } catch (IOException e) {
            throw new JspException(e);
        }
        return EVAL_PAGE;
    }
// ...
}

However, when I called this taglib with a null value I still got a value of 0.

<mytag:bigDecimal value="${aBigDecimal}" />

The output:
0

After a bit of trial and error I discovered that the JSP tab library framework does a little bit of magic on known classes, such as Strings and Numeric objects. Classes of Numeric types that are null get converted to 0 and Strings get converted into the empty string ""

I discovered that if you change your internal value to an java.lang.Object instead of a java.math.BigDecimal the magic doesn’t know what to do and just passes null to your class.

So the new class looks like the following…

package name.kayley.blog.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

public class BigDecimalTagSupport extends TagSupport {

    private Object value;

    public void setValue(Object value) {
        this.value = value;
    }

    public int doEndTag() throws JspException {
        try {
            if (value != null) {
                // Cast value to a BigDecimal 
                // if you need to.
                pageContext
                        .getOut()
                        .print(formatAsPercentage(value));
            }
        } catch (IOException e) {
            throw new JspException(e);
        }
        return EVAL_PAGE;
    }
//...
}

So now when I use the tag and pass a null BigDecimal to it I get the required result of no output.

WIN

I have an example web app demonstrating this, if anyone wants a copy just let me know.

Technorati Tags: , , , , ,

Pay At The Pump and LPG Finder for BlackBerry(R)

For the past few months on and off I’ve been working on a project for my friend and former colleague John Haselden of Abstractec. He has made a website and an iPhone phone application which utilises web services of the website. The project is called Fuelista, and the applications are Pay at the Pump and iPayAtThePump for iPhone, (with variants for LPG etc).

The idea of the Pay at the Pump and LPG Finder applications is to provide a location based service to allow you to find petrol stations that provide a “pay at the pump” or “LPG” service. The application does a GPS lookup on your location, and returns a list of garages that provide that service. You can then navigate through and get more details of the garage, and if you require you can a route from your current location to the garage.

Abstractec are trying out more platforms than just the iPhone. They are investigating Android, BlackBerry and PalmPre. I was asked if I would like a go at the BlackBerry port, so I thought “why not?”

I wanted to get into writing iPhone applications for fun and maybe a bit of extra cash, I bought myself a Macbook Pro and an iPhone in mid 2009, but there is a lot to learn to get up and running with iPhone development, the applications are written in Objective-C and the development environment is quite difficult to get your head around when you’re starting off, especially coming from a Java background.
BlackBerry applications however are written using Java.

Research In Motion (RIM) provide a layer on top of J2ME providing their own classes and UI components specifically for the BlackBerry handsets.

The application is complete and also I have created the LPG equivalent. Check out the video below for a demo of the application. The GPS lookup on the handset that I have to test with is quite poor…Hence the timeout when the first lookup occurs.

Enjoy…

Update 9th December 2018 – I dug out the code for this little app and it’s available here if anyone is interested.

Technorati Tags: , , , , , ,

How to Avoid ClassCastExceptions when using Hibernate Proxied Objects

Recently at work I was faced with an issue on the live system. A certain page within the application would only show the system’s standard error message. After looking in the logs I discovered there was a ClassCastException being thrown, this was due to the fact that a lazily loaded List of objects retrieved by hibernate contained proxied objects. The entities that were proxied were using a single table inheritance strategy, so there is a base class that is subclassed several times. The entities once loaded were being sorted into separate lists that are typed according to the subclasses, they were being cast into the appropriate subclass but this cast was failing because instead of being an instance of say Animal it was an instance of Animal$$EnhancerByCGLIB$$10db1483 i.e. A Hibernate Proxy. Let me show you an example.

So lets say we have a base class that has an inheritance strategy set…

package name.kayley.cglibexample;

import javax.persistence.*;

@Entity
@Table(name="animals")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="animal_type_id",
    discriminatorType=DiscriminatorType.INTEGER
)
public class Animal {

    @Id
    @GeneratedValue
    private Long id;
    
    @Column
    private String name;
    
    @Column
    private Integer age;
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

And then we had 2 subclasses of this Animal class, lets say, Cat and Dog


package name.kayley.cglibexample;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("1")
public class Cat extends Animal {
   // some cat specific attributes
}


package name.kayley.cglibexample;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("2")
public class Dog extends Animal {
   // some dog specific attributes
}

As you can see the discriminator column is animal_type_id, which I have defined 2 to be Dog and 1 to be Cat. So now we should make sure we have some data in a database table called animals…

SELECT id,animal_type_id,name,age FROM animals a LIMIT 0,1000

'id', 'animal_type_id', 'name', 'age'
1, 2, 'Scooby', 3
2, 1, 'Felix', 4
3, 1, 'Garfield', 4

Right so to recreate a similar situation I will show you a unit test that will pass! What I am trying to do is to retrieve the list of animals and sort them in the java into separate lists, a list of dogs and a list of cats..

public void testCGLibInstanceOfError() throws Exception {

    List dogs = new ArrayList();
    List cats = new ArrayList();

    Iterator it = hibernateDao.createQuery("from Animal").iterate();

    while (it.hasNext()) {

        Animal animal = it.next();
        assertTrue(animal.getClass().getName().contains("Animal$$EnhancerByCGLIB$$"));
                    
        if (animal instanceof Cat) {
            cats.add((Cat) animal);
        } 
        else if (animal instanceof Dog) {
            dogs.add((Dog) animal);
        }
    }
    assertEquals(0, dogs.size());
    assertEquals(0, cats.size());
}

Now using the data in the database we want a list of 1 dog and a list of 2 cats. Here I’m being particularly evil and using instanceof to differentiate these. The assertEquals at the end of the test pass, even though the code looks like it should be adding items to these lists based on what is in the database. This is because animal is not an instanceof Cat or Dog but an instanceof Animal$$EnhancerByCGLIB$$10db1483.

So how do we solve this issue?

I first thought about using Hibernate.initialize(animal); which will force the animal class to become the actual object, not the proxy. I felt this approach to be dirty and it is really polluting code that shouldn’t really know anything about hibernate.

So I did some googling and after reading the hibernate documentation and then this I set about using the Gang Of Four Visitor Pattern.

First we need a visitor interface that we can implement in your test…

package name.kayley.cglibexample;

public interface AnimalEntityVisitor {
    void visit(Dog dog);
    void visit(Cat cat);
}

Next we need is an interface for our Animal class to implement…

package name.kayley.cglibexample;

public interface AnimalEntity {
    void accept(AnimalEntityVisitor visitor);
}

Now we make our Animal class I showed earlier implement the AnimalEntity


package name.kayley.cglibexample;

import javax.persistence.*;

@Entity
@Table(name="animals")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="animal_type_id",
    discriminatorType=DiscriminatorType.INTEGER
)
public abstract class Animal implements AnimalEntity {
   ...
   // see top of post
   ...
}

And next implement the method in the subclasses..


package name.kayley.cglibexample;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("1")
public class Cat extends Animal {
   // some cat specific attributes

   public void accept(AnimalEntityVisitor visitor) {
      visitor.visit(this);
   }
}


package name.kayley.cglibexample;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("2")
public class Dog extends Animal {
   // some dog specific attributes

   public void accept(AnimalEntityVisitor visitor) {
      visitor.visit(this);
   }
}

All we need to do is implement the visitor in our test…


public class MyTest 
    extends AbstractTransactionalDataSourceSpringContextTests 
    implements AnimalEntityVisitor {

        ...
    // some spring setup     
        ...

    private List dogs = new ArrayList();
    private List cats = new ArrayList();
    
    public void testCGLibUsingVisitor() throws Exception {

        Iterator it = hibernateDao.createQuery("from Animal").iterate();

        while (it.hasNext()) {

            Animal animal = it.next();

            assertTrue(animal.getClass().getName().contains("Animal$$EnhancerByCGLIB$$"));

            animal.accept(this);
            
        }
        assertEquals(1, dogs.size());
        assertEquals(2, cats.size());
    }

    public void visit(Dog dog) {
        dogs.add(dog);
    }

    public void visit(Cat cat) {
        cats.add(cat);
    }
}

So instead of using instanceof or having to cast anything (which is what was happening in the live code I talked about) which can cause weird side effects and ClassCastExceptions, you can just use this visitor pattern. What happens is the test is implementing the AnimalEntityVisitor, so when animal.accept(this); is called, it calls the appropriate visit method on “this”. In our case the visit method is just adding the object to a list. So I’ve updated the assertEquals calls to be what I now expect and the test passes.
Happy days, enjoy!
Technorati Tags: , , , , , ,

How to Undelete files removed with “rm” in Ubuntu

Recently I found myself in a situation where I needed to recover some files I accidentally deleted. I had written a program in Java to read off a JBoss Messaging Dead Letter Queue, and save them in a format that could be re-inputted back into the system.

So my program was writing files into a directory within my home directory, and while I was testing the program I was not committing back to JMS so ensure the messages stayed there. When I was confident that the program was correct, I added the commit, I then removed the files that my tests had produce by using “rm *”.

I then ran my program for real. In the directory I was expecting files to appear I typed the usual “ls -ltr” to see a list of files. I pressed the up key to run the last command i.e. “ls -ltr” to see some more files created. I repeated this a few times, until I accidentally pressed the up key twice…..yep you guessed it….”rm *”, the files were deleted and the commit to JMS means they were no longer on the queue. My heart sank.

Now I rememebered on the old days of DOS there was an UNDELETE command, so I was looking around the web to see if there was something similar for Linux. I was in luck, especially as my program was still running.

Basically thanks to this information, I recovered my deleted files with relative ease. 🙂

Ok so to do this, I believe you need to be as fortunate as I was and still have the program that created the deleted files still running, so let’s create a Java program that will write to a file and then keep running…


package name.kayley.undeletefiletest;

import java.io.FileWriter;
import java.io.PrintWriter;

public class UndeleteFileTest {
 public static void main(String[] args) throws Exception {  
  FileWriter writer = new FileWriter("/tmp/testFile.txt");
  
  PrintWriter printer = new PrintWriter(writer);
  
  printer.append("hello this file is going to be deleted.");
  printer.flush();
  Object lock = new Object();
  
  synchronized(lock) {
   lock.wait();
  }
 }
}

Ok, now run that program and it will create a file in /tmp called testFile.txt, so lets see it..


andy@andy-laptop:/tmp$ ls -ltr *.txt
-rw-r--r-- 1 andy andy 39 2009-02-13 18:54 testFile.txt

So we can see the file is there, lets delete it to see if we can recover it…

andy@andy-laptop:/tmp$ rm testFile.txt

Now the file is gone. So how do we get it back? Well first we need to find the PID of the process that created the file that was deleted. We know that it was a java program that created it so we’ll use the lsof and search for java..

andy@andy-laptop:/tmp$ lsof | grep java
.....[lots of things listed]
java      9048       andy    4r      REG        8,1       39 1622029 /tmp/testFile.txt (deleted)

No now we know the PID is 9048 lets find the file… so there should be a directory /proc/[PID]/fd…


andy@andy-laptop:/tmp$ cd /proc/9048/fd
andy@andy-laptop:/proc/9048/fd$ ls -ltr
total 0
lr-x------ 1 andy andy 64 2009-02-13 18:54 4 -> /tmp/testFile.txt (deleted)
l-wx------ 1 andy andy 64 2009-02-13 18:54 3 -> /usr/local/jdk1.6.0_07/jre/lib/rt.jar
l-wx------ 1 andy andy 64 2009-02-13 18:54 2 -> pipe:[43665]
l-wx------ 1 andy andy 64 2009-02-13 18:54 1 -> pipe:[43664]
lr-x------ 1 andy andy 64 2009-02-13 18:54 0 -> pipe:[43663]

Bingo! We have found the deleted file, so now all we need to do is cat the link to a new file, so here I put it into my home directory…


andy@andy-laptop:/proc/9048/fd$ cat 4 > ~/testFile.txt

Next, I go to my home folder and have a look at the file, and hey presto…its restored…


andy@andy-laptop:/proc/9048/fd$ cd
andy@andy-laptop:~$ less testFile.txt 
hello this file is going to be deleted.
testFile.txt (END) 

Technorati Tags: , , , , ,

How to Print Out Bind Variables in Java Prepared Statements.

Ever been frustrated by the fact that when you use (as you should do!) bind variables within a java prepared statement you cannot see the actual values that get put into those bind variables? I know I have. For example if you had a class that had some JDBC calls that did the following….

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection("jdbc:mysql://localhost/message_board", "root", "");

statement = connection.prepareStatement("select * from messages where id = ?");
statement.setLong(1,1L);

resultSet = statement.executeQuery();

There is no nice out the box way to find out what the ?’s will be resolved to. I’ve always wanted a method on PreparedStatement that would address this, but alas there still is none. Traditionally I would have done something like the following…


long id = 1L;
String sql = "select * from messages where id = ?"
logger.info(sql + "; ?1=" + id);

statement = connection.prepareStatement(sql);
statement.setLong(1,id);

Or write your own convoluted method for replacing each ? with the value which is prone to bugs and not being database agnostic.

When using an old jdbc based in-house framework this really wasn’t an issue for me, as we had such a method that was prone to bugs but for the most part it worked ok. Since then the work I have been doing has been much more hibernate based. This is where the issue came for me, you can turn on sql logging either by configuring your favourite logging framework to do so, or by turning on the hibernate.show_sql property, which will give you the following output…


select message0_.id as id2_, message0_.email_address as email2_2_, message0_.inserted_at as inserted3_2_, message0_.remote_host as remote4_2_, message0_.text as text2_ from messages message0_ where message0_.id=?

This sql was generated from the following hql

from Message where id = ?

Now apart from this sql being particularly hideous to look at, the question mark is still there, we have no way of possibly knowing what those bind variables were without printing them out at the time the hql is executed.

Enter p6spy.

I did a bit of googling about and found other people had the same issue. And there is an answer to this problem, and its called p6spy. P6spy is a jdbc driver that is a proxy for your real driver. I’ve only just started to use it, I believe it is capable of many cool things, but the one that I am interested in allows you to print out sql from prepared statements with – yep, you guessed it – the bind variables substituted for the real values.

You can download it from http://www.p6spy.com, it must be fairly stable as the last release was in 2003 (I’m so annoyed that I didn’t know about this earlier). There seems to be a little problem with their website as when you click download you get an error while trying to fill in or ignore their survey. Don’t panic, once you’ve ignored the survey go back to the main page and click download again and the files will be there waiting for you to download.

So how do we configure this little beauty? Well the download is p6spy-install.zip, unzip this somewhere safe (and keep in mind that all the files are in the root of the zip file, so you might want to create a directory to unzip it to first). The files we’re interested in are p6spy.jar and spy.properties. Copy the jar file into the lib folder of your project and copy the spy.properties file into somewhere that will be on the classpath.

If you look inside spy.properties you will see that it’s really very well documented and tells you all you really need to know on how to configure it. Although one thing to note in there is that they have said that the real driver you configure in your application should be set to com.p6spy.engine.P6SpyDriver which is actually incorrect, it should be com.p6spy.engine.spy.P6SpyDriver, the first one doesn’t exist and you will get a ClassNotFoundException.

So what I did… In my spy.properties I set the realdriver…

# mysql Connector/J driver
realdriver=com.mysql.jdbc.Driver

(note by default the open source mysql driver is uncommented which was a gotcha for me at first, so I commented it out, and uncommented the above)

Next I altered my code in the first example to the following…

Class.forName("com.p6spy.engine.spy.P6SpyDriver");

connection = DriverManager.getConnection("jdbc:mysql://localhost/message_board", "root", "");

statement = connection.prepareStatement("select * from messages where id = ?");
statement.setLong(1,1L);

resultSet = statement.executeQuery();

And lo and behold in the root of my eclipse project on the file system a file called spy.log has appeared. and in it the following…

1217374504582|6|0|statement|select * from messages where id = ?|select * from messages where id = 1
1217374504595|-1||resultset|select * from messages where id = 1|email_address = myemail@nospam.com, id = 1, inserted_at = 2008-07-29 00:00:00.0, remote_host = localhost, text = Thank you for showing me the wonders of p6spy

There is lots of other stuff in the log file as well but as you can see its very handy, as it shows you the statement with question marks and with the real values substituted, and also shows you the results set which is passed back 🙂 Beautiful lol.

And the hql of

from Message where id = ?

is output as:


1217374913350|5|2|statement|select message0_.id as id2_, message0_.email_address as email2_2_, message0_.inserted_at as inserted3_2_, message0_.remote_host as remote4_2_, message0_.text as text2_ from messages message0_ where message0_.id=?|select message0_.id as id2_, message0_.email_address as email2_2_, message0_.inserted_at as inserted3_2_, message0_.remote_host as remote4_2_, message0_.text as text2_ from messages message0_ where message0_.id=1
1217374913369|-1||resultset|select message0_.id as id2_, message0_.email_address as email2_2_, message0_.inserted_at as inserted3_2_, message0_.remote_host as remote4_2_, message0_.text as text2_ from messages message0_ where message0_.id=1|email2_2_ = myemail@nospam.com, remote4_2_ = localhost

Still ugly but at least I can now see what the bind variables are going to be 🙂

You can also configure it so it comes out in your normal logging file, the spy.properties file details how you do that.

Oh how I wish I’d have known this 4 years ago.

Technorati Tags: , , , , ,,,

How to get Logitech QuickCam Ultra Vision working in Ubuntu

I have recently removed Windows from my Dell Inspiron 9400, and replaced it with Ubuntu, which is a flavour of Linux (or GNU/Linux if you’re picky).

I was surprised at how easy the installation was, almost everything worked out of the box, wireless, all the media keys and 95% of the fn keys. (I’m having trouble with fn+f8 and fn+10 not working correctly)

Anyway I am very happy with the Installation, I love the effects you have on the windows, it looks really pretty.

I wanted to get my Logitech QuickCam Ultra Vision working with Ubuntu, so I could use it on Skype. I plugged it in and nothing happened…ho hum… on windows it would have picked it up and installed it, and asked for the driver cd, but anyway, after a bit of googling around I found this thread, and I followed the advise of Andrew Barber on there.

Make sure first you have all the tools for the job:

sudo apt-get install linux-headers-uname -r linux-restricted-modules-uname -r build-essential subversion

Once you have got all them you will need to use subversion to get the driver from the svn repo..
svn checkout http://svn.berlios.de/svnroot/repos/linux-uvc/

Then you need to compile and install

cd linux-uvc/linux-uvc/trunk
make
sudo make install

Plug the camera in and take a look at dmesg. It may [hopefully] give you the device listing for it... eg /dev/video1

Point your application at that device and see if it works.

I checked dmseg to see what had happened...

$ dmesg | grep -i vid
[ 0.000000] BIOS-provided physical RAM map:
[ 8.445959] Boot video device is 0000:00:02.0
[ 26.441798] input: Video Bus as /devices/LNXSYSTM:00/device:00/PNP0A03:00/device:2b/LNXVIDEO:00/input/input7
[ 26.489145] ACPI: Video Device [VID] (multi-head: yes rom: no post: no)
[ 26.497079] input: Video Bus as /devices/LNXSYSTM:00/device:00/PNP0A03:00/LNXVIDEO:01/input/input8
[ 26.537089] ACPI: Video Device [VID1] (multi-head: yes rom: no post: no)
[ 26.537246] input: Video Bus as /devices/LNXSYSTM:00/device:00/PNP0A03:00/LNXVIDEO:02/input/input9
[ 26.585000] ACPI: Video Device [VID2] (multi-head: yes rom: no post: no)
[ 2902.096243] Linux video capture interface: v2.00
[ 2902.175522] uvcvideo: Found UVC 1.00 device <unnamed> (046d:08c9)
[ 2902.189674] usbcore: registered new interface driver uvcvideo
[ 2902.189683] USB Video Class driver (v0.1.0)

After that it worked in skype, I needed to set the sound in device to be the microphone of the web cam in the options of skype...

Sound In: USB Device 0x46d:0x8c9 (hw:U0x46d0x8c9,0)
Sound Out: Default device(default)
Ringing: Default device(default)

and in the video settings of skype...

Select webcam: UVC Camera (0x46d:0x8c9) (/dev/video0)

I am still currently having a weird issue where it seems that skype takes over the sound of all other applications. If I try and play some music after a skype call then it won't play until I reboot. Also it seems I'm allowed a max of 1 skype call before the sound in skype doesn't work, and therefore doesn't let me make or receive anymore calls. Again it only seems at the moment that a reboot will fix it. For the moment I will live with this as I don't use skype that often anyway. (If anyone has any advice for this part please leave a comment)

Technorati Tags: , , , ,,

How to Inject Spring Beans into Servlets Revisited.

Back in November I wrote a post about How to Inject Spring Beans into Servlets, since then one of the comments made on the post by angel, mentioned an interface and Servlet that comes with spring that does this the proper spring way. Thank you very much angel, this is what I was looking for before I wrote the last post 🙂

The proper way how this should be achieved is to write a class that implements HttpRequestHandler and define this class in your applicationContext.xml. Then you define a servlet in web.xml with a class of HttpRequestHandlerServlet and make sure the name of the servlet is the same as the bean you defined in applicationContext.xml.

First lets write a class that we want to inject into a servlet…

package name.kayley.httprequesthandlertest;

public interface ServiceToInject {
  public String someMethod();
}
package name.kayley.httprequesthandlertest;

public class ServiceToInjectImpl implements ServiceToInject {
  public String someMethod() {
      return "someMethod called";
  }
}

Next we’ll write a class that implements the HttpRequestHandler this is basically our servlet.

package name.kayley.httprequesthandlertest;

public class MyHttpRequestHandler implements HttpRequestHandler {

   private ServiceToInject serviceToInject;

   public void setServiceToInject(ServiceToInject serviceToInject) {
       this.serviceToInject = serviceToInject;
   }

   public void handleRequest(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {

       System.out.println("******* HELLO *******");
       System.out.println(serviceToInject.someMethod());
   }
}

Next all we need to do is configure our web.xml and applicationContext.xml…

First applicationContext.xml…

<xml version="1.0" encoding="UTF-8">
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="serviceToInject" class="name.kayley.httprequesthandlertest.ServiceToInjectImpl"/>

    <bean id="myhttprequesthandler" class="name.kayley.httprequesthandlertest.MyHttpRequestHandler">
        <property name="serviceToInject" ref="serviceToInject"/>
    </bean>

</beans>

And finally configure your web.xml …

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>myhttprequesthandler</servlet-name>
        <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myhttprequesthandler</servlet-name>
        <url-pattern>/MyServlet</url-pattern>
    </servlet-mapping>
</web-app>

Now run your web app and go to http://localhost:8080/MyServlet

You should see in your console log the output of your injected servlet…

******* HELLO *******
someMethod called

Brilliant 🙂 Thanks again angel.

Technorati Tags: , , , ,