spring - JNDI 和 Spring 配置 - jee :lookup

标签 spring tomcat jndi

我想使用 JNDI 将我的类发布为资源之一。在 Tomcat 的 context.xml 中我有

<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.springframework.beans.factory.support.DefaultListableBeanFactory@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.springframework.beans.factory.support.DefaultListableBeanFactory@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

最佳答案

我很确定您需要使用驱动程序而不是方言(这是我对 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 而不是你正在做的事情。

关于spring - JNDI 和 Spring 配置 - jee :lookup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12186274/

相关文章:

java - 自定义 Vaadin 导航器

java - 测试类中的接口(interface) getBean

java - Tomcat 自动转发到某个页面。找不到设置

java - JNDI 依赖项是什么?

tomcat - hsqldb - 应用程序之间没有同步

java - 如何使用 Spring Boot 从文件中获取数据列表?

java - 没有回复监听器的 Spring Integration JMS 网关

java - 在 Spring 和 tomcat 中从 java 访问文件系统

java - 尝试在 jQuery + Struts2 中执行 ajax getJson 或 post 操作时,Tomcat7 出现 500 内部服务器错误

java - 是否可以从 javax.naming.InitialContext 获取 "everything"?