java ee - Factory class with database connection in spring -
i need write factory class calls database , stores data static object. know how write normal java. not able in spring. able pattern, not getting database connection when "factory-method" called. below code
public class applicationsettingsdao { private static applicationsettingsdao objsettings = null; // new applicationsettingsdao(); @autowired private datasource datasource; private applicationsettingsdao() { getdbconncection(); } public void setdatasource(datasource datasource) { this.datasource = datasource; } public datasource getdatasource() { return this.datasource; } public static applicationsettingsdao getinstance() { if (objsettings == null) { synchronized (applicationsettingsdao.class) { if (objsettings == null) { objsettings = new applicationsettingsdao(); } } } return objsettings; } public void getdbconncection() { system.out.println("datasource -> " + datasource); try { connection objconnection = datasource.getconnection(); preparedstatement objprepstatement = objconnection.preparestatement("select applicationsettingid, settingid, value applicationsetting"); resultset objresultset = objprepstatement.executequery(); while(objresultset.next()) { system.out.println("--> " + objresultset.getint("settingid") + " >> " + objresultset.getstring("value")); } } catch (sqlexception e) { e.printstacktrace(); } } }
bean configuration
<bean id="objsettings" class="com.schoolcity.stars.dao.common.applicationsettingsdao" factory-method="getinstance" > <property name="datasource" ref="datasource"/> </bean>
am not able datasource object on container startup. works if call
applicationsettingsdao.getinstance().getdbconncection();
i need invoke getdbconncection() when getinstance() called.
thank you
Comments
Post a Comment