简体   繁体   English

为什么我不能在Spring中使用JavaConfig创建我的数据源?

[英]Why can't I create my Datasource with JavaConfig in Spring?

Can someone please tell me what I am missing I am trying to use JavaConfig in a Spring MVC project to setup the following database but I cant set the driverClass, user, password etc? 有人可以告诉我我错过了什么我试图在Spring MVC项目中使用JavaConfig来设置以下数据库但我不能设置driverClass,用户,密码等?

Can someone please tell me why 有人可以告诉我为什么

@Bean
public DataSource dataSource() {

    DataSource ds = new DriverManagerDataSource();
    try {
        ds.setDriverClass("com.mysql.jdbc.Driver");
        ds.setUser("jboss");
        ds.setPassword("xoJ4u0Hs");
        ds.setJdbcUrl("jdbc:mysql://6dhdbm01/jboss1");
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    return ds;
}

I FIXED IT: I HAD TO CHANGE THE CODE TO THIS: 我把它固定了:我不得不改变这个代码:

@Bean
    public DataSource dataSource() {

         // com.mchange.v2.c3p0.ComboPooledDataSource ds = new com.mchange.v2.c3p0.ComboPooledDataSource();

        BasicDataSource ds = new BasicDataSource();


        try {

            /*

            This was old code for using C3P0 Database pooling
            ds.setDriverClass("com.mysql.jdbc.Driver");
            ds.setUser("jboss");
            ds.setPassword("xoJ4u0Hs");
            ds.setJdbcUrl("jdbc:mysql://6dhdbm01/jboss1");
            */

            ds.setDriverClassName("com.mysql.jdbc.Driver");
            ds.setUsername("jboss");
            ds.setPassword("xoJ4u0Hs");
            ds.setUrl("jdbc:mysql://6dhdbm01/jboss1");

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return ds;
    }

But I am getting this error.. Am I missing something in maven pom.xml 但是我收到了这个错误..我错过了maven pom.xml中的内容

java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool

My guesses: 我的猜测:

  • You're importing javax.activation.DataSource rather than javax.sql.DataSource 您正在导入javax.activation.DataSource而不是javax.sql.DataSource
  • com.mysql.jdbc.Driver isn't on classpath com.mysql.jdbc.Driver不在类路径上
  • URL / Credentials are wrong URL /凭据错误

But really don't have much to go off of, first guess is a compile time issue, second two are deploy time issues. 但是真的没什么可走的,首先猜测是编译时问题,第二个是部署时间问题。

EDIT: Can't find what symbol? 编辑:找不到什么符号? My guess is DriverManagerDataSource as it is not part of core, it's part of spring-jdbc. 我的猜测是DriverManagerDataSource,因为它不是核心的一部分,它是spring-jdbc的一部分。 Also, wouldn't really recommend that DataSource implementation as it isn't a connection pool, it creates a new connection each time, look into commons-dbcp 另外,不会真的推荐DataSource实现,因为它不是连接池,每次都会创建一个新连接,查看commons-dbcp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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