Activiti Process Engine bean is not creating - spring

My spring-context.xml file
<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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spenter code herering-tx-3.0.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/activiti_ex_db" />
<property name="username" value="postgres" />
<property name="password" value="user" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="create-drop" />
<property name="jobExecutorActivate" value="false" />
</bean>
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<!-- <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" /> -->
## Java Class
This is my main program
import org.activiti.spring.ProcessEngineFactoryBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ActivitEx {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = null;
try {
applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
ProcessEngineFactoryBean processEngin =(ProcessEngineFactoryBean) applicationContext.getBean("processEngine");
System.out.println(processEngin);
} catch (Exception e) {
e.printStackTrace();
applicationContext = null;
}
}
}
The following error I am getting while running the above class.
Could you please help..
Error
May 17, 2016 7:20:35 PM org.activiti.engine.impl.interceptor.CommandContext close
SEVERE: Error while closing command context
java.lang.NullPointerException
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:42)
at org.activiti.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:42)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)
at org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:40)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33)
at org.activiti.engine.impl.ProcessEngineImpl.<init>(ProcessEngineImpl.java:77)
at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.buildProcessEngine(ProcessEngineConfigurationImpl.java:237)
at org.activiti.spring.SpringProcessEngineConfiguration.buildProcessEngine(SpringProcessEngineConfiguration.java:60)
at org.activiti.spring.ProcessEngineFactoryBean.getObject(ProcessEngineFactoryBean.java:56)
at org.activiti.spring.ProcessEngineFactoryBean.getObject(ProcessEngineFactoryBean.java:32)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:144)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:252)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
at Clint.main(Clint.java:9)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'processEngine': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:151)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:252)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
at Clint.main(Clint.java:9)
Caused by: java.lang.NullPointerException
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:42)
at org.activiti.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:42)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)
at org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:40)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33)
at org.activiti.engine.impl.ProcessEngineImpl.<init>(ProcessEngineImpl.java:77)
at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.buildProcessEngine(ProcessEngineConfigurationImpl.java:237)
at org.activiti.spring.SpringProcessEngineConfiguration.buildProcessEngine(SpringProcessEngineConfiguration.java:60)
at org.activiti.spring.ProcessEngineFactoryBean.getObject(ProcessEngineFactoryBean.java:56)
at org.activiti.spring.ProcessEngineFactoryBean.getObject(ProcessEngineFactoryBean.java:32)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:144)
... 6 more

In the processEngineConfiguration bean definition, can you try by changing the below property value
<property name="databaseSchemaUpdate" value="create-drop" />
to
<property name="databaseSchemaUpdate" value="true" />

Related

Spring Context initialization failed while bootstrapping with Websphere 7.x server

