Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Simple Example of the SPring 3.1 Environment Abstraction?

 
Bartender
Posts: 1674
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been looking all over the Internet for a simple abstraction example of the environment (books aren't yet available that cover this). Specifically, I would like an example to show how to configure a property in a configuration file like "jdbcDriver" that I could then access in a method in some application class. Most of the examples I've seen seem to take a hard right or left turn and veer away from the simple example I'm looking for.

The Spring source didn't have quite the example I was hoping for either.

Below is what I have working so far, but the last statement prints out null -- which is to be expected since I haven't configured, via an example, of the environment abstraction, so I'm unable to read a property set in a configuration file.



Look forward to any suggestions.

- mike
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what you are trying to do you should just use the @PropertySource annotation. Now I am assuming you are using a web application and that code snippet probably came out of an ApplicationInitializer class. That would be fine but unnecessary I think for your use case.

The thing I think your missing is the spring environment as in org.springframework.core.env.Environment. That is where you go to grab your properties.

Here is a stand alone application to illustrate but nothing changes for a web-app really.

Say you have a properties file called

src/main/resources/app.properties


Now lets say you have Config file that looks like this I like using Java based configuration. You can use the Java based configuration together with XML as well. If I were doing XML only i would probably just specify the location of my property file on the propertySourcesPlaceholderConfigurer directly.




Here is the main class


This would output the user.home directory followed by Hello World.

Does that help?
 
Mike London
Bartender
Posts: 1674
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bill Gorder wrote:For what you are trying to do you should just use the @PropertySource annotation. Now I am assuming you are using a web application and that code snippet probably came out of an ApplicationInitializer class. That would be fine but unnecessary I think for your use case.

The thing I think your missing is the spring environment as in org.springframework.core.env.Environment. That is where you go to grab your properties.

Here is a stand alone application to illustrate but nothing changes for a web-app really.

Say you have a properties file called

src/main/resources/app.properties


Now lets say you have Config file that looks like this I like using Java based configuration. You can use the Java based configuration together with XML as well. If I were doing XML only i would probably just specify the location of my property file on the propertySourcesPlaceholderConfigurer directly.




Here is the main class


This would output the user.home directory followed by Hello World.

Does that help?



Bill,

That is perfect! Just what I needed. THANK YOU!!!

-------

Would you please also show me what you mean by the XML configuration case ("... just specify the location of my property file on the propertySourcesPlaceholderConfigurer directly.") ? That is the method we use.

I tried to add an @ImportResource("classpath:/config_file.xml") to the AppConfig.java file.

But, when I ran the sample code, it barfed since there were unresolved ${} placeholders in this xml config file.

What am I missing for the XML case?

Thanks,

-mike
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,

What you have there with the mix of XML and Java config should work. What properties are not being resolved? Did you declare the PropertySourcesPlaceHolder bean as static, like in my example?

I modified the example to prove this works:

AppConfig.java



Application.java



MyBean.java


src/main/resources/app-config.xml


app.properties



This prints out the same as before followed by 'bill'

If I was not going to use the mix (no @PropertySource annotation) you can load the properties file on the PropertySourcesPlaceholder bean.

 
Mike London
Bartender
Posts: 1674
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bill Gorder wrote:Hi Mike,

What you have there with the mix of XML and Java config should work. What properties are not being resolved? Did you declare the PropertySourcesPlaceHolder bean as static, like in my example?

I modified the example to prove this works:

AppConfig.java



Application.java



MyBean.java


src/main/resources/app-config.xml


app.properties



This prints out the same as before followed by 'bill'

If I was not going to use the mix (no @PropertySource annotation) you can load the properties file on the PropertySourcesPlaceholder bean.



Bill,

This is additional terrific information, thanks.

However, in our application, the placeholders that are failing have a bean setup like this:

<bean id="c3po" class="com.mchange.v2.c3p0.ComboPooledDataSource">
.
.
.
</bean>

How would I get the "jdbcUrl" property from that class?

I tried before posting again to do the @ImportResource as you did, which points to the servlet config file, but get a null for the property when I try to access it in code.

as in: String jdbcUrl = annotateEnvironment.getProperty("jdbcUrl");

The actual property is buried inside this bean definition in the xml config file:

<bean id="c3po" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" " value="${database.url.value}"/>
</bean>

I also tried to import the bean from the XML file sort of like your updated example did, but got a casting error.

Bean myBean = context.getBean("...")

Tried with both the beanid and the class name (ComboPooledDataSource).

Does this additional information help?

Thanks again....truly appreciate your superb examples

- mike


 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should work the same way.... Are you storing your JDBC information in a properties file? It is really no different then the example I gave with the MyBean injecting a URL. You might be running into context scope problems if this is a web application and you are scoping the propertySourcesPlaceholderConfigurer to the servlet context rather than the root context. I said much the same in your other post. Lets choose one of these thread to continue the discussion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!