2
votes

Hi i am trying to use jndi data source. below is the code

context.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:

<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?

1
A field cannot have the name jdbc/pluto.Sotirios Delimanolis

1 Answers

9
votes

You have a bean definition as such

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jdbc/pluto" value="java:comp/env/jdbc/pluto"/>
</bean>

The property name jdbc/pluto is supposed to match a setter, so Spring expects something like setJdbc/Pluto() but that is obviously not correct java syntax.

Looking at the JndiObjectFactoryBean it looks like you want the setJndiName() method from JndiObjectLocator super class.

So your bean should look like

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/pluto"/>
</bean>