I am working on a migration task where we are migrating the project from Websphere 6.1 to Websphere 7.0.0.39. Also upgraded JDK version from 1.5 to 1.6.
Actually one of the web project, uses spring and hibernate combination. The version of spring is 2.5.3. The spring context is initialized while bootstrapping i.e. # server startup.
So i am getting org.springframework.web.context.ContextLoader initWebApplicationContext Context initialization failed error while server startup.
I also did a google search on the below issue and found the jira SPR-4788 which says spring2.5 && ibm jdk6 will produce a NPE as below.
Stack Trace from the server startup log :
org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext#27822782]: org.springframework.beans.factory.support.DefaultListableBeanFactory#39ed39ed
[5/9/16 16:29:03:417 IST] 0000000c DefaultListab I org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#39ed39ed: defining beans [applicationContextHolder,dataSource,entityManagerFactory,transactionManager,jndiEPTemplate,sequenceProxy,transferActionItemDao,transferTemplateActionItemDao,achActionItemDao,achTemplateActionItemDao,wireActionItemDao,wireTemplateActionItemDao,posPayActionItemDao,cdBalancesDao,bankIRReportsDao,favoriteDao,stateDao,secureMsgActionItemDao,specialReportItemDao,alertsActionItemDao,shortcutMenuDao,userActionItemDao,achAuthActionItemDao,corporateWireBannerDao,wireBannerDao,corporateTransferBannerDao,transferBannerDao,corporateAchBannerDao,achBannerDao,corporatePosPayBannerDao,posPayBannerDao,issuesBannerDao,stopPayBannerDao,uploadBannerDao,corporateIssuesBannerDao,corporateStopPayBannerDao,corporateUploadBannerDao,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.config.internalTransactionAdvisor,CDBalancesDao,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy
[5/9/16 16:29:03:417 IST] 0000000c ContextLoader E org.springframework.web.context.ContextLoader initWebApplicationContext Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor': Initialization of bean failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:478)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(AccessController.java:224)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:217)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:579)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:366)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:1721)
at com.ibm.ws.webcontainer.webapp.WebApp.commonInitializationFinish(WebApp.java:388)
at com.ibm.ws.webcontainer.webapp.WebAppImpl.initialize(WebAppImpl.java:299)
at com.ibm.ws.webcontainer.webapp.WebGroupImpl.addWebApplication(WebGroupImpl.java:100)
at com.ibm.ws.webcontainer.VirtualHostImpl.addWebApplication(VirtualHostImpl.java:166)
at com.ibm.ws.webcontainer.WSWebContainer.addWebApp(WSWebContainer.java:732)
at com.ibm.ws.webcontainer.WSWebContainer.addWebApplication(WSWebContainer.java:617)
at com.ibm.ws.webcontainer.component.WebContainerImpl.install(WebContainerImpl.java:376)
at com.ibm.ws.webcontainer.component.WebContainerImpl.start(WebContainerImpl.java:668)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:1146)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.fireDeployedObjectStart(DeployedApplicationImpl.java:1320)
at com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:611)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:945)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:759)
at com.ibm.ws.runtime.component.ApplicationMgrImpl$3.run(ApplicationMgrImpl.java:2082)
at com.ibm.ws.security.auth.ContextManagerImpl.runAs(ContextManagerImpl.java:5398)
at com.ibm.ws.security.auth.ContextManagerImpl.runAsSystem(ContextManagerImpl.java:5486)
at com.ibm.ws.security.core.SecurityContext.runAsSystem(SecurityContext.java:255)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:2087)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:434)
at com.ibm.ws.runtime.component.CompositionUnitImpl.start(CompositionUnitImpl.java:123)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:377)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.access$300(CompositionUnitMgrImpl.java:123)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl$CUInitializer.run(CompositionUnitMgrImpl.java:944)
at com.ibm.wsspi.runtime.component.WsComponentImpl$_AsynchInitializer.run(WsComponentImpl.java:496)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1662)
Caused by: java.lang.NullPointerException
at org.springframework.core.GenericTypeResolver.getTypeVariableMap(GenericTypeResolver.java:144)
at org.springframework.core.GenericTypeResolver.resolveReturnType(GenericTypeResolver.java:93)
at org.springframework.beans.GenericTypeAwarePropertyDescriptor.getPropertyType(GenericTypeAwarePropertyDescriptor.java:58)
at java.beans.PropertyDescriptor.setWriteMethod(PropertyDescriptor.java:111)
at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:77)
at org.springframework.beans.GenericTypeAwarePropertyDescriptor.<init>(GenericTypeAwarePropertyDescriptor.java:47)
at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:250)
at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:144)
at org.springframework.beans.BeanWrapperImpl.getCachedIntrospectionResults(BeanWrapperImpl.java:252)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:380)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1248)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1008)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:470)
... 39 more
[5/9/16 16:29:03:433 IST] 0000000c webapp E com.ibm.ws.webcontainer.webapp.WebApp notifyServletContextCreated SRVE0283E: Exception caught while initializing context: {0}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor': Initialization of bean failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:478)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(AccessController.java:224)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:217)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:579)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:366)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:1721)
at com.ibm.ws.webcontainer.webapp.WebApp.commonInitializationFinish(WebApp.java:388)
at com.ibm.ws.webcontainer.webapp.WebAppImpl.initialize(WebAppImpl.java:299)
at com.ibm.ws.webcontainer.webapp.WebGroupImpl.addWebApplication(WebGroupImpl.java:100)
at com.ibm.ws.webcontainer.VirtualHostImpl.addWebApplication(VirtualHostImpl.java:166)
at com.ibm.ws.webcontainer.WSWebContainer.addWebApp(WSWebContainer.java:732)
at com.ibm.ws.webcontainer.WSWebContainer.addWebApplication(WSWebContainer.java:617)
at com.ibm.ws.webcontainer.component.WebContainerImpl.install(WebContainerImpl.java:376)
at com.ibm.ws.webcontainer.component.WebContainerImpl.start(WebContainerImpl.java:668)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:1146)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.fireDeployedObjectStart(DeployedApplicationImpl.java:1320)
at com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:611)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:945)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:759)
at com.ibm.ws.runtime.component.ApplicationMgrImpl$3.run(ApplicationMgrImpl.java:2082)
at com.ibm.ws.security.auth.ContextManagerImpl.runAs(ContextManagerImpl.java:5398)
at com.ibm.ws.security.auth.ContextManagerImpl.runAsSystem(ContextManagerImpl.java:5486)
at com.ibm.ws.security.core.SecurityContext.runAsSystem(SecurityContext.java:255)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:2087)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:434)
at com.ibm.ws.runtime.component.CompositionUnitImpl.start(CompositionUnitImpl.java:123)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:377)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.access$300(CompositionUnitMgrImpl.java:123)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl$CUInitializer.run(CompositionUnitMgrImpl.java:944)
at com.ibm.wsspi.runtime.component.WsComponentImpl$_AsynchInitializer.run(WsComponentImpl.java:496)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1662)
Caused by: java.lang.NullPointerException
at org.springframework.core.GenericTypeResolver.getTypeVariableMap(GenericTypeResolver.java:144)
at org.springframework.core.GenericTypeResolver.resolveReturnType(GenericTypeResolver.java:93)
at org.springframework.beans.GenericTypeAwarePropertyDescriptor.getPropertyType(GenericTypeAwarePropertyDescriptor.java:58)
at java.beans.PropertyDescriptor.setWriteMethod(PropertyDescriptor.java:111)
at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:77)
at org.springframework.beans.GenericTypeAwarePropertyDescriptor.<init>(GenericTypeAwarePropertyDescriptor.java:47)
at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:250)
at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:144)
at org.springframework.beans.BeanWrapperImpl.getCachedIntrospectionResults(BeanWrapperImpl.java:252)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:380)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1248)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1008)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:470)
... 39 more
Spring jars in Class-Path of the web project:
3rdparty/spring-2.5.3.jar
3rdparty/spring-aop.jar
3rdparty/spring-beans.jar
3rdparty/spring-binding.jar
3rdparty/spring-context-support.jar
3rdparty/spring-context.jar
3rdparty/spring-core.jar
3rdparty/spring-dao.jar
3rdparty/spring-faces.jar
3rdparty/spring-jdbc.jar
3rdparty/spring-js.jar
3rdparty/spring-mock.jar
3rdparty/spring-security-acl.jar
3rdparty/spring-security-core-tiger.jar
3rdparty/spring-security-core.jar
3rdparty/spring-security-taglibs.jar
3rdparty/spring-support.jar
3rdparty/spring-test.jar
3rdparty/spring-tx.jar
3rdparty/spring-web.jar
3rdparty/spring-webflow.jar
3rdparty/spring-webmvc.jar
Spring Configuration File
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- http://static.springframework.org/spring/docs/2.5.x/reference/xsd-config.html -->
<!-- see Spring in Action pg 143 -->
<bean id="applicationContextHolder" class="com.s1.core.WebApplicationContextHolder" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.WebSphereDataSourceAdapter">
<property name="targetDataSource">
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/EPDatabase" />
</bean>
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="bankingEjbPersistenceUnit"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="DB2"/>
<property name="showSql" value="false"/>
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="jndiEPTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">com.ibm.websphere.naming.WsnInitialContextFactory</prop>
<prop key="java.naming.provider.url">iiop://localhost:#BOOT_STRAP_PORT#</prop>
</props>
</property>
</bean>
<bean id="sequenceProxy" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean" scope="prototype">
<property name="jndiName" value="ejb/com/s1/arch/sequence/SequenceServiceHome"/>
<property name="businessInterface" value="com.s1.arch.persistence.sequence.SequenceService"/>
<property name="jndiTemplate">
<ref local="jndiEPTemplate"/>
</property>
<property name="cacheHome" value="false"/>
<property name="lookupHomeOnStartup" value="false"/>
<property name="resourceRef" value="false"/>
<property name="homeInterface" value="com.s1.arch.persistence.sequence.SequenceServiceHome"/>
</bean>
<bean id="transferActionItemDao" class="com.s1.core.dashboard.dao.TransferActionItemDao" autowire="byName" />
<bean id="transferTemplateActionItemDao" class="com.s1.core.dashboard.dao.TransferTemplateActionItemDao" autowire="byName" />
<bean id="achActionItemDao" class="com.s1.core.dashboard.dao.AchActionItemDao" autowire="byName" />
<bean id="achTemplateActionItemDao" class="com.s1.core.dashboard.dao.AchTemplateActionItemDao" autowire="byName" />
<bean id="wireActionItemDao" class="com.s1.core.dashboard.dao.WireActionItemDao" autowire="byName" />
<bean id="wireTemplateActionItemDao" class="com.s1.core.dashboard.dao.WireTemplateActionItemDao" autowire="byName" />
<bean id="posPayActionItemDao" class="com.s1.core.dashboard.dao.PosPayActionItemDao" autowire="byName" />
<bean id="cdBalancesDao" class="com.s1.core.dashboard.dao.CDBalancesDao" autowire="byName" />
<bean id="bankIRReportsDao" class="com.s1.core.dashboard.dao.BankIRReportsDao" autowire="byName" />
<bean id="favoriteDao" class="com.s1.core.dashboard.dao.FavoriteDao" autowire="byName" />
<bean id="stateDao" class="com.s1.core.dashboard.dao.StateDao" autowire="byName" />
<bean id="secureMsgActionItemDao" class="com.s1.core.dashboard.dao.SecureMsgActionItemDao" />
<bean id="specialReportItemDao" class="com.s1.core.dashboard.dao.SpecialReportItemDao" autowire="byName" />
<bean id="alertsActionItemDao" class="com.s1.core.dashboard.dao.AlertsActionItemDao" autowire="byName" />
<bean id="shortcutMenuDao" class="com.s1.core.dashboard.dao.ShortcutMenuDao" autowire="byName" />
<bean id="userActionItemDao" class="com.s1.core.dashboard.dao.UserActionItemDao" autowire="byName" />
<bean id="achAuthActionItemDao" class="com.s1.core.dashboard.dao.AchAuthActionItemDao" autowire="byName" />
<bean id="corporateWireBannerDao" class="com.s1.pso.core.banner.dao.PSOCorporateWireBannerDao" autowire="byName" />
<bean id="wireBannerDao" class="com.s1.pso.core.banner.dao.PSOWireBannerDao" autowire="byName" />
<bean id="corporateTransferBannerDao" class="com.s1.pso.core.banner.dao.PSOCorporateTransferBannerDao" autowire="byName" />
<bean id="transferBannerDao" class="com.s1.pso.core.banner.dao.PSOTransferBannerDao" autowire="byName" />
<bean id="corporateAchBannerDao" class="com.s1.pso.core.banner.dao.PSOCorporateAchBannerDao" autowire="byName" />
<bean id="achBannerDao" class="com.s1.pso.core.banner.dao.PSOAchBannerDao" autowire="byName" />
<bean id="corporatePosPayBannerDao" class="com.s1.pso.core.banner.dao.PSOCorporatePosPayBannerDao" autowire="byName" />
<bean id="posPayBannerDao" class="com.s1.pso.core.banner.dao.PSOPosPayBannerDao" autowire="byName" />
<bean id="issuesBannerDao" class="com.s1.pso.core.banner.dao.PSOIssuesBannerDao" autowire="byName" />
<bean id="stopPayBannerDao" class="com.s1.pso.core.banner.dao.PSOStopPayBannerDao" autowire="byName" />
<bean id="uploadBannerDao" class="com.s1.pso.core.banner.dao.PSOUploadBannerDao" autowire="byName" />
<bean id="corporateIssuesBannerDao" class="com.s1.pso.core.banner.dao.PSOCorporateIssuesBannerDao" autowire="byName" />
<bean id="corporateStopPayBannerDao" class="com.s1.pso.core.banner.dao.PSOCorporateStopPayBannerDao" autowire="byName" />
<bean id="corporateUploadBannerDao" class="com.s1.pso.core.banner.dao.PSOCorporateUploadBannerDao" autowire="byName" />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config /> </beans>
Persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="bankingEjbPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<!-- The provider only needs to be set if you use several JPA providers
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<provider>org.unitils.orm.jpa.util.provider.hibernate.UnitilsHibernatePersistenceProvider</provider>
-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.s1.core.dashboard.bean.AchActionItem</class>
<class>com.s1.core.dashboard.bean.AlertsActionItem</class>
<class>com.s1.core.dashboard.bean.BankIRReports</class>
<class>com.s1.core.dashboard.bean.BankTransactionActionItem</class>
<class>com.s1.core.dashboard.bean.CDBalancesEntity</class>
<class>com.s1.core.dashboard.bean.FavoriteItem</class>
<class>com.s1.core.dashboard.bean.PosPayActionItem</class>
<class>com.s1.core.dashboard.bean.SecureMsgActionItem</class>
<class>com.s1.core.dashboard.bean.ShortcutMenuEntity</class>
<class>com.s1.core.dashboard.bean.SpecialReportItem</class>
<class>com.s1.core.dashboard.bean.StateItem</class>
<class>com.s1.core.dashboard.bean.TransferActionItem</class>
<class>com.s1.core.dashboard.bean.WireActionItem</class>
<properties>
<!-- SQL stdout logging -->
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
<property name="hibernate.connection.release_mode" value="after_transaction"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="3000"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
<property name="hibernate.connection.datasource" value="jdbc/EPDatabase" />
<property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver" />
</properties>
</persistence-unit>
<persistence-unit name="parserPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.s1.core.parser.ParsedPDStatement</class>
<class>com.s1.core.parser.ParsedPDStatementDetail</class>
<class>com.s1.core.parser.ParsedPDTransaction</class>
<class>com.s1.core.parser.ParsedPDTransactionMemo</class>
<class>com.s1.core.parser.ParsedCDStatement</class>
<class>com.s1.core.parser.ParsedCDStatementDetail</class>
<class>com.s1.core.parser.ParsedCDTransaction</class>
<class>com.s1.core.parser.ParsedCDTransactionMemo</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.DB2Dialect" />
<!--<property name="hibernate.hbm2ddl.auto" value="update" /> -->
<!-- Enable pooling of connections for efficiency. Can be disabled in development -->
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="300" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
<property name="hibernate.connection.datasource" value="jdbc/EPDatabase" />
<property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver" />
</properties>
</persistence-unit>
</persistence>
Listener configuration in web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

