Friday, June 4, 2010

Spring recipes - Chapter 3

Ok, I tried to import Chapter 3 last night, but ran into an issue. First of all, there were 2 chapter 3 projects, so some kind of conflict - only one was imported, I think.

Secondly, the code that I looked at didn't exactly look like the code from the book.

I've been thinking about, instead of importing the projects, creating them myself. This will give me a better feel for how to create project from scratch. So let's try that.

Ok, I just created a project called "MySequence1". First, let's create the SequenceGenerator class by copying from the books code. Ok, there's two projects, but only one has SequenceGenerator, and that's the "SequenceDeclaration" project.

Rats. It's annotated code, not the same. It looks like I might have downloaded the code from the next version of the book. Let's try to find the 2.5 version.

Hmm...it was published with 2008 version. Well, he must have changed it. Blah.

Ok, - let's just see if we can run it.

Change java to 1.5 in eclipes.

Run the java main class.

Ok, I got this output:

20100604100000-0005-0010-0020
20100604100001-0005-0010-0020

So, what happened?

Here's main:

package com.apress.springrecipes.sequence;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");

SequenceGenerator generator =
(SequenceGenerator) context.getBean("sequenceGenerator");

System.out.println(generator.getSequence());
System.out.println(generator.getSequence());
}
}

It got the context from beans.xml.

Here's beans.xml.

beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">







class="com.apress.springrecipes.sequence.SequenceGenerator">





class="com.apress.springrecipes.sequence.ReverseGenerator" />









class="com.apress.springrecipes.sequence.DatePrefixGenerator">




5
10
20



So main instantiates the Sequence Generator.



Parent?

class="com.apress.springrecipes.sequence.SequenceGenerator">






What does mean? It's trying to inject suffixes...

Suffixes takes a list:

public void setSuffixes(List suffixes) {
this.suffixes = suffixes;
}

Where are the Integers?

Here:


5
10
20



So the is how it was identified.

Plus, besides having the list of suffixes, it will have





an "initial" value of "100000"

Ok, now this:

System.out.println(generator.getSequence());
System.out.println(generator.getSequence());

Which generated this:

20100604100000-0005-0010-0020
20100604100001-0005-0010-0020

Here the method

public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer(); // create a string buffer

buffer.append(prefixGenerator.getPrefix()); // see below - it's the date
buffer.append(initial + counter++); // initial is 100000; counter inits to 0;

// so now we have 20100604100000

// use a decimal formatter to setup a format of four zeros.
// iterate through them,
// Append the formatted suffixes.

DecimalFormat formatter = new DecimalFormat("0000");
for (int suffix : suffixes) {
buffer.append("-");
buffer.append(formatter.format(suffix));
}
return buffer.toString();
}

Ok, where's the prefix generator coming from?

Looks like this in the source:

@Resource(name = "datePrefixGenerator")
private PrefixGenerator prefixGenerator;


Heres the xml:

class="com.apress.springrecipes.sequence.DatePrefixGenerator">



Since there's not setter, obviously the @Resource annotation filled in

private PrefixGenerator prefixGenerator;

from the XML.


Ok, if you look back to some of my comments in the source code, they were like this:


public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer(); // create a string buffer

buffer.append(prefixGenerator.getPrefix()); // see below - it's the date
buffer.append(initial + counter++); // initial is 100000; counter intits to 0;

// so now we have 20100604100000

// use a decimal formatter to setup a format of four zeros.
// iterate through them,
// Append the formatted suffixes.

DecimalFormat formatter = new DecimalFormat("0000");
for (int suffix : suffixes) {
buffer.append("-");
buffer.append(formatter.format(suffix));
}
return buffer.toString();
}


And voila. That's Lesson 3, part 1.

No comments:

Post a Comment