首页 > 解决方案 > how to Inject Calendar getInstance in Spring , please

问题描述

<bean id="date" class="java.util.Calendar.getInstance" />

how can i inject Calendar instance Please help

i am confused what to use ,i am new to spring In date where new keyword is required is fine but here what have to do

标签: javaspring

解决方案


In a <bean> declaration class should specify the complete classname. getInstance is a factory method & hence it should be as follows:

<bean id="date" class="java.util.Calendar" factory-method="getInstance"/>

It might be better to use the java.time classes introduced in Java 8, rather than the old java.util.Calendar & java.util.Date.

<bean id="date" class="java.time.LocalDate" factory-method="now">

NB: Nevertheless, I can't think of a valid use case, to have the date as a bean.


推荐阅读