GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — spring-framework-issues/SPR-8482 at master · nickstreet/spring-framework-issues

Expand file tree

/

SPR-8482

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

SPR-8482

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
src
src
 
 
README.markdown
README.markdown
 
 
pom.xml
pom.xml
 
 

README.markdown

A demonstration of Spring 3.1's new CommandLinePropertySource support.

The simple main method in Spr8482Main wires up a Spring ApplicationContext with a single 'Greeter' bean having a sayGreeting() method.

You'll notice that Greeter uses @Inject to get hold of the Spring Environment object, and when sayHello() is called, tries to look up a property from the environment named 'greeting'. The environment may contain any number of property sources, and by default looks to the system properties and system enviroment variables.

Try running this main method as-is. You'll see that the 'Greeter' bean says "Welcome!", as a default greeting when no 'greeting' property is present.

Try running the class with a command line argument of "--greeting='Wilkommen'", and you'll see that the greeting changes. This is because a CommandLinePropertySource has been configured and added to the Spring application context's environment. You can do this from within Eclipse using the Run->Run Configurations menu, or from the Maven command line with the following:

$ mvn package exec:java -Dexec.mainClass="org.springframework.issues.Spr8482Main" -Dexec.args="--greeting=Wilkommen"

Now, try leaving the --greeting command line argument in place while also setting a 'greeting' environment variable when running the application:

$ greeting=Bienvenidos  mvn package exec:java -Dexec.mainClass="org.springframework.issues.Spr8482Main" -Dexec.args="--greeting=Wilkommen"

You'll notice that 'Bienvenidos' is now the greeting. This is because the system environment property source has higher precedence in the application context's Environment than does our command-line property source. Notice how the command line property source was added with the #getLast() method? Try making this #getFirst() and see what happens!

As an additional excercise, consider replacing the JOptCommandLinePropertySource with Spring's own SimpleCommandLinePropertySource. See the Javadoc for details, but the idea with that variant is to provide the simplest possible approach to parsing command line opts.

Enjoy!

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.