Extract Mongo auto-configuration from Spring Data
Extract Mongo auto-configuration classes from Spring Data specific auto-configuration, allowing Mongo to be used without Spring Data if require.
This commit is contained in:
@@ -16,125 +16,29 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.data;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
|
||||
|
||||
import com.mongodb.DBPort;
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoClientURI;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Mongo
|
||||
* Repositories.
|
||||
*
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Oliver Gierke
|
||||
* @see EnableMongoRepositories
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Mongo.class, MongoRepository.class })
|
||||
@ConditionalOnMissingBean(MongoRepositoryFactoryBean.class)
|
||||
@Import(MongoRepositoriesAutoConfigureRegistrar.class)
|
||||
public class MongoRepositoriesAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@Import(MongoRepositoriesAutoConfigureRegistrar.class)
|
||||
@EnableConfigurationProperties(MongoProperties.class)
|
||||
protected static class MongoRepositoriesConfiguration {
|
||||
|
||||
@Autowired
|
||||
private MongoProperties config;
|
||||
|
||||
private Mongo mongo;
|
||||
|
||||
@PreDestroy
|
||||
public void close() throws UnknownHostException {
|
||||
if (this.mongo != null) {
|
||||
this.mongo.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(Mongo.class)
|
||||
public Mongo mongo() throws UnknownHostException {
|
||||
this.mongo = this.config.mongo();
|
||||
return this.mongo;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(MongoTemplate.class)
|
||||
public MongoTemplate mongoTemplate(Mongo mongo) throws UnknownHostException {
|
||||
return new MongoTemplate(mongo, this.config.database());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ConfigurationProperties(name = "spring.data.mongodb")
|
||||
public static class MongoProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port = DBPort.PORT;
|
||||
|
||||
private String uri = "mongodb://localhost/test";
|
||||
|
||||
private String database;
|
||||
|
||||
public String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
|
||||
public String database() {
|
||||
return this.database == null ? new MongoClientURI(this.uri).getDatabase()
|
||||
: this.database;
|
||||
}
|
||||
|
||||
public Mongo mongo() throws UnknownHostException {
|
||||
return (this.host != null ? new MongoClient(this.host, this.port)
|
||||
: new MongoClient(new MongoClientURI(this.uri)));
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public String getDatabase() {
|
||||
return this.database;
|
||||
}
|
||||
|
||||
public void setDatabase(String database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public String getUri() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
public void setUri(String uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,11 +18,9 @@ package org.springframework.boot.autoconfigure.data;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
import org.springframework.data.mongodb.repository.config.MongoRepositoryConfigurationExtension;
|
||||
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
|
||||
|
||||
/**
|
||||
@@ -31,7 +29,6 @@ import org.springframework.data.repository.config.RepositoryConfigurationExtensi
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@ConditionalOnMissingBean(MongoRepositoryFactoryBean.class)
|
||||
class MongoRepositoriesAutoConfigureRegistrar extends
|
||||
AbstractRepositoryConfigurationSourceSupport {
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.autoconfigure.data;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.mongodb.Mongo;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's
|
||||
* {@link MongoTemplate}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Oliver Gierke
|
||||
* @see EnableMongoRepositories
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Mongo.class, MongoTemplate.class })
|
||||
public class MongoTemplateAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private MongoProperties config;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MongoTemplate mongoTemplate(Mongo mongo) throws UnknownHostException {
|
||||
return new MongoTemplate(mongo, this.config.getMongoClientDatabase());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.autoconfigure.mongo;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.mongodb.Mongo;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Mongo.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Oliver Gierke
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(Mongo.class)
|
||||
@EnableConfigurationProperties(MongoProperties.class)
|
||||
public class MongoAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private MongoProperties config;
|
||||
|
||||
private Mongo mongo;
|
||||
|
||||
@PreDestroy
|
||||
public void close() throws UnknownHostException {
|
||||
if (this.mongo != null) {
|
||||
this.mongo.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public Mongo mongo() throws UnknownHostException {
|
||||
this.mongo = this.config.createMongoClient();
|
||||
return this.mongo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.autoconfigure.mongo;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import com.mongodb.DBPort;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoClientURI;
|
||||
|
||||
/**
|
||||
* Configuration properties for Mongo.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@ConfigurationProperties(name = "spring.data.mongodb")
|
||||
public class MongoProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port = DBPort.PORT;
|
||||
|
||||
private String uri = "mongodb://localhost/test";
|
||||
|
||||
private String database;
|
||||
|
||||
public String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public String getDatabase() {
|
||||
return this.database;
|
||||
}
|
||||
|
||||
public void setDatabase(String database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public String getUri() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
public void setUri(String uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getMongoClientDatabase() {
|
||||
if (this.database != null) {
|
||||
return this.database;
|
||||
}
|
||||
return new MongoClientURI(this.uri).getDatabase();
|
||||
}
|
||||
|
||||
public MongoClient createMongoClient() throws UnknownHostException {
|
||||
if (this.host != null) {
|
||||
return new MongoClient(this.host, this.port);
|
||||
}
|
||||
return new MongoClient(new MongoClientURI(this.uri));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,12 +11,14 @@ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.MongoRepositoriesAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.MongoTemplateAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.jms.JmsTemplateAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.data.alt.CityMongoDbRepository;
|
||||
import org.springframework.boot.autoconfigure.data.mongo.City;
|
||||
import org.springframework.boot.autoconfigure.data.mongo.CityRepository;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
@@ -47,8 +48,9 @@ public class MongoRepositoriesAutoConfigurationTests {
|
||||
@Test
|
||||
public void testDefaultRepositoryConfiguration() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class,
|
||||
this.context.register(TestConfiguration.class, MongoAutoConfiguration.class,
|
||||
MongoRepositoriesAutoConfiguration.class,
|
||||
MongoTemplateAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(CityRepository.class));
|
||||
@@ -60,8 +62,9 @@ public class MongoRepositoriesAutoConfigurationTests {
|
||||
@Test
|
||||
public void testNoRepositoryConfiguration() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(EmptyConfiguration.class,
|
||||
this.context.register(EmptyConfiguration.class, MongoAutoConfiguration.class,
|
||||
MongoRepositoriesAutoConfiguration.class,
|
||||
MongoTemplateAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
|
||||
@@ -73,7 +76,8 @@ public class MongoRepositoriesAutoConfigurationTests {
|
||||
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(CustomizedConfiguration.class,
|
||||
MongoRepositoriesAutoConfiguration.class,
|
||||
MongoAutoConfiguration.class, MongoRepositoriesAutoConfiguration.class,
|
||||
MongoTemplateAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(CityMongoDbRepository.class));
|
||||
|
||||
Reference in New Issue
Block a user