java - WARNING: Exception encountered during context initialization -
before work not working.and when worked use session null in credicarddao.java updating customer status , inserting new creditcard record database.
apr 14, 2017 9:32:36 org.springframework.context.support.filesystemxmlapplicationcontext preparerefresh info: refreshing org.springframework.context.support.filesystemxmlapplicationcontext@68de145: startup date [fri apr 14 09:32:36 cdt 2017]; root of context hierarchy apr 14, 2017 9:32:36 org.springframework.beans.factory.xml.xmlbeandefinitionreader loadbeandefinitions info: loading xml bean definitions file [c:\bcj_dec_2016\workspace\corejava\creditcardprocess\spring.xml] apr 14, 2017 9:32:37 org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter initcontrolleradvicecache info: looking @controlleradvice: org.springframework.context.support.filesystemxmlapplicationcontext@68de145: startup date [fri apr 14 09:32:36 cdt 2017]; root of context hierarchy apr 14, 2017 9:32:37 org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter initcontrolleradvicecache info: looking @controlleradvice: org.springframework.context.support.filesystemxmlapplicationcontext@68de145: startup date [fri apr 14 09:32:36 cdt 2017]; root of context hierarchy apr 14, 2017 9:32:37 org.springframework.context.support.filesystemxmlapplicationcontext refresh warning: exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.beancreationexception: error creating bean name 'creditcarddao' defined in file [c:\bcj_dec_2016\workspace\corejava\creditcardprocess\target\classes\com\bcj\creditcardprocess\dao\creditcarddao.class]: instantiation of bean failed; nested exception org.springframework.beans.beaninstantiationexception: failed instantiate [com.bcj.creditcardprocess.dao.creditcarddao]: constructor threw exception; nested exception java.lang.nullpointerexception exception in thread "main" org.springframework.beans.factory.beancreationexception: error creating bean name 'creditcarddao' defined in file [c:\bcj_dec_2016\workspace\corejava\creditcardprocess\target\classes\com\bcj\creditcardprocess\dao\creditcarddao.class]: instantiation of bean failed; nested exception org.springframework.beans.beaninstantiationexception: failed instantiate [com.bcj.creditcardprocess.dao.creditcarddao]: constructor threw exception; nested exception java.lang.nullpointerexception @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiatebean(abstractautowirecapablebeanfactory.java:1155) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1099) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:513) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:483) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:761) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:866) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:542) @ org.springframework.context.support.filesystemxmlapplicationcontext.(filesystemxmlapplicationcontext.java:140) @ org.springframework.context.support.filesystemxmlapplicationcontext.(filesystemxmlapplicationcontext.java:84) @ com.bcj.creditcardprocess.creditcardmain.main(creditcardmain.java:15) caused by: org.springframework.beans.beaninstantiationexception: failed instantiate [com.bcj.creditcardprocess.dao.creditcarddao]: constructor threw exception; nested exception java.lang.nullpointerexception @ org.springframework.beans.beanutils.instantiateclass(beanutils.java:154) @ org.springframework.beans.factory.support.simpleinstantiationstrategy.instantiate(simpleinstantiationstrategy.java:89) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiatebean(abstractautowirecapablebeanfactory.java:1147) ... 13 more caused by: java.lang.nullpointerexception @ com.bcj.creditcardprocess.dao.creditcarddao.(creditcarddao.java:22) @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:62) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45) @ java.lang.reflect.constructor.newinstance(constructor.java:423) @ org.springframework.beans.beanutils.instantiateclass(beanutils.java:142) ... 15 more
creditcardmain.java*
package com.bcj.creditcardprocess; import org.springframework.context.applicationcontext; import org.springframework.context.support.filesystemxmlapplicationcontext; import com.bcj.creditcardprocess.service.creditcardservice; public class creditcardmain { public static void main(string[] args) { @suppresswarnings("resource") applicationcontext context = new filesystemxmlapplicationcontext("spring.xml"); creditcardservice obj = (creditcardservice) context.getbean("ccardservice"); //creditcardservice ccardservice = new creditcardservice(); obj.processcreditcard(); } }
creditcardservice.java
package com.bcj.creditcardprocess.service; import java.util.iterator; import java.util.list; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import com.bcj.creditcardprocess.dao.creditcarddao; import com.bcj.creditcardprocess.model.customer; @service public class creditcardservice { @autowired private creditcarddao ccarddao; public void setccarddao(creditcarddao ccarddao) { this.ccarddao = ccarddao; } public void processcreditcard() { list<customer> customerlist = ccarddao.getcustomerlist(); // creating pool of 5 threads executorservice executor = executors.newfixedthreadpool(5); (iterator iterator = customerlist.iterator(); iterator.hasnext();) { customer cust = (customer) iterator.next(); system.out.println(cust); workerthread thread = new workerthread(); thread.threadmain(customerlist); //runnable worker = new workerthread(cust); // calling execute method of executorservice //executor.execute(worker); /*workerthread thread = new workerthread(); thread.threadmain(customerlist); } executor.shutdown(); while (!executor.isterminated()) { } system.out.println("finished threads");*/ /* * list<object> result = (list<object>) customerlist; iterator itr = * result.iterator(); while(itr.hasnext()){ object[] obj = (object[]) * itr.next(); * * system.out.println(string.valueof(obj[0])+" "+string.valueof(obj[1] * )); * */ /* * (int = 0; < 10; i++) { runnable worker = new workerthread(obj * ); executor.execute(worker);//calling execute method of * executorservice } */ /*executor.shutdown(); while (!executor.isterminated()) { } system.out.println("finished threads");*/ } } }
workerthread.java
package com.bcj.creditcardprocess.service; import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import java.util.iterator; import java.util.list; import java.util.random; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import com.bcj.creditcardprocess.dao.creditcarddao; import com.bcj.creditcardprocess.model.creditcard; import com.bcj.creditcardprocess.model.customer; @service public class workerthread implements runnable { @autowired private creditcarddao ccarddao; public void setccarddao(creditcarddao ccarddao) { this.ccarddao = ccarddao; } public workerthread() { } workerthread(customer cust2) { this.cust = cust2; } customer cust; public void run() { system.out.println(thread.currentthread().getname() + " (start) message = " + cust.getfirstname()); try { getcutomerdetailsbytextfile(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println(thread.currentthread().getname() + " (end)"); } public synchronized void getcutomerdetailsbytextfile() throws ioexception { filereader fr = new filereader("details.txt"); bufferedreader br = new bufferedreader(fr); string line = ""; while ((line = br.readline()) != null) { system.out.println(line); string[] details = line.split(" "); if (details[0].equalsignorecase(cust.getfirstname()) && details[1].equalsignorecase(cust.getlastname()) && details[2].equals(cust.getssn())) { system.out.println(" ssn had"); if (integer.parseint(details[3]) > 700 && integer.parseint(cust.getannualincome()) > 100000) { creditcard card = new creditcard(); card.setcreditlimit(5000); card.setcustomerid(cust.getid()); generatecardnumber(card, cust); cust.setstatus("approved"); } else if (integer.parseint(details[3]) > 600 && integer.parseint(details[3]) < 700 && integer.parseint(cust.getannualincome()) > 70000) { creditcard card = new creditcard(); card.setcreditlimit(3000); card.setcustomerid(cust.getid()); generatecardnumber(card, cust); cust.setstatus("approved"); } else { ccarddao.updatecustomer(cust); cust.setstatus("declined"); } break; } } } public void threadmain(list<customer> customerlist) { executorservice executor = executors.newfixedthreadpool(5); (iterator iterator = customerlist.iterator(); iterator.hasnext();) { customer cust = (customer) iterator.next(); runnable worker = new workerthread(cust); executor.execute(worker);// calling execute method of // executorservice } executor.shutdown(); while (!executor.isterminated()) { } system.out.println("finished threads"); } private void generatecardnumber(creditcard ccard, customer cust) { random rand = new random(); string cardnumber = integer.tostring(rand.nextint(9999) + 1000) + integer.tostring(rand.nextint(9999) + 1000) + integer.tostring(rand.nextint(9999) + 1000) + integer.tostring(rand.nextint(9999) + 1000); string cvv = integer.tostring(rand.nextint(999) + 100); calendar cal = calendar.getinstance(); date today = cal.gettime(); cal.add(calendar.year, 3); // previous year add -1 date next = cal.gettime(); simpledateformat adf = new simpledateformat("mm/yy"); string expirydate = adf.format(next); ccard.setcardnumber(cardnumber); ccard.setcvv(cvv); ccard.setexpirydate(expirydate); ccarddao.persistcreditcard(ccard, cust); } }
creditcarddao.java
package com.bcj.creditcardprocess.dao; import java.util.list; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.repository; import org.springframework.transaction.annotation.transactional; import com.bcj.creditcardprocess.model.creditcard; import com.bcj.creditcardprocess.model.customer; @repository @transactional public class creditcarddao { @autowired private sessionfactory sessionfactory; private session session = sessionfactory.getcurrentsession(); public sessionfactory getsessionfactory() { return sessionfactory; } public void setsessionfactory(sessionfactory sessionfactory) { this.sessionfactory = sessionfactory; } // session session = sessionfactory.getcurrentsession(); list<customer> custs; public list<customer> getcustomerlist() { transaction tx = session.begintransaction(); list custs = session.createquery("from customer status='new'").list(); return custs; } public void persistcreditcard(creditcard ccard, customer cust) { int x = ccard.getcustomerid(); session session = sessionfactory.getcurrentsession(); transaction tx = session.begintransaction(); session.persist(ccard); session.update(cust); system.out.println("customer saved sucessfully" + ccard); } public void updatecustomer(customer cust) { session session = sessionfactory.getcurrentsession(); transaction tx = session.begintransaction(); session.update(cust); } }
spring.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.bcj.creditcardprocess" /> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="ccardservice" class="com.bcj.creditcardprocess.service.creditcardservice" /> <bean id="ccarddao" class="com.bcj.creditcardprocess.dao.creditcarddao" /> <tx:annotation-driven transaction-manager="transactionmanager" /> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory" /> </bean> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="driverclassname" value="com.mysql.jdbc.driver" /> <property name="url" value="jdbc:mysql://localhost:3306/citibank" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!--hibernate 4 sessionfactory bean definition --> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="annotatedclasses"> <list> <value>com.bcj.creditcardprocess.model.creditcard</value> <value>com.bcj.creditcardprocess.model.customer</value> </list> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect </prop> <prop key="hibernate.hbm2ddl.auto">update </prop> <prop key="hibernate.show_sql">true</prop> <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> </props> </property> </bean> </beans>
pom.xml
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.bcj</groupid> <artifactid>creditcardprocess</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>creditcardprocess</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>4.3.7.release</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-core</artifactid> <version>4.3.3.final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>4.3.7.release</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java--> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>6.0.6</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>4.3.7.release</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp --> <dependency> <groupid>commons-dbcp</groupid> <artifactid>commons-dbcp</artifactid> <version>1.4</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>4.3.7.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>4.3.7.release</version> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>servlet-api</artifactid> <version>2.5</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-orm</artifactid> <version>4.3.7.release</version> </dependency> </dependencies> </project>
maybe try change
private session session = sessionfactory.getcurrentsession();
to function
public session getsession() { return sessionfactory.getcurrentsession(); }
i thinking during class initialization, class trying initialize private session session. , sessionfactory not ready yet.
Comments
Post a Comment