Inject Java 5 Enums with Spring

This is a quick entry about how to "inject" a Java 5 enum using the Spring Framework. This may not be a common problem, or even considered an obstacle for the average Spring user. I wouldn't consider myself an average Spring user as my involvement with Spring is limited to a small domain of the product I work on. Either way, I thought it would be nice to share my solution with others...

The main players in this example are:

Until I can find a good way to embed code/xml in WordPress, the code will only be viewable via the links provided.

You'll notice in the applicationContext.xml that the Status enum has been configured twice. Once as it's own bean, Status.ACTIVE, and the other as an anonymous "inner bean" of the accountService bean's defaultStatus property value. In either case you'll notice that each bean definition declares the attribute, factory-method="valueOf". This declaration tells the Spring Framework to use the static factory method, valueOf on the Enum class. We'll also declare a constructor-arg value with the name of the enum, either ACTIVE or PENDING. In short, the enum will be constructed using the Status.valueOf( "ACTIVE" ), or Status.valueOf( "PENDING" ).

The sample code and eclipse project can be downloaded in it's entirety here. This project requires Spring Framework 1.2.8 and Commons - Logging.  Oh... and of course, JUnit.

Spread the word... These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Slashdot
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Technorati

Flex 2: How to get URL query-string variables within Flex application

This has been a reoccurring problem on flexcoders and in the small circle of Flex developers I know. It is a problem that I had encountered and thought that I had found an answer; Abdul Qabiz's Blog posting (duplicated) had described a (in his own words) "quick-and-dirty" solution. That method works well for pulling the String value of the parameters passed in the query-string, but what do you do when those values are URL Encoded? Below is my (and I use my very loosely as it isn't much to claim) solution that provides similar behavior as described in Abdul's posting, but requires significantly less code and is much more readable:

Source and Usage

Given the URL:
http://deepthoughts.orsomethinglikethat.com?myParam=My%20Value

the return value is "My Value".

This may not be the ideal solution if the URL is pointed directly to the SWF or if you choose to make your parameters available via FlashVars.

Spread the word... These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Slashdot
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Technorati

Token Phrases

There is a new page containing a compilation of my favorite "Token Phrases." The phrases themselves may not be of any special meaning to anyone but myself and a handful of others, but the definitions themselves may touch your heart.

It's a work in progress so feel free to comment to this post with your favorite phrases and their universally understood definitions.

Spread the word... These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Slashdot
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Technorati

Flex 2: addExternalEventListener( … )

I've recently been looking for a way to invoke an ActionScript/Flex function based on a browser event… say window.unload (orsomethinglikethat). The ability to hook into Application creationComplete is present, however I am unaware of any built-in Flex/FlashPlayer unload/shutdown hook points.

I’ll now turn my attention to letting the browser notify the Flex Application of events. There are a couple of ways to accomplish this, both of which involve using the flash.external.ExternalInterface.addCallBack() to expose a Flex function to the containing browser:

    • Embed or import JavaScript in the Flex application’s HTML wrapper to hard code the browser event handlers to invoke the exposed Flex callback functions.
    • Assign the browser event handlers in the Flex code.

Both options have a time and place, but for my purposes, I’ll choose the latter option. I prefer not to maintain additional JavaScript and/or custom HTML wrapper code, and ideally I’d like to be able to directly map a browser event to a Flex function.

Source and Usage

Steps #1 & 3 are uninteresting and strictly interact with ExternalInterface.
Step #2 is a bit more interesting as it involves constructing the JavaScript statement that binds the browser event to the callBackAlias.
Since ExternalInterface.call() uses the functionName parameter as a pointer to a function that has been defined in the tag, it is not possible to pass in a String of JavaScript as we would with the JavaScript eval() function. Instead we “wrap” our JavaScript in an anonymous function to be executed upon ExternalInterface.call() invocation. The script we have just “wrapped” assigns the browser event handler to another anonymous function that contains the script to invoke the provided Flex callBack function by the provided alias.

Categories

  • Blogroll

  • Monthly Archives