Mongock和@Value注解似乎不起作用。

huangapple go评论62阅读模式
英文:

Mongock and @Value annotation does not appear to work

问题

以下是翻译好的内容:

我正在尝试使用@Value注解在通过SpringBootMongockBuilder创建SpringBootMongock对象时获取URI和数据库名称。在执行mvn install时,它尝试加载应用程序上下文并失败,因为我的Spring Contract测试无法连接到application.yml文件中的数据库。这在构建我的应用程序时我是不想要的。

我已经通过在方法签名中注入Environment对象来解决了这个问题,但我不明白为什么@Value不起作用。我已经使用@Configuration对类进行了注解,这正常工作。

更新:我仍然需要从yaml文件中获取URI以使用@Bean注解创建我的MongoClient

@Configuration
public class MongockConfiguration {
    
    @Value(${spring.data.mongodb.uri})
    private String uri;
    
    @Bean
    public MongoClient mongoClient(){
        return MongoClients.create(uri);
    }
    
    @Bean
    public SpringBootMongock mongock(Application context, Environment environment) throws Exception {
        return new SpringBootMongockBuilder(mongoClient(), dbname, ChangeLogOne.class.getPackage().getName())
            .setEnabled(migrate)
            .setApplicationContext(applicationContext)
            .build();
    }
}

上面的一些值我从Environment对象中提取,因为@Value对我不起作用... 这里不需要提供精确的值。

异常信息如下:

java.lang.IllegalStateException: 无法加载应用程序上下文
Caused by: org.springframework.beans.factory.BeanCreationException: 在类路径资源 [blah/dht/mcs/registrationservice/config/MongockConfiguration.class] 中定义的名为 'mongock' 的 bean 创建失败:通过工厂方法实例化 bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException: 无法实例化 [com.github.cloudyrock.mongock.SpringBootMongock]:工厂方法 'mongock' 抛出异常;嵌套异常是 com.mongodb.MongoSecurityException: 认证时出现异常,MongoCredential{mechanism=SCRAM-SHA-1, userName='testUser', source='admin', password=<hidden>, mechanismProperties={}}
Caused by: org.springframework.beans.BeanInstantiationException: 无法实例化 [com.github.cloudyrock.mongock.SpringBootMongock]:工厂方法 'mongock' 抛出异常;嵌套异常是 com.mongodb.MongoSecurityException: 认证时出现异常,MongoCredential{mechanism=SCRAM-SHA-1, userName='testUser', source='admin', password=<hidden>, mechanismProperties={}}
Caused by: com.mongodb.MongoSecurityException: 认证时出现异常,MongoCredential{mechanism=SCRAM-SHA-1, userName='testUser', source='admin', password=<hidden>, mechanismProperties={}}
Caused by: com.mongodb.MongoCommandException: 在服务器 localhost:27017 上命令执行失败,错误代码为 18 (AuthenticationFailed):'Authentication failed.'。完整响应如下:{ "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }

作为记录,使用硬编码值与使用@Value注解一切正常。

英文:

I am trying to use the @Value annotation to grab the URI and database name while creating a SpringBootMongock object via SpringBootMongockBuilder and during mvn install it attempts to load the application context and fails because my Spring Contract tests cannot connect to the database in my application.yml file. Which I do not want while building my app anyways.
I have gotten around this by injecting the Environment object in my method signature but I don't understand why the @Value is not working. I have annotated the class with @Configuration which works fine.

Update: I still need to grab the URI from the yaml file to create my MongoClient using the @Bean annotation..

    @Configuration
    public class MongockConfiguration {
    
    @Value(${spring.data.mongodb.uri})
    private String uri;
    
   @Bean
   public MongoClient mongoClient(){
      return MongoClients.create(uri);
   }

   @Bean
   public SpringBootMongock mongock(Application context, Environment environment)   throws Exception {
 
   return new SpringBootMongockBuilder(mongoClient(), dbname, ChangeLogOne.class.getPackage().getName()).setEnabled(migrate).setApplicationContext(applicationContext).build();
}

some of the above values I pull from the Environment object as the @Value wasn't working for me .. no need to provide exact values here

exception is

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;mongock&#39; defined in class path resource [blah/dht/mcs/registrationservice/config/MongockConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.github.cloudyrock.mongock.SpringBootMongock]: Factory method &#39;mongock&#39; threw exception; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=&#39;testUser&#39;, source=&#39;admin&#39;, password=&lt;hidden&gt;, mechanismProperties={}}
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.github.cloudyrock.mongock.SpringBootMongock]: Factory method &#39;mongock&#39; threw exception; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=&#39;testUser&#39;, source=&#39;admin&#39;, password=&lt;hidden&gt;, mechanismProperties={}}
Caused by: com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=&#39;testUser&#39;, source=&#39;admin&#39;, password=&lt;hidden&gt;, mechanismProperties={}}
Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): &#39;Authentication failed.&#39; on server localhost:27017. The full response is { &quot;ok&quot; : 0.0, &quot;errmsg&quot; : &quot;Authentication failed.&quot;, &quot;code&quot; : 18, &quot;codeName&quot; : &quot;AuthenticationFailed&quot; }

and for the record with the values hardcoded vs using the @Value annotation everything works as expected.

答案1

得分: 0

问题与Mongock无关,但我很清楚地看到这是因为在出错时未注入MongoClient。

正如您所提到的,当您提供硬编码的uri时,我倾向于说Spring找不到spring.data.mongodb.uri,但如果我没有错,它会失败并说找不到该属性,而不是您正在收到的错误,因为您没有提供默认值。

无论如何,大致是在那附近,由于某种原因,每当您运行此代码并且失败时,MongoClient未被添加到上下文中,原因未明。

希望对您有所帮助。

英文:

The issue is not related to Mongock, but I see pretty clear that is due to MongoClient not being injected whenever you get the error.

As you mentioned it works when you provide the uri hardcoded, I was tempted to say that Spring doesn't find the spring.data.mongodb.uri, but if I am not wrong, it would fail saying that property is not found, rather than the error you are getting, as you are not providing a default value.

Anyway, it's something around there, for some reason, whenever you are running this and fails, the MongoClient is not added to the context for some reason.

I hope you find it useful.

huangapple
  • 本文由 发表于 2020年4月8日 08:27:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61091513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定