2016-12-14 5 views
0

Mongoクラスに対して@Injectアノテーションを使用する目的を知りたい。なぜ私は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(); 
      } 
     }; 
     } 
    } 

答えて

0

prrlyあなたは注入をサポートするフレームワークを使用していて、xmlファイルにmongo接続プロパティを定義しています。このinjectはその定義とプロパティを探し、それらのプロパティとの以前に作成された接続でこの変数を設定します! mongoとは何の関係もありません!

関連する問題