|
|
Questions on Spring-Config.
Hi, I have some basic questions about Spring.
1. How do I pass Dates in the config file? This line is throwing a String to Date conversion error.
lt;property name=quot;del_version_datequot; value=quot;12/27/2007quot; /gt;
2. How can I bind a property to the return value of a function, instead of hardcoding it into the bean-config file.
e.g., I want the del_version_date to get the value new Date(System.currentTimemillis);.
3. How do I generate tons of beans? Lets say I want 20 beans for my clients. Do I have to write 20 data sets in the bean config or is there another strategy.
Thanks in advance.
Bump!!!!!!!
You bump fast ... !
From your question (especially point 3.), I have the feeling that you are trying to declare entities (the objects that should be in the database) in your spring config ... tell me if I am wrong ...
For point 1, have a look at the manual 5.4.2 and following. Basically, you have to register a CustomDateEditor.
For point 2, no idea.
For point 3, I'd like to know a bit more about what you are trying to do. Why do you need 20 instances of the same bean ? Mabe what you want is a prototype scope, but I have a feeling that there is something wrong in your understanding of Spring ...
Originally Posted by gehelFor point 2, no idea.
Probably, not the best solution, but it should workCode:
public class DateProvider { public Date getCurrentDate() { return new Date(); }
}
and in contextCode:
lt;bean id=quot;dateProviderquot; class=quot;DateProviderquot;/gt;
lt;bean id=quot;iNeedCurrentDatequot; class=quot;INeedCurrentDatequot;gt; lt;property name=quot;del_version_datequot; ref=quot;dateProvider.currentDatequot; /gt;
lt;/beangt;
Originally Posted by gehelYou bump fast ... !
From your question (especially point 3.), I have the feeling that you are trying to declare entities (the objects that should be in the database) in your spring config ... tell me if I am wrong ...
For point 1, have a look at the manual 5.4.2 and following. Basically, you have to register a CustomDateEditor.
For point 2, no idea.
For point 3, I'd like to know a bit more about what you are trying to do. Why do you need 20 instances of the same bean ? Mabe what you want is a prototype scope, but I have a feeling that there is something wrong in your understanding of Spring ...
Yes, I am just starting to learn Spring. Thanks for the answers above.
Originally Posted by al0
Code:
lt;bean id=quot;dateProviderquot; class=quot;DateProviderquot;/gt;
lt;bean id=quot;iNeedCurrentDatequot; class=quot;INeedCurrentDatequot;gt; lt;property name=quot;del_version_datequot; ref=quot;dateProvider.currentDatequot; /gt;
lt;/beangt;Exactly what am looking for... thanks!
I had figured it out up to a custom DateProvider, but did not know how to bind the property (del_version_date) to ref-bean dateProvider's specific property (currentDate). So the answer is the dot notation - dateProvider.currentDate.
Thanks a lot.
Sorry, but my answer was partially wrong, you can not just use dot notation,
it should be Code:
lt;bean id=quot;iNeedCurrentDatequot; class=quot;INeedCurrentDatequot;gt; lt;property name=quot;del_version_datequot;gt; lt;util:property-path id=quot;currentDatequot; path=quot;dateProvider.currentDatequot;/gt; lt;/propertygt;
lt;/beangt;
or, alternativelyCode:
lt;bean id=quot;iNeedCurrentDatequot; class=quot;INeedCurrentDatequot;gt; lt;property name=quot;del_version_datequot;gt; lt;bean id=quot;testBean.agequot; class=quot;org..beans.factory.config.PropertyPathFactoryBeanquot;/gt; lt;/propertygt;
or
Code:
lt;bean id=quot;iNeedCurrentDatequot; class=quot;INeedCurrentDatequot;gt; lt;property name=quot;del_version_datequot;gt; lt;bean factory-bean=quot;dateProviderquot; factory-method=quot;getCurrentDatequot;/gt; lt;/propertygt;
Likely, more alternatives may be found.
Regards,
OleksandrOriginally Posted by jaspreet_incExactly what am looking for... thanks!
I had figured it out up to a custom DateProvider, but did not know how to bind the property (del_version_date) to ref-bean dateProvider's specific property (currentDate). So the answer is the dot notation - dateProvider.currentDate.
Thanks a lot.
Originally Posted by al0Sorry, but my answer was partially wrong, you can not just use dot notation,
it should be
Interestingly, simple dot notation did work, and I see the date value inserted into the DB (12/28/2007 12:00:00 AM). Here is my code:Code:
lt;!-- TODO - How to bind this property to a function call? --gt;
lt;property name=quot;del_version_datequot; ref=quot;dateGenerator.datequot; /gt;
lt;bean id=quot;dateGeneratorquot; class=quot;hibernate.spring.DateGeneratorquot;gt;
lt;descriptiongt;Returns the current time.lt;/descriptiongt;
lt;/beangt;package hibernate.spring;
import java.sql.Date;
public class DateGenerator {
@SuppressWarnings(quot;unusedquot;)
private Date date;
public Date getDate() {
return new Date(System.currentTimeMillis());
}
}Yes, now I recognized it, see 3.3.3.7 in Spring 2.0.7 Reference.
Approaches from my second post you need if you want not just set bean property, but create a separate bean from value of some property.Originally Posted by jaspreet_incInterestingly, simple dot notation did work, and I see the date value inserted into the DB (12/28/2007 12:00:00 AM). Here is my code:Code:
lt;!-- TODO - How to bind this property to a function call? --gt;
lt;property name=quot;del_version_datequot; ref=quot;dateGenerator.datequot; /gt;
lt;bean id=quot;dateGeneratorquot; class=quot;hibernate.spring.DateGeneratorquot;gt;
lt;descriptiongt;Returns the current time.lt;/descriptiongt;
lt;/beangt;package hibernate.spring;
import java.sql.Date;
public class DateGenerator {
@SuppressWarnings(quot;unusedquot;)
private Date date;
public Date getDate() {
return new Date(System.currentTimeMillis());
}
}
Originally Posted by al0Yes, now I recognized it, see 3.3.3.7 in Spring 2.0.7 Reference.
Sorry to confuse you, but the dot notation DID NOT WORK.
I am trying one your alternatives, but nothing is working yet.
To simplify, can you illustrate in a different example please?
Lets say bean A has a property propa, whose value needs to be the return of bean B's method funcb. Code:
class A { private Date propa; public Date getPropa(){
return propa;
}
public void setPropa(Date propa){
this.propa = propa;
}}
class B { public Date funcb { return new Date(System.currentTimeMillis()); }
}
Whats the entry for
lt;property name=quot;propaquot; value=quot;??quot; ref=quot;??quot;gt;
and
Bean B entry to return funcb to propa?
Thank you. And also where can I read up on this? |
|