permaculture playing cards*
The moose likes Spring and the fly likes Injection in Spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Injection in Spring" Watch "Injection in Spring" New topic

Injection in Spring

Prav Chau
Ranch Hand

Joined: Jul 17, 2007
Posts: 58
How can I set property for Bean class using java code instead of applicationcontext.xml file.

Consider I have spring bean declared in applicationcontext.xml. I can set it using <property> tag but I want to use java code to set this property as its dynamic.

Thanks in Advance

Prav Chau
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
I'm not sure what you mean.
You can just call setProperty in Java code.

SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Prav Chau
Ranch Hand

Joined: Jul 17, 2007
Posts: 58
Sure. I want to inject that property using Spring Framework. In below xml we set poolName member variable of MyDataSource class it in applicationcontext.xml file. I want to set it through java code.

<bean id="myDataSource" class="springexample.hibernate.MyDataSource">
<property name="poolName">
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Sorry, could you please to explain more? I don't get you.

You can just call myDataSource.setPoolName("POOLLL") in Java code, but I doubt that is your question.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249

Look for the PropertyPlaceholderConfigurer class. I will allow you to put a {$} in its place that you can dynamically change later, but not in code, just in a properties file. I assume you change the datasource based on whether you are deploying on a developers box, test or production. So the best solution is to have each location have their own property file that gets loaded.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Injection in Spring
Similar Threads
404 in using acegi security with Spring
JMS implementation using JNDI in spring application
XML name space error in Spring data JPA hibernate applicationContext.xml
getting property value in jsp page from
Invocation of init method failed; exception is org.hibernate.InvalidMappingException