3

我在 grails、groovy 上构建 Web 应用程序,它与 3 DB 的 MySql、Oracle(暂时指向 H2)、Mongo 进行交互。为了创建数据源,我配置了以下属性

environments:

development:

    dataSources:

        dbCreate: create-drop

        dataSource:
            # local dev mysql db
             url: "jdbc:mysql://localhost:3307/my_db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"
             username: root
             password: root

        oraclerpt:
           url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

    # mongodb settings
    grails:
        mongodb:
           url: "mongodb://localhost/my_db"
            # port: 27017
           #  databaseName: "my_db"
test:
    dataSources:
        oraclerpt:
            url: "jdbc:oracle:thin:@remoteIP:1521/my_db"
            username: XXX
            password: XXXX
            formatSql: true
            use_sql_comments: true
        dataSource:
            url: "jdbc:dataSource://RemoteAlias:3306/de_test"
            username: XXX
            password: XXXX
    # mongodb settings
    grails:
        mongodb:
            url: "mongodb://localhost/de_mfs_test"
# put settings here for deployment environments
production:

应用程序.yml

数据源:

dataSource:

    dbCreate: none

    pooled: true

    jmxExport: true
    driverClassName: "com.mysql.cj.jdbc.Driver"
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    properties:
          jmxEnabled: true
          initialSize: 5
          maxActive: 50
          minIdle: 5
          maxIdle: 25
          maxWait: 10000
          maxAge: 600000
          timeBetweenEvictionRunsMillis: 5000
          minEvictableIdleTimeMillis: 60000
          validationQuery: SELECT 1 FROM DUAL
          validationQueryTimeout: 3
          validationInterval: 15000
          testOnBorrow: true
          testWhileIdle: true
          testOnReturn: false
          jdbcInterceptors: ConnectionState
          defaultTransactionIsolation: 2 #

当我建立这个得到以下错误

2016-12-26 12:15:58,276 ERROR localhost-startStop-1  org.springframework.boot.context.embedded.tomcat.TomcatStarter - Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'filterInvocationInterceptorFilterDeregistrationBean': Cannot resolve reference to bean 'filterInvocationInterceptor' while setting bean property 'filter'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filterInvocationInterceptor': Cannot resolve reference to bean 'authenticationManager' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationManager': Cannot resolve reference to bean 'daoAuthenticationProvider' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoAuthenticationProvider': Cannot resolve reference to bean 'userDetailsService' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailsService': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'transactionManager_dbCreate' while setting constructor argument with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager_dbCreate': Cannot resolve reference to bean 'dataSource_dbCreate' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource_dbCreate' is defined
2016-12-26 12:15:58,479 ERROR main  org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean named 'dataSource_dbCreate' that could not be found.


Action:

Consider defining a bean named 'dataSource_dbCreate' in your configuration.


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':bootRun'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_111\bin\java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Error |
Failed to start server (Use --stacktrace to see the full trace)

Process finished with exit code 1
4

1 回答 1

2

dbCreate在错误的块中使用,这就是它尝试创建 dataSource_dbCreate 的原因:

environments:

    development:

        dataSources:

            dbCreate: create-drop

            dataSource:

将其移动到指定的数据源下:

environments:

    development:

        dataSources:

            dataSource:

                dbCreate: create-drop

还可以查看带有示例的官方文档

于 2016-12-26T15:26:54.893 回答