Tuesday, May 29, 2007

Environment-specific config with Spring

The Problem


After a jaunt working with Rails I'm back in Java-land. Now that I've tasted the sweet simplicity I'm jonesin' to feed it back into my Java projects.

So here's a little hack that let me apply my Rails idiom of environment/[development|test|production] to the Java world.

The Implementation

The config files

development.properties:
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@...

hibernate.dialect=org.hibernate.dialect.OracleDialect
...

test.properties:
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:mem:unittest
jdbc.username=sa
jdbc.password=

hibernate.dialect=org.hibernate.dialect.HSQLDialect
Same deal with production.properties

Creating the ApplicationContext

It's not a web-app so I'm doing it programatically.
beanFactory = new ClassPathXmlApplicationContext(new String[] {"beans.xml"}, false);

PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
configurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);

String environment = System.getProperty("environment", "development");
File customProperties = new File("/etc/conf/custom.properties");
configurer.setLocations(new Resource[] {new ClassPathResource(environment + ".properties"), new FileSystemResource(customProperties)});

beanFactory.addBeanFactoryPostProcessor(configurer);
beanFactory.refresh();

Unit tests

I also created a mini spring file beans-test.xml:
<beans>
<bean class=\"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer\">
<property name=\"locations\">
<value>classpath:test.properties</value>
</property>
</bean>
</beans>
All it does is load in my test.properties.
My test superclass
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

public abstract class TestSupport extends AbstractDependencyInjectionSpringContextTests {
public AbstractMdsIntegrationTestSupport() {
setPopulateProtectedVariables(true);
}

@Override
protected String[] getConfigLocations() {
return new String[] {"classpath:/beans-test.xml", "classpath:/beans.xml"};
}
}

Results

Now I can check out a clean codebase and run the unit tests and also fire up my application with the main() function, both from my IDE with no flag, properties file editing, etc. Then I can switch to production mode with just a -Denvironment=production.

You may have caught the customProperties file, that's how I keep from having to wrangle my production passwords into the classpath or system properties.

24 comments:

Anonymous said...

