spring jndi 数据源设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16466759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring jndi datasource setup
提问by Aadam
Hi i am trying to use jndi data source. below is the code
嗨,我正在尝试使用 jndi 数据源。下面是代码
context.xml
上下文.xml
<Context antiJARLocking="true" path="/SpringMVCTest">
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
maxActive="20" maxIdle="10" maxWait="10000"
name="jdbc/pluto" password=""
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/spring?zeroDateTimeBehavior=convertToNull"
username="pluto"/>
</Context>
in spring-servlet config bean is:
在 spring-servlet 配置 bean 中是:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jdbc/pluto" value="java:comp/env/jdbc/pluto"/>
</bean>
i am getting this error
我收到这个错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private app.contact.service.ContactService app.contact.controller.ContactController.contactService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private app.contact.dao.ContactDAO app.contact.service.ContactServiceImpl.contactDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory app.contact.dao.ContactDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbc/pluto' of bean class [org.springframework.jndi.JndiObjectFactoryBean]: Bean property 'jdbc/pluto' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbc/pluto' of bean class [org.springframework.jndi.JndiObjectFactoryBean]: Bean property 'jdbc/pluto' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
org.springframework.beans.factory.BeanCreationException:创建名为“contactController”的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 app.contact.service.ContactService app.contact.controller.ContactController.contactService; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“contactServiceImpl”的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private app.contact.dao.ContactDAO app.contact.service.ContactServiceImpl.contactDAO; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“contactDAOImpl”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.hibernate.SessionFactory app.contact.dao.ContactDAOImpl.sessionFactory; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/spring-servlet.xml] 中定义名称为“sessionFactory”的 bean 创建时出错:设置 bean 属性时无法解析对 bean“dataSource”的引用'数据源'; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/spring-servlet.xml] 中定义名称为“dataSource”的 bean 创建时出错:设置属性值时出错;嵌套异常是 org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbc/pluto' of bean class [org.springframework.jndi.JndiObjectFactoryBean]: Bean property 'jdbc/pluto' 不可写或具有无效的 setter 方法。setter 的参数类型是否与 getter 的返回类型匹配?相关原因:org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/spring-servlet.xml] 中定义名称为“dataSource”的 bean 创建时出错:设置属性值时出错;嵌套异常是 org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbc/pluto' of bean class [org.springframework.jndi.JndiObjectFactoryBean]: Bean property 'jdbc/pluto' 不可写或具有无效的 setter 方法。
回答by Sotirios Delimanolis
You have a bean definition as such
你有一个 bean 定义
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jdbc/pluto" value="java:comp/env/jdbc/pluto"/>
</bean>
The property name jdbc/plutois supposed to match a setter, so Spring expects something like setJdbc/Pluto()but that is obviously not correct java syntax.
属性名称jdbc/pluto应该与 setter 匹配,因此 Spring 需要类似的东西,setJdbc/Pluto()但这显然不是正确的 java 语法。
Looking at the JndiObjectFactoryBeanit looks like you want the setJndiName()method from JndiObjectLocatorsuper class.
看着JndiObjectFactoryBean看起来你想要setJndiName()来自JndiObjectLocator超类的方法。
So your bean should look like
所以你的豆子应该看起来像
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/pluto"/>
</bean>

