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
Author

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
SCJP, SCWCD, SCBCD, SCEA, http://pravinchaudhari.blogspot.com/
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">
<value>POOLLL</value>
</property>
</bean>
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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Look for the PropertyPlaceholderConfigurer class. I will allow you to put a {$property.name} 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.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
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 application.properties
Invocation of init method failed; exception is org.hibernate.InvalidMappingException