开发者

Configuring Datasource in Spring 3.0

开发者 https://www.devze.com 2023-03-31 08:39 出处:网络
Hello guys I have configured a connection pool and JNDI resource in glassfish 2.1. I can get the Datasource via lookup method in my projects and everything works good. However I decided to try Spring

Hello guys I have configured a connection pool and JNDI resource in glassfish 2.1. I can get the Datasource via lookup method in my projects and everything works good. However I decided to try Spring framework and to use my existing connection pool.

In the Spring context file I have the following:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/name" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>
<bean id="dao" class="com.mycompany.mavenproject3.Dao">
    <property name="simpleJdbcTemplate" ref="jdbcTemplate"/>
</bean> 

When I deploy the project I get:

java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required]

Is there anything else I have to configure i开发者_如何学运维n that file or in any other file in order to get the Datasource?


Presumably, com.mycompany.mavenproject3.Dao extends JdbcDaoSupport, but you're setting a property named simpleJdbcTemplate on it, leading me to believe that you've defined your own property to hold the template since that doesn't exist on Spring's implementation. It's therefore complaining at you because you're required to set either the dataSource property or the jdbcTemplate property of the JdbcDaoSupport object before using it, exactly like it's telling you. Change <property name="simpleJdbcTemplate"... to <property name="jdbcTemplate"....

If your DAO doesn't extend JdbcDaoSupport, then find what does and remove it or set its properties appropriately.


You can also call your datasource directly in your dao bean, don't need to do an another bean for jdbcTemplate. So your context file become something like this:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/name" />
<bean id="dao" class="com.mycompany.mavenproject3.Dao">
        <property name="dataSource" ref="dataSource"/>
</bean>

After you just have to extends JdbcDaoSupport spring class (in which contain the getter and setter of datasource) on your Dao class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号