简体   繁体   中英

Spring Mvc DataSource Bean when using annotations

I would like to know how Dependency Injection is done on the dataSource bean when using annotations in the applicationContext.xml or whats the annotation equivalent for injecting this bean into DAO's, i have the following dataSource bean defined:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
</bean>

Something like this: Create a SimpleJdbcTemplate and inject it with the DataSource . Then annotate that into your DAO:

@Repository
public class FooDao {
    @Resource(name = "jdbcTemplate")
    private SimpleJdbcTemplate jdbcTemplate;
}

You're correct - you don't need anything more than SimpleJdbcTemplate . Hibernate isn't necessary.

Either of these should work.

@Resource private DataSource dataSource;

or

@Autowired private DataSource dataSource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM