2012-08-29 53 views
0

我想使用JNDI将我的类作为资源之一发布。在Tomcat中的context.xml的我JNDI和Spring配置 - jee:查找

<Resource 
    name="jdbc/dialect" 
    type="pl.meble.taboret.utils.SQLiteDialect" 
    auth="Container"/> 

在我的web.xml

<resource-ref> 
    <description>sqlite dialect</description> 
    <res-ref-name>jdbc/dialect</res-ref-name> 
    <res-type>pl.meble.taboret.utils.SQLiteDialect</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

,并在Spring配置文件中的一个:

INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.s[email protected]18706f6: defining beans 
djfdoigjdoijfdsofjsdoij//the message added at the end of no-parameter constructor of SQLite dialect class 
INFO : org.springframework.beans.factory.config.PropertiesFactoryBean - Loading properties file from class path resource [config.properties] 
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.s[email protected]18706f6: defining beans 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialect': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) 
    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.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
    at java.lang.Thread.run(Thread.java:662) 
Caused by: javax.naming.NamingException: Cannot create resource instance 
    at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:146) 
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:154) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:831) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:154) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:831) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:154) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:831) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:168) 
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158) 
    at javax.naming.InitialContext.lookup(InitialContext.java:392) 
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154) 
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) 
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152) 
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) 
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) 
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) 
    at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201) 
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) 
    ... 22 more 

我添加打印到年底没有参数的构造函数,看起来对象是构造的,所以我不知道它为什么会抱怨Invocation of init method failed

回答

0

我很确定你需要使用驱动程序而不是方言(这是我的MySQL设置,只需更改特定的SQL impl的驱动程序)。

<Resource name="jdbc/myname" auth="Container" type="javax.sql.DataSource" 
maxActive="100" maxIdle="30" maxWait="10000" 
username="user" password="pass" driverClassName="com.mysql.jdbc.Driver" 
url="jdbc:mysql://url/db"/> 

然后

<resource-ref> 
    <description>The JNDI Database resource</description> 
    <res-ref-name>jdbc/myname</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Application</res-auth> 
    <res-sharing-scope>Shareable</res-sharing-scope> 
</resource-ref> 

我想你会想设置您的hibernate.dialect到pl.meble.taboret.utils.SQLiteDialect,而不是你在做什么。

+0

我知道你在提出什么建议,但我想要外化hibernate方言,我已经正确配置了我的数据源。 – Andna

0

尝试以下方法:

<Resource 
    name="jdbc/dialect" 
    type="java.lang.String" 
    value="pl.meble.taboret.utils.SQLiteDialect" 
    auth="Container"/> 

<resource-ref> 
    <description>sqlite dialect</description> 
    <res-ref-name>jdbc/dialect</res-ref-name> 
    <res-type>java.lang.String</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

然后执行你的JNDI查找,铸值作为一个字符串,并将其馈送到的Class.forName()方法。