Spring/Hibernate JPA NamedQuery not found

I have a weird issue: entity manager can't create named query, I'm getting java.lang.IllegalArgumentException: Named query not found while methods, like find() and merge(), works. createQuery() also works. I'm using Hibernate as JPA provider and Spring as DI container. Code and configs are follows:
spring-beans.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="org.example.testapp.database" />
<bean id="newsForm" class="org.example.testapp.presentation.form.NewsForm">
<property name="newsMessage" ref="news" />
<property name="newsList">
<list>
<ref bean="news" />
</list>
</property>
</bean>
<bean name="news" class="org.example.testapp.model.News">
</bean>
<bean name="/news" class="org.example.testapp.presentation.action.NewsAction"
scope="prototype">
<property name="dao" ref="dao" />
</bean>
<bean id="dao" class="org.example.testapp.database.dao.JpaHibernateNewsDao"
scope="singleton">
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.example.testapp.model"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="username" value="login" />
<property name="password" value="password" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager" />
news.java
#Entity
#NamedQueries({
#NamedQuery(name="News.select", query="select n from News n order by news_date" ),
#NamedQuery(name="News.delete", query="delete from News where id in :value_list")
})
#Table(name="news")
public class News {
jpadao.java
public class JpaHibernateNewsDao implements Dao {
#PersistenceContext
private EntityManager entityManager;
#SuppressWarnings("unchecked")
#Override
#Transactional
public List<News> getList() throws DaoException {
List<News> news = entityManager.createNamedQuery("News.select").getResultList();
return news;
}
Found a answer: Spring just didn't see #Entity, so there was really no such #NamedQuery. Now I have to figure, why this config didn't work:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.example.testapp.model"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />

inject entitymanager using spring+jpa

How to inject EntityManager object using spring+JPA
I am using spring-context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.javapapers.spring.mvc" />
<context:component-scan base-package="com.cnsi.dao.Impl" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- <jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/spitterPU"
/> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#10.86.24.7:1521:wahipaaD" />
<property name="username" value="mddemo" />
<property name="password" value="mddemo" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.OracleDialect" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/ spring-persistence.xml" />
<property name="persistenceUnitName" value="personPersistenceUnit" />
<property name="dataSource" ref="dataSource" />
<!-- <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> -->
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<!-- <bean class="com.cnsi.dao.Impl.JpaUserDao" id="jpaUserDao">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean> -->
</beans>
And my dao is, inject EntityManager
package com.cnsi.dao.Impl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.cnsi.dao.UserDao;
import com.cnsi.modal.User;
#Repository
public class JpaUserDaoImpl implements UserDao {
#Autowired
EntityManagerFactory entityManagerFactory;
EntityManager em = entityManagerFactory.createEntityManager();
#Override
public void add(User u) {
em.persist(u);
}
#Override
public void edit(User u) {
em.merge(u);
}
#Override
public void delete(int id) {
em.remove(getInterfaceId(id));
}
#Override
public User getInterfaceId(int id) {
return (User) em.find(User.class, id);
}
#Override
public List<User> getAllInterface() {
return em.createQuery("from user").getResultList();
}
}
my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="personPersistenceUnit" transaction-type="RESOURCE_LOCAL" >
<class>com.cnsi.modal.User</class>
</persistence-unit>
</persistence>
So I am getting below error:
java.lang.NoSuchMethodError: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.getJpaVendorAdapter()Lorg/springframework/orm/jpa/JpaVendorAdapter;
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:240)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:232)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFacto
#PersistenceContext
private EntityManager em;
That's how you inject entity manager in your controller.

