• Post Reply Bookmark Topic Watch Topic
  • New Topic

Injection in Spring

 
Prav Chau
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean.
You can just call setProperty in Java code.
 
Prav Chau
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!