bbw bisexual personals black [url=http://loveepicentre.com/]senior christian dating help[/url] pros and cons of tl dating http://loveepicentre.com/ internet dating leeds

Anonymous said...

Find a Dell Laptop Battery [url=http://www.hqlaptopbatteries.com/-g2-laptopbatterymodel856.html]Discount Laptop Batteries[/url] laptop review http://www.hqlaptopbatteries.com/-zt1000-laptopbatterymodel560.html Samsung laptop battery
Discount Laptop Batteries [url=http://www.hqlaptopbatteries.com/-i1370-laptopbatterymodel450.html]AC Adapter[/url] replacement laptop batteries http://www.hqlaptopbatteries.com/-3003wlm-laptopbatterymodel1374.html laptop review
compare laptop prices [url=http://www.hqlaptopbatteries.com/-2755dvd-laptopbatterymodel34.html]excellent laptop battery[/url] laptop review http://www.hqlaptopbatteries.com/page4.html compare prices laptop batteries

Anonymous said...

Hi,

When ever I surf on web I come to this website[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url]You have really contiributed very good info here tastybyte.blogspot.com. Let me tell you one thing guys, some time we really forget to pay attention towards our health. In plain english I must warn you that, you are not serious about your health. Research displays that almost 80% of all U.S. adults are either fat or weighty[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url] So if you're one of these people, you're not alone. In fact, most of us need to lose a few pounds once in a while to get sexy and perfect six pack abs. Now next question is how you can achive quick weight loss? [url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]Quick weight loss[/url] is really not as tough as you think. You need to improve some of you daily habbits to achive weight loss in short span of time.

About me: I am webmaster of [url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]Quick weight loss tips[/url]. I am also mentor who can help you lose weight quickly. If you do not want to go under difficult training program than you may also try [url=http://www.weightrapidloss.com/acai-berry-for-quick-weight-loss]Acai Berry[/url] or [url=http://www.weightrapidloss.com/colon-cleanse-for-weight-loss]Colon Cleansing[/url] for effective weight loss.

Anonymous said...

drug impact body [url=http://usadrugstoretoday.com/products/karela.htm]karela[/url] pictures of an open heart http://usadrugstoretoday.com/products/cystone.htm optical fibers in medical instruments http://usadrugstoretoday.com/products/uroxatral.htm
zoloft without prescription [url=http://usadrugstoretoday.com/products/noroxin.htm]noroxin[/url] medical billing clearing house [url=http://usadrugstoretoday.com/products/vantin.htm]macbeth imagery darkness blood nature super natural[/url]

Anonymous said...

next depression [url=http://usadrugstoretoday.com/products/compazine.htm]compazine[/url] miami heart institute diet http://usadrugstoretoday.com/catalogue/l.htm micro min pin health history http://usadrugstoretoday.com/categories/weight-loss.htm
asthma onset in adulthood [url=http://usadrugstoretoday.com/catalogue/i.htm]No prescription online pharmacy[/url] how many people have bipolar disorder [url=http://usadrugstoretoday.com/products/diclofenac.htm]diet and nutrition facts and food pyriamid[/url]

Anonymous said...

mexican pharmacys online [url=http://usadrugstoretoday.com/categories/party-pills.htm]party pills[/url] medical wales http://usadrugstoretoday.com/catalogue/f.htm journals on visualization and its effects on health http://usadrugstoretoday.com/categories/skin-care.htm
the sacred heart of jesus [url=http://usadrugstoretoday.com/products/shallaki.htm]shallaki[/url] average size for 15 year olds penis [url=http://usadrugstoretoday.com/products/motrin.htm]nurses and handwashing and infection in nicu[/url]

Anonymous said...

ospa pharmacy [url=http://usadrugstoretoday.com/products/anafranil.htm]anafranil[/url] guide to diabetes tests http://usadrugstoretoday.com/categories/cholesterol.htm penis enlagement surgery http://usadrugstoretoday.com/products/tricor.htm
seperation and health insurance [url=http://usadrugstoretoday.com/products/calcium-carbonate.htm]calcium carbonate[/url] low carbohydrate diet [url=http://usadrugstoretoday.com/products/ed-strips.htm]diet book skinny[/url]

Anonymous said...

network medical [url=http://usadrugstoretoday.com/products/roxithromycin.htm]roxithromycin[/url] what dissolves red blood cells http://usadrugstoretoday.com/products/tofranil.htm drug grants http://usadrugstoretoday.com/products/omnicef.htm
how to have a orgasm [url=http://usadrugstoretoday.com/products/maxaman.htm]maxaman[/url] gag reflex anxiety [url=http://usadrugstoretoday.com/categories/gum.htm]free diet pills uk[/url]

Anonymous said...

http://www.liquidxhosting.com/Blah/viewtopic.php?f=2&t=119069 http://forum.stosstrupp-gold-germany.de/viewtopic.php?f=8&t=5060 http://bangladeshcallcenters.com/viewtopic.php?f=16&t=39501 http://www.ictbaltic.com/smf/index.php?topic=106835.new#new
http://www.snowsurge.com/forum/viewtopic.php?f=11&t=127939 http://cinerama.no/forum/viewtopic.php?f=2&t=293788 http://bc.virtualrevolution.nl/forum/viewtopic.php?f=5&t=11139 http://www.karmaultimate.com/forum/viewtopic.php?f=3&t=36031
http://hpcgi1.nifty.com/%7Ecsc/apeboard_plus.cgi?command=read_message&msgnum=20 http://www.thecirclemagazine.com/smf/index.php?topic=162062.new#new http://www.quotile-sequencer.com/phpBB3/viewtopic.php?f=6&t=267906

Anonymous said...

http://alwayshealth.in/smoke/kottonmouth-kings-smoke-weed
[url=http://alwayshealth.in/infection]medical drug lipitor[/url] fast urine drug screen results [url=http://alwayshealth.in/pravachol/pravachol-ineffective]pravachol ineffective[/url]
onlin3 pharmacy ratings http://alwayshealth.in/olmesartan/olmesartan-medoxomil-hctz
[url=http://alwayshealth.in/china-tea/ice-tea-psp]can only detect drugs taken orally[/url] uk softabs cailis levitra [url=http://alwayshealth.in/stress/water-stress]water stress[/url]
national health insurance in india http://alwayshealth.in/pain-relief/pain-relief-medication
[url=http://alwayshealth.in/pain-relief/lower-back-pain-ice-relief]drugs for seasonal affective disorder[/url] drug programs for the uninsured [url=http://alwayshealth.in/smoke/fire-and-smoke-damage-cleanup-ashland-ky]fire and smoke damage cleanup ashland ky[/url] mountain park health center arizona [url=http://alwayshealth.in/potency/potency-of-vitamins]potency of vitamins[/url]

Anonymous said...

domestic air travel canada http://atravel.in/tourism_knoxville-tourism best nz travel deals
[url=http://atravel.in/car-rental_airport-car-rental-tampa-florida-htm]liquids on plane travel[/url] travel weather on interstates [url=http://atravel.in/travel_how-far-does-light-travel]how far does light travel[/url]
disneyland travel packages http://atravel.in/disneyland_disneyland-cinderella-lp
[url=http://atravel.in/car-rental_buying-a-rental-car]travel supreme california[/url] aljo lite travel trailer [url=http://atravel.in/airline_airline-pilots-security-training]airline pilots security training[/url]
american express travel new york city http://atravel.in/expedia_expedia-discount-coupon-expedia-discount-coupon how do boat travel best [url=http://atravel.in/tour_the-cure-tour-dates]the cure tour dates[/url]

Anonymous said...

softball clothes http://topcitystyle.com/evening-cocktail-page3.html trendy clothes for women [url=http://topcitystyle.com/dolce-amp-gabbana-on-sale-brand2.html]the clothes rack for kids[/url] playboy shoes and clothing
http://topcitystyle.com/true-religion-brand17.html womens clothes shops online in the us [url=http://topcitystyle.com/white-blue-men-color55.html]girls dress shoes[/url]

Anonymous said...

adult share sites http://theporncollection.in/masturbating/how-to-get-mour-plessor-out-of-masturbating
[url=http://theporncollection.in/hentai-porn/steam-hearts-hentai]adult crack password[/url] free adult movie content [url=http://theporncollection.in/hentai-porn/scorpio-hentai-movie]scorpio hentai movie[/url]
sexy video from reality shows http://theporncollection.in/lesbian-xxx/lesbian-chic
[url=http://theporncollection.in/lesbian-xxx/lesbian-gay-community]spanking adult erotic video[/url] how to use yogurt in a squirting dildo [url=http://theporncollection.in/gay-male/gay-fotos]gay fotos[/url]
redway adult http://theporncollection.in/sex-mature/free-older-mature-women-pics
[url=http://theporncollection.in/mature-xxx/mature-lesbian-stories]manhandler dildo review[/url] free sexy lingerie thumbs [url=http://theporncollection.in/porn-dvd/the-best-of-justin-dragon-porn]the best of justin dragon porn[/url]
dragonballz with sailormoon hentai http://theporncollection.in/gay-porn/first-time-pictures-of-gay-sex
[url=http://theporncollection.in/lesbian-video/lesbian-vedio]lesbian dildo anal[/url] rigid dildo [url=http://theporncollection.in/gay-sex/gay-loutraki]gay loutraki[/url]

Anonymous said...

death sentence movie download [url=http://worldmovs.co.cc/full_version----------------------------07--------r------------dvd-rmvb.---------/20346database/]????????????(???????¤°?¶?°?07????????R?¶???????¬)(DVD-RMVB.???????»)[/url] arena district movie [url=http://worldmovs.co.cc/full_version-radarmen-from-the-moon/19288database/]Radarmen From The Moon[/url]
quantico movie theater [url=http://full-length-movies.com/dvd-quality-movie-what-happens-in-vegas/24704database/]What Happens in Vegas[/url] the movie white fang [url=http://full-length-movies.com/dvd-quality-movie-america-and-lewis-hine/16112database/]America And Lewis Hine[/url]
amateur allure movie [url=http://worldmovs.co.cc/full_version-der-sieg-des-glaubens--leni-riefenstahl-/6522database/]Der Sieg Des Glaubens (Leni Riefenstahl)[/url] madame x movie [url=http://full-length-movies.com/dvd-quality-movie-mithya/27214database/]Mithya[/url]
cbs movie code 11 14 [url=http://full-length-movies.com/dvd-quality-movie-ultimate-avengers/10254database/]Ultimate Avengers[/url] mmog movie [url=http://worldmovs.co.cc/full_version-moscow-zero/29216database/]Moscow Zero[/url]

Anonymous said...

character travel mugs http://xwg.in/flight/america-west-airline-flight-status gaeta to rome travel
[url=http://xwg.in/disneyland/three-star-hotels-north-disneyland]dive travel packages[/url] travel ti me [url=http://xwg.in/adventure/cherokee-health-adventure-quest]cherokee health adventure quest[/url]
anna travel agency meserole avenue brooklyn http://xwg.in/disneyland
[url=http://xwg.in/airline/best-airline-to-join-the-mile-high-club]international travel services immunizations kuwait[/url] travel bound tour operator [url=http://xwg.in/maps/maps-of-wakulla-county-fl]maps of wakulla county fl[/url]
travel destinations in south korea seoul http://xwg.in/airlines/continental-airlines-newarktoaruba packing light business travel [url=http://xwg.in/lufthansa/embraer-bandeirante]embraer bandeirante[/url]

Anonymous said...

health fair at walter reed [url=http://usadrugstoretoday.com/catalogue/f.htm]No prescription online pharmacy[/url] medical school fellowships http://usadrugstoretoday.com/categories/arthritis.htm
medical aesthetic equipment [url=http://usadrugstoretoday.com/products/proscar.htm]proscar[/url] online mexico pharmacy for retin a order [url=http://usadrugstoretoday.com/catalogue/e.htm ]diet before cardiac surgery [/url] back pain vs kidney
health increase great lose life food lbs years news diet [url=http://usadrugstoretoday.com/categories/anti-pilz.htm]anti pilz[/url] imaging3 fda approval http://usadrugstoretoday.com/categories/erectile-dysfunction.htm
how to identify pills [url=http://usadrugstoretoday.com/tos.htm]no prescription pharmacies[/url] low cost health insurance planspre existing [url=http://usadrugstoretoday.com/products/zestril.htm ]can anxiety make you lose weight fast [/url] bacteria in space

Anonymous said...

beach clothes apparel http://www.thefashionhouse.us/efor-casual-shirts-brand110.html aluminum furniture designer [url=http://www.thefashionhouse.us/north-sails-men-brand58.html]designer fabric pillow dog bed[/url] patty hughes fashion
http://www.thefashionhouse.us/black-leopard-print-v-neck-color43.html pics of jordan shoes [url=http://www.thefashionhouse.us/?action=products&product_id=1082]clothes cabinet[/url]

Anonymous said...

http://xwv.in/nervousness/nexium/nervousness
[url=http://xwv.in/oxcarbazepine/oxcarbazepine/tab]united health care pharmacy coverage[/url] ward cut rate drug [url=http://xwv.in/kamagra/kamagra/uk/cheap]kamagra uk cheap[/url]
social anxiety drugs list http://xwv.in/osteoporosis
[url=http://xwv.in/pantoprazole]online drug dealer[/url] drug documentaries [url=http://xwv.in/parlodel/watson/pharmacia/and/oxytrol]watson pharmacia and oxytrol[/url]
health care history http://xwv.in/naltrexone/naltrexone/opiate/test
[url=http://xwv.in/karela/karela/and/conan]pharmacy on sale miami florida[/url] the baton rouge advocate article on the drug alli [url=http://xwv.in/viagra/ultram/viagra/vioxx/xenical]ultram viagra vioxx xenical[/url] mereck drug screen [url=http://xwv.in/pantoprazole/is/protonix/a/generic/for/pantoprazole]is protonix a generic for pantoprazole[/url]

Anonymous said...

teen designer wallpaper series http://www.thefashionhouse.us/xs-dresses-size46.html aluminum furniture designer [url=http://www.thefashionhouse.us/replay-casual-brand48.html]womens shoes[/url] pam diabetic shoes
http://www.thefashionhouse.us/yellow-brown-women-color194.html zappos shoes [url=http://www.thefashionhouse.us/round-neck-page2.html]tourti galis architects urban designers[/url]

Anonymous said...

merrill shoes in ma http://luxefashion.us/on-sale-leather-jacket-type2.html dog clothes patterns [url=http://luxefashion.us/-v-neck-on-sale-category91.html]middle eastern fashion designers[/url] baby shoes
http://luxefashion.us/off-white-yellow-evening-cocktail-color209.html nike soccer shoes [url=http://luxefashion.us/54-sweaters-size9.html]childrens designer clothing[/url]

Anonymous said...

the lottery dream book http://wqm.in/blackjack_games-for-samsung-blackjack a2dp bluetooth stereo playback for blackjack
[url=http://wqm.in/slot_rail-honing-slot-tech]dv 2003 visa lottery winners[/url] betting nfl football online [url=http://wqm.in/roulette_on-line-play-roulette-game]on line play roulette game[/url]
betting poker casino http://wqm.in/jokers_clowns-to-the-left-of-me-jokers-to-the-right
[url=http://wqm.in/gambling-online_help-with-gambling-debt]casino crush forum[/url] topaz lodge and casino [url=http://wqm.in/poker-online_pirates-poker-dice-game]pirates poker dice game[/url]
blackjack supplies http://wqm.in/online-casinos_casinos-in-buffalo-ny tachi casino best odds slots [url=http://wqm.in/betting_bodog-betting-lines-and-stats]bodog betting lines and stats[/url]

Anonymous said...

auto ovens http://xwm.in/daihatsu/daihatsu-wakefield rent all auto care
[url=http://xwm.in/plymouth/plymouth-mi-entertainment]fallon mercedes[/url] auto trader online fl [url=http://xwm.in/oldsmobile/oldsmobile-big-block-crank-case-breathing]oldsmobile big block crank case breathing[/url]
william wessel auto http://xwm.in/citroen/citroen-c2-bluetooth-enable
[url=http://xwm.in/porsche/porsche-944-upgrades]stage one auto[/url] mercedes custom wheels [url=http://xwm.in/chopper/big-dog-chopper-south-jersey]big dog chopper south jersey[/url]
theme of the dodge ball movie http://xwm.in/nissan/nissan-titan-replacement-power-seat-switch
[url=http://xwm.in/bugatti/carlo-bugatti-furniture]wheelman auto sales[/url] mercedes 163 820 66 10 [url=http://xwm.in/chrysler]chrysler[/url]

Anonymous said...

using mircosoft vista sync center to sync blackjack phone http://lwv.in/gambling-online/casino-gambling-tips casino blackjack gambling bonus
[url=http://lwv.in/jackpot/jackpot-casino-appereanes]roulette game best internet casinos[/url] indian casinos of so california [url=http://lwv.in/roulette/blog-roulette-in-c]blog roulette in c[/url]
blackjack dvd cards strategy count mit minutes basic member team http://lwv.in/baccarat/baccarat-crystal-massena
[url=http://lwv.in/casino-playing-cards]yahoo msn lottery in london[/url] game lodge casino [url=http://lwv.in/online-casinos/debit-card-casinos]debit card casinos[/url]
make a lottery wheel http://lwv.in/joker/the-joker-comic-book-comparison bingo games to play [url=http://lwv.in/poker-online/video-poker-how-to-play]video poker how to play[/url]

Anonymous said...

alien movie mask [url=http://moviestrawberry.com/films/film_pete_s_dragon/]pete s dragon[/url] zune movie converters http://moviestrawberry.com/hqmoviesbygenres/download-genre_family-movies/?page=9 the rock movie by walt disney
show place movie times [url=http://moviestrawberry.com/films/film_the_master_of_disguise/]the master of disguise[/url] movie aion flex http://moviestrawberry.com/films/film_deep_winter/ big booty movie blogs
asian adult free movie trailers [url=http://moviestrawberry.com/films/film_nell/]nell[/url] the man of steel movie
new mexico movie camera rentals [url=http://moviestrawberry.com/films/film_the_kid_i/]the kid i[/url] the pavilion movie theater http://moviestrawberry.com/films/film_babylon_5_thirdspace/ cars movie clip art
life size movie [url=http://moviestrawberry.com/films/film_braindead/]braindead[/url] songs from the movie stick it without lyrics http://moviestrawberry.com/films/film_brothers_sisters/ high school musical the movie