Spring PropertyPlaceholderConfigurer not passing property values to bean

I have a very simple requirement which has turned complicated and I have spent a day on it without any luck. I have a properties file called jdbc.properties which has the DB connection details. I need to create a datasource connection with the values from the property file. The property value is not being passed right now, leading to DB connectivity error messages. If I hardcode the property values in the bean, it works. My spring config file myBatis.DataSource.config.xml looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="newProperty"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-
init="true">
<property name="locations" value="jdbc.properties.${env}"/>
</bean>
<bean id="newDataSource" class="org.apache.commons.dbcp.BasicDataSource" depends-
on="newProperty" destroy-method="close">
<property name="username" value="${DBUSER}" />
<property name="password" value="${DBPASSWORD}" />
<property name="url" value="${DBURL}" />
<property name="driverClassName" value="${DRIVER}" />
<property name="poolPreparedStatements" value="false" />
<property name="defaultAutoCommit" value="false" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<property name="defaultTransactionIsolation" value="2" />
<property name="timeBetweenEvictionRunsMillis" value="10000" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation"
value="com/automation/config/oneValidation-config.xml" />
<property name="dataSource" ref="newDataSource" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="com.automation.config" />
</bean>
</beans>
The value for ${env} is passed as a system property to the class. The code for the class is as below:
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MybatisTest {
public static void main(String[] args) {
MybatisTest mybatisTest = new MybatisTest();
mybatisTest.testSelectQuery();
}
public void testSelectQuery(){
String resource = "oneValidation-config.xml";
SqlSession session = null;
try {
ApplicationContext context = new ClassPathXmlApplicationContext("myBatis.DataSource.config.xml");
SqlSessionFactory sqlSessionFactory = (SqlSessionFactory)context.getBean("sqlSessionFactory");
session = sqlSessionFactory.openSession(ExecutorType.BATCH);
System.out.println("Test" +
session.selectOne("com.automation.config.PostingMapper.
countByExample",null));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(session != null)
session.close();
}
}
}
The error I am getting is as shown below and is sue to the fact that the ${DBUSER}, ${DBPASSWORD} fields are not being retrieved from the property file jdbc.properties:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC
Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot
create PoolableConnectionFactory (JZ00L: Login failed. Examine the SQLWarnings chained
to this exception for the reason(s).)
I ran accross this as well. In the mybatis documentation here I found why this happens.
From the source: "NOTE sqlSessionFactoryBean and sqlSessionTemplateBean properties were the only option available up to MyBatis-Spring 1.0.2 but given that the MapperScannerConfigurer runs earlier in the startup process that PropertyPlaceholderConfigurer there were frequent errors. For that purpose that properties have been deprecated and the new properties sqlSessionFactoryBeanName and sqlSessionTemplateBeanName are recommended."
Try changing your MapperScannerConfigurer bean from
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="com.automation.config" />
</bean>
to
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.automation.config" />
</bean>
Try this:
<property name="locations" value="classpath:/yourFolderName/jdbc.properties"/>

Spring applicationContext.xml: "The markup in the document following the root element must be well-formed"

I searched through the same questions but unfortunately I couldn't find the solution.
I face with the following exception:
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 32 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 32; columnNumber: 2; The markup in the document following the root element must be well-formed.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:530)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:444)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at Main.main(Main.java:18)
Caused by: org.xml.sax.SAXParseException; lineNumber: 32; columnNumber: 2; The markup in the document following the root element must be well-formed.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1388)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(XMLDocumentScannerImpl.java:1382)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:237)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:300)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
... 14 more
and this is my applicationContext.xml:
<context:component-scan base-package="com.springHibernate" />
<context:annotation-config />
<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/myapp" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<!-- Beans Declaration -->
<bean id="User" class="com.mycompany.myapp.core.model.User"/>
<bean id="Institute" class="com.mycompany.myapp.core.model.Institute"/>
<!-- User Service Declaration -->
<bean id="UserBoImpl" class="com.mycompany.myapp.core.bo.impl.UserBoImpl">
<property name="userDao" ref="UserDaoImpl" />
</bean>
<bean id="InstituteBoImpl" class="com.mycompany.myapp.core.bo.impl.InstituteBoImpl">
<property name="instituteDao" ref="InstituteDaoImpl" />
</bean>
<!-- User DAO Declaration -->
<bean id="UserDaoImpl" class="com.mycompany.myapp.core.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<bean id="InstituteDaoImpl" class="com.mycompany.myapp.core.dao.impl.InstituteDaoImpl">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
</beans>
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>com.mycompany.myapp.core.model.User</value>
<value>com.mycompany.myapp.core.model.Institute</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
I don't know what the reason of exception is ...
I appreciate your comments.
The <beans> tag is closed and then more bean definitions are added. Move the closing beans tag so it nests the bean definitions. Another related thing that can burn you with XML config files is using the format tool within your IDE. Never format the XML config with a tool, it can add additional whitespace to your xml which gets interpreted breaking your configuration.
In a nutshell your doing this:
<beans>
<bean id="1"/>
</beans>
<bean id="2"/>
<bean id="3"/>
When it should be like:
<beans>
<bean id="1"/>
<bean id="2"/>
<bean id="3"/>
</beans>
Full Configuration With Changes
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.springHibernate" />
<context:annotation-config />
<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/mydb" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<!-- Beans Declaration -->
<bean id="User" class="com.mycompany.myapp.core.model.User"/>
<bean id="Institute" class="com.mycompany.myapp.core.model.Institute"/>
<!-- User Service Declaration -->
<bean id="UserBoImpl" class="com.mycompany.myapp.core.bo.impl.UserBoImpl">
<property name="userDao" ref="UserDaoImpl" />
</bean>
<bean id="InstituteBoImpl" class="com.mycompany.myapp.core.bo.impl.InstituteBoImpl">
<property name="instituteDao" ref="InstituteDaoImpl" />
</bean>
<!-- User DAO Declaration -->
<bean id="UserDaoImpl" class="com.mycompany.myapp.core.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<bean id="InstituteDaoImpl" class="com.mycompany.myapp.core.dao.impl.InstituteDaoImpl">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>com.mycompany.myapp.core.model.User</value>
<value>com.mycompany.myapp.core.model.Institute</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
</beans>

Resources