为什么我们在Mongo类之上使用@Inject注释?

时间:2016-12-14 05:08:43

标签: spring mongodb

我想知道在Mongo类上使用@Inject注释的目的。

@Configuration
    @EnableMongoRepositories(basePackages = "de.hypoport.repository")
    public class MongoConfiguration extends AbstractMongoConfiguration {

      @Value("${mongo.uri}")
      String mongoUri;

      **@Inject
      Mongo mongo;**

      @Override
      protected String getDatabaseName() {
        return "mongostress";
      }

      @Override
      @Bean
      public Mongo mongo() throws UnknownHostException {
        return new MongoClient(new MongoClientURI(mongoUri, builder()`enter code here`
            .connectTimeout((int) SECONDS.toMillis(5))
            .socketTimeout((int) SECONDS.toMillis(10))
            .connectionsPerHost(100)
            .threadsAllowedToBlockForConnectionMultiplier(50)
            .readPreference(ReadPreference.primaryPreferred())
            .writeConcern(ACKNOWLEDGED)
        ));
      }

      @Override
      @Bean
      public MongoTemplate mongoTemplate() throws Exception {
        MongoTemplateWithRetry mongoTemplateWithRetry = new MongoTemplateWithRetry(mongoDbFactory(), mappingMongoConverter());
        mongoTemplateWithRetry.setRetryEnabled(true);
        mongoTemplateWithRetry.logMongoWarnings(false);
        return mongoTemplateWithRetry;
      }

      @Override
      @Bean
      public MongoDbFactory mongoDbFactory() {
        return new SimpleMongoDbFactory(mongo, getDatabaseName(), getUserCredentials(), getAuthenticationDatabaseName()) {
          @Override
          public PersistenceExceptionTranslator getExceptionTranslator() {
            return MongoTemplateWithRetry.getExceptionTranslator();
          }
        };
      }
    }

1 个答案:

答案 0 :(得分:0)

您正在使用支持注入的框架,并且您还在xml文件中定义了mongo连接属性!这个inject事物将查找该定义和属性,并使用先前创建的与这些属性的连接设置此变量!并且与mongo无关!