Java tutorial
/* * Copyright (c) 2013 ITOCHU Techno-Solutions Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.co.ctc_g.jse.data.mongo.showcase.config; import java.net.UnknownHostException; import jp.co.ctc_g.jfw.core.internal.InternalException; import jp.co.ctc_g.jfw.core.util.Maps; import jp.co.ctc_g.jfw.core.util.Strings; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.auditing.IsNewAwareAuditingHandler; import org.springframework.data.authentication.UserCredentials; import org.springframework.data.mapping.context.MappingContextIsNewStrategyFactory; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.mapping.event.AuditingEventListener; import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener; import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; import com.mongodb.ServerAddress; /** * <p> * ????MongoDB?Connection?????? * </p> * <p> * ????????3????? * <ol> * <li>????????</li> * <li>JavaConfig???</li> * <ul> * <div> * <pre class="brush:java"> * @Configuration * @Import(MongoContextConfig.class) * public class AppContextConfig { * // App? * } * </pre> * </div> * </ul> * <li>XML???</li> * <ul> * <div> * <pre class="brush:java"> * <beans> * <bean class="jp.co.ctc_g.jse.core.data.mongo.config.MongoContextConfig" /> * </beans> * </pre> * </div> * </ul> * </ol> * </p> * <p> * ?????? * ????Java??? * ??????? * ???????? * ????????? * ?????? * ???Spring?{@link org.springframework.context.support.PropertySourcesPlaceholderConfigurer}??? * ?XML?JavaConfig????? * <pre class="brush:java"> * <context:property-placeholder location="classpath:Mongo.properties" * ignore-unresolvable="true" * ignore-resource-not-found="true" /> * </pre> * ???? * <pre class="brush:java"> * ???1????1????? * ???Jdbc.properties??????PropertySource??????????? * @PropertySource("classpath:/Mongo.properties") * public class PropertyPlacehodler { * public PropertyPlacehodler() {} * @Bean * public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() { * PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); * configurer.setIgnoreResourceNotFound(true); * configurer.setIgnoreUnresolvablePlaceholders(true); * return configurer; * } * } * </pre> * </p> * <p> * ????????? * </p> * <table> * <thead> * <tr> * <th></th> * <th>?</th> * <th></th> * </tr> * </thead> * <tbody> * <tr> * <td>mongo.host</td> * <td>MongoDB???????????</td> * <td>127.0.0.1</td> * </tr> * <tr> * <td>mongo.port</td> * <td>MongoDB?????</td> * <td>27017</td> * </tr> * <tr> * <td>mongo.username</td> * <td>MongoDB??????</td> * <td>??</td> * </tr> * <tr> * <td>mongo.password</td> * <td>MongoDB????</td> * <td>??</td> * </tr> * <tr> * <td>mongo.connectionsPerHost</td> * <td>MongoDB?????</td> * <td>10</td> * </tr> * <tr> * <td>mongo.threadsAllowedToBlockForConnectionMultiplier</td> * <td>MongoDB???????</td> * <td>4</td> * </tr> * <tr> * <td>mongo.connectTimeout</td> * <td>MongoDB??????</td> * <td>1000</td> * </tr> * <tr> * <td>mongo.maxWaitTime</td> * <td>MongoDB???????</td> * <td>1500</td> * </tr> * <tr> * <td>mongo.autoConnectRetry</td> * <td>MongoDB??????</td> * <td>true</td> * </tr> * <tr> * <td>mongo.socketKeepAlive</td> * <td>MongoDB??????</td> * <td>true</td> * </tr> * <tr> * <td>mongo.databaseName</td> * <td>MongoDB??????</td> * <td>??</td> * </tr> * </tbody> * </table> * @author ITOCHU Techno-Solutions Corporation. */ @Configuration public class MongoContextConfig { /** * ?MongoDB??? * * 127.0.0.1 */ @Value("${mongo.host:127.0.0.1}") private String host; /** * ?MongoDB??? * * 27017 */ @Value("${mongo.port:27017}") private int port; /** * ?MongoDB??? * * ?? */ @Value("${mongo.username:}") private String username; /** * ?MongoDB? * * ?? */ @Value("${mongo.password:}") private String password; /** * ?MongoDB?? * * 10 */ @Value("${mongo.connectionsPerHost:10}") private int connectionsPerHost; /** * ?MongoDB???? * * 4 */ @Value("${mongo.threadsAllowedToBlockForConnectionMultiplier:4}") private int threadsAllowedToBlockForConnectionMultiplier; /** * ?MongoDB??? * * 1000 */ @Value("${mongo.connectTimeout:1000}") private int connectTimeout; /** * ?MongoDB???? * * 1500 */ @Value("${mongo.maxWaitTime:1500}") private int maxWaitTime; /** * ?MongoDB??? * * true */ @Value("${mongo.autoConnectRetry:true}") private boolean autoConnectRetry; /** * ?MongoDB??? * * true */ @Value("${mongo.socketKeepAlive:true}") private boolean socketKeepAlive; /** * ?MongoDB??? * * true */ @Value("${mongo.databaseName:}") private String databaseName; /** * ?? */ public MongoContextConfig() { } // -------------------- Mongo? /** * MongoDB??????? * @return {@link MongoClient} ? */ @Bean public MongoClient mongo() { try { return new MongoClient(new ServerAddress(host, port), options()); } catch (UnknownHostException e) { throw new InternalException(MongoContextConfig.class, "E-MONGO-CONFIG#0001", Maps.hash("host", host).map("port", Integer.toString(port)), e); } } /** * MongoDB???MongoClientOptions????? * @return {@link MongoClientOptions} ? */ @Bean public MongoClientOptions options() { return config().build(); } /** * MongoDB? MongoDbFactory ????? * @return {@link SimpleMongoDbFactory} ? */ @Bean public MongoDbFactory mongoDbFactory() { if (Strings.isEmpty(databaseName)) throw new InternalException(MongoContextConfig.class, "E-MONGO-CONFIG#0002"); if (!Strings.isEmpty(username) && !Strings.isEmpty(password)) { return new SimpleMongoDbFactory(mongo(), databaseName, auth()); } else { return new SimpleMongoDbFactory(mongo(), databaseName); } } /** * ?????? * @return {@link UserCredentials} ? */ @Bean public UserCredentials auth() { return new UserCredentials(username, password); } /** * MongoDB???????MongoOperations ????? * @return {@link MongoTemplate} ? */ @Bean public MongoOperations mongoTemplate() { return new MongoTemplate(mongoDbFactory(), converter()); } /** * @return {@link MongoMappingContext} */ @Bean public MongoMappingContext mappingContext() { return new MongoMappingContext(); } /** * @return {@link AuditingEventListener} */ @SuppressWarnings("unchecked") @Bean public AuditingEventListener auditingEventListener() { return new AuditingEventListener(auditingHandler()); } /** * @return {@link IsNewAwareAuditingHandler} */ @SuppressWarnings("rawtypes") @Bean public IsNewAwareAuditingHandler auditingHandler() { return new IsNewAwareAuditingHandler(support()); } /** * @return {@link MappingContextIsNewStrategyFactory} */ public MappingContextIsNewStrategyFactory support() { return new MappingContextIsNewStrategyFactory(mappingContext()); } /** * ???MongoDB?????? * @return {@link MongoOperationLoggingEventListener} */ @Bean public LoggingEventListener logging() { return new LoggingEventListener(); } /** * MongoDB?????????????? * @return {@link MongoClientOptions.Builder} */ protected MongoClientOptions.Builder config() { return MongoClientOptions.builder().connectionsPerHost(connectionsPerHost) .threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier) .connectTimeout(connectTimeout).maxWaitTime(maxWaitTime).autoConnectRetry(autoConnectRetry) .socketKeepAlive(socketKeepAlive); } /** * {@link MongoConverter}???????????? * ????????null??? * @return {@link MongoConverter} ? */ protected MongoConverter converter() { return null; } }