Add Mongo auto configuration

Spring Data Mongo is the only dependency so it doesn't
really need a new starter. Added a sample though.
pull/50/head
Dave Syer 11 years ago
parent d06d202fd0
commit 191894a16a

@ -91,6 +91,11 @@
<artifactId>spring-data-jpa</artifactId> <artifactId>spring-data-jpa</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-acl</artifactId> <artifactId>spring-security-acl</artifactId>

@ -0,0 +1,125 @@
/*
* Copyright 2012-2013 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.lang.annotation.Annotation;
import java.util.Collection;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfigurationUtils;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryBeanDefinitionBuilder;
import org.springframework.data.repository.config.RepositoryBeanNameGenerator;
import org.springframework.data.repository.config.RepositoryConfiguration;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
/**
* @author Dave Syer
*/
public abstract class AbstractRepositoryConfigurationSourceSupport implements
BeanFactoryAware, ImportBeanDefinitionRegistrar, BeanClassLoaderAware {
private ClassLoader beanClassLoader;
private static Log logger = LogFactory
.getLog(AbstractRepositoryConfigurationSourceSupport.class);
private BeanFactory beanFactory;
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
final BeanDefinitionRegistry registry) {
final ResourceLoader resourceLoader = new DefaultResourceLoader();
final AnnotationRepositoryConfigurationSource configurationSource = getConfigurationSource();
final RepositoryConfigurationExtension extension = getRepositoryConfigurationExtension();
extension.registerBeansForRoot(registry, configurationSource);
final RepositoryBeanNameGenerator generator = new RepositoryBeanNameGenerator();
generator.setBeanClassLoader(this.beanClassLoader);
Collection<RepositoryConfiguration<AnnotationRepositoryConfigurationSource>> repositoryConfigurations = extension
.getRepositoryConfigurations(configurationSource, resourceLoader);
for (final RepositoryConfiguration<AnnotationRepositoryConfigurationSource> repositoryConfiguration : repositoryConfigurations) {
RepositoryBeanDefinitionBuilder builder = new RepositoryBeanDefinitionBuilder(
repositoryConfiguration, extension);
BeanDefinitionBuilder definitionBuilder = builder.build(registry,
resourceLoader);
extension.postProcess(definitionBuilder, configurationSource);
String beanName = generator.generateBeanName(
definitionBuilder.getBeanDefinition(), registry);
registry.registerBeanDefinition(beanName,
definitionBuilder.getBeanDefinition());
}
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
protected abstract RepositoryConfigurationExtension getRepositoryConfigurationExtension();
protected abstract AnnotationRepositoryConfigurationSource getConfigurationSource();
protected AnnotationRepositoryConfigurationSource getConfigurationSource(
Class<?> annotated, Class<? extends Annotation> annotation) {
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(annotated,
true);
AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
metadata, annotation) {
@Override
public java.lang.Iterable<String> getBasePackages() {
return AbstractRepositoryConfigurationSourceSupport.this
.getBasePackages();
};
};
return configurationSource;
}
protected Iterable<String> getBasePackages() {
List<String> basePackages = AutoConfigurationUtils
.getBasePackages(this.beanFactory);
if (basePackages.isEmpty()) {
logger.warn("Unable to find repository base packages. If you need Repositories please define "
+ "a @ComponentScan annotation or else disable *RepositoriesAutoConfiguration");
}
return basePackages;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}

@ -16,104 +16,31 @@
package org.springframework.boot.autoconfigure.data; package org.springframework.boot.autoconfigure.data;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfigurationUtils;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension; import org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource; import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryBeanDefinitionBuilder;
import org.springframework.data.repository.config.RepositoryBeanNameGenerator;
import org.springframework.data.repository.config.RepositoryConfiguration;
import org.springframework.data.repository.config.RepositoryConfigurationExtension; import org.springframework.data.repository.config.RepositoryConfigurationExtension;
import org.springframework.util.Assert;
/** /**
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data JPA * {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data JPA
* Repositories. * Repositories.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer
*/ */
class JpaRepositoriesAutoConfigureRegistrar implements ImportBeanDefinitionRegistrar, class JpaRepositoriesAutoConfigureRegistrar extends
BeanFactoryAware, BeanClassLoaderAware { AbstractRepositoryConfigurationSourceSupport {
private BeanFactory beanFactory;
private ClassLoader beanClassLoader;
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
final BeanDefinitionRegistry registry) {
final ResourceLoader resourceLoader = new DefaultResourceLoader();
final AnnotationRepositoryConfigurationSource configurationSource = getConfigurationSource();
final RepositoryConfigurationExtension extension = new JpaRepositoryConfigExtension();
extension.registerBeansForRoot(registry, configurationSource);
final RepositoryBeanNameGenerator generator = new RepositoryBeanNameGenerator();
generator.setBeanClassLoader(this.beanClassLoader);
Collection<RepositoryConfiguration<AnnotationRepositoryConfigurationSource>> repositoryConfigurations = extension
.getRepositoryConfigurations(configurationSource, resourceLoader);
for (final RepositoryConfiguration<AnnotationRepositoryConfigurationSource> repositoryConfiguration : repositoryConfigurations) {
RepositoryBeanDefinitionBuilder builder = new RepositoryBeanDefinitionBuilder(
repositoryConfiguration, extension);
BeanDefinitionBuilder definitionBuilder = builder.build(registry,
resourceLoader);
extension.postProcess(definitionBuilder, configurationSource);
String beanName = generator.generateBeanName(
definitionBuilder.getBeanDefinition(), registry);
registry.registerBeanDefinition(beanName,
definitionBuilder.getBeanDefinition());
}
}
private AnnotationRepositoryConfigurationSource getConfigurationSource() {
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(
EnableJpaRepositoriesConfiguration.class, true);
AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
metadata, EnableJpaRepositories.class) {
@Override
public java.lang.Iterable<String> getBasePackages() {
return JpaRepositoriesAutoConfigureRegistrar.this.getBasePackages();
};
};
return configurationSource;
}
protected Iterable<String> getBasePackages() {
List<String> basePackages = AutoConfigurationUtils
.getBasePackages(this.beanFactory);
Assert.notEmpty(
basePackages,
"Unable to find JPA repository base packages, please define "
+ "a @ComponentScan annotation or disable JpaRepositoriesAutoConfigure");
return basePackages;
}
@Override @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { protected AnnotationRepositoryConfigurationSource getConfigurationSource() {
this.beanFactory = beanFactory; return getConfigurationSource(EnableJpaRepositoriesConfiguration.class,
EnableJpaRepositories.class);
} }
@Override @Override
public void setBeanClassLoader(ClassLoader classLoader) { protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() {
this.beanClassLoader = classLoader; return new JpaRepositoryConfigExtension();
} }
@EnableJpaRepositories @EnableJpaRepositories

@ -0,0 +1,133 @@
/*
* Copyright 2012-2013 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 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 com.mongodb.DBPort;
import com.mongodb.Mongo;
import com.mongodb.MongoURI;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Mongo
* Repositories.
*
* @author Dave Syer
* @see EnableMongoRepositories
*/
@Configuration
@ConditionalOnClass({ Mongo.class, MongoRepository.class })
public class MongoRepositoriesAutoConfiguration {
@Import(MongoRepositoriesAutoConfigureRegistrar.class)
@Configuration
@EnableConfigurationProperties(MongoProperties.class)
protected static class MongoRepositoriesConfiguration {
@Autowired
private MongoProperties config;
@PreDestroy
public void close() throws UnknownHostException {
mongo().close();
}
@Bean
@ConditionalOnMissingBean(Mongo.class)
Mongo mongo() throws UnknownHostException {
return this.config.mongo();
}
@Bean
@ConditionalOnMissingBean(MongoTemplate.class)
MongoTemplate mongoTemplate(Mongo mongo) throws UnknownHostException {
return new MongoTemplate(mongo, this.config.database());
}
}
@ConfigurationProperties(name = "spring.data.mongo")
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 MongoURI(this.uri).getDatabase()
: this.database;
}
public Mongo mongo() throws UnknownHostException {
return this.host != null ? new Mongo(this.host, this.port) : new Mongo(
new MongoURI(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;
}
}
}

@ -0,0 +1,49 @@
/*
* Copyright 2012-2013 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 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.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
/**
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Mongo
* Repositories.
*
* @author Dave Syer
*/
class MongoRepositoriesAutoConfigureRegistrar extends
AbstractRepositoryConfigurationSourceSupport {
@Override
protected AnnotationRepositoryConfigurationSource getConfigurationSource() {
return getConfigurationSource(EnableMongoRepositoriesConfiguration.class,
EnableMongoRepositories.class);
}
@Override
protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() {
return new MongoRepositoryConfigurationExtension();
}
@EnableMongoRepositories
private static class EnableMongoRepositoriesConfiguration {
}
}

@ -5,6 +5,7 @@ org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\

@ -21,8 +21,8 @@ import javax.persistence.EntityManagerFactory;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.ComponentScanDetectorConfiguration; import org.springframework.boot.autoconfigure.ComponentScanDetectorConfiguration;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.test.City; import org.springframework.boot.autoconfigure.data.jpa.City;
import org.springframework.boot.autoconfigure.data.test.CityRepository; import org.springframework.boot.autoconfigure.data.jpa.CityRepository;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@ -20,8 +20,8 @@ import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration; import org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.data.test.City; import org.springframework.boot.autoconfigure.data.jpa.City;
import org.springframework.boot.autoconfigure.data.test.CityRepository; import org.springframework.boot.autoconfigure.data.jpa.CityRepository;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;

@ -0,0 +1,75 @@
/*
* Copyright 2012-2013 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 org.junit.Test;
import org.springframework.boot.autoconfigure.ComponentScanDetectorConfiguration;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.City;
import org.springframework.boot.autoconfigure.data.mongo.CityRepository;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.mongodb.Mongo;
import static org.junit.Assert.assertNotNull;
/**
* Tests for {@link MongoRepositoriesAutoConfiguration}.
*
* @author Dave Syer
*/
public class MongoRepositoriesAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@Test
public void testDefaultRepositoryConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class,
ComponentScanDetectorConfiguration.class,
MongoRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(CityRepository.class));
assertNotNull(this.context.getBean(Mongo.class));
}
@Test
public void testNoRepositoryConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(EmptyConfiguration.class,
ComponentScanDetectorConfiguration.class,
MongoRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(Mongo.class));
}
@Configuration
@ComponentScan(basePackageClasses = City.class)
protected static class TestConfiguration {
}
@Configuration
protected static class EmptyConfiguration {
}
}

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.autoconfigure.data.test; package org.springframework.boot.autoconfigure.data.jpa;
import java.io.Serializable; import java.io.Serializable;

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.autoconfigure.data.test; package org.springframework.boot.autoconfigure.data.jpa;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;

@ -0,0 +1,76 @@
/*
* Copyright 2012-2013 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.mongo;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class City implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String state;
@Column(nullable = false)
private String country;
@Column(nullable = false)
private String map;
protected City() {
}
public City(String name, String country) {
super();
this.name = name;
this.country = country;
}
public String getName() {
return this.name;
}
public String getState() {
return this.state;
}
public String getCountry() {
return this.country;
}
public String getMap() {
return this.map;
}
@Override
public String toString() {
return getName() + "," + getState() + "," + getCountry();
}
}

@ -0,0 +1,32 @@
/*
* Copyright 2012-2013 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.mongo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
public interface CityRepository extends Repository<City, Long> {
Page<City> findAll(Pageable pageable);
Page<City> findByNameLikeAndCountryLikeAllIgnoringCase(String name, String country,
Pageable pageable);
City findByNameAndCountryAllIgnoringCase(String name, String country);
}

@ -19,6 +19,7 @@
<module>spring-boot-sample-aop</module> <module>spring-boot-sample-aop</module>
<module>spring-boot-sample-batch</module> <module>spring-boot-sample-batch</module>
<module>spring-boot-sample-data-jpa</module> <module>spring-boot-sample-data-jpa</module>
<module>spring-boot-sample-data-mongo</module>
<module>spring-boot-sample-integration</module> <module>spring-boot-sample-integration</module>
<module>spring-boot-sample-jetty</module> <module>spring-boot-sample-jetty</module>
<module>spring-boot-sample-profile</module> <module>spring-boot-sample-profile</module>

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-data-mongo</artifactId>
<packaging>jar</packaging>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,29 @@
package org.springframework.boot.sample.data.mongo;
import org.springframework.data.annotation.Id;
public class Customer {
@Id
private String id;
private String firstName;
private String lastName;
public Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}

@ -0,0 +1,12 @@
package org.springframework.boot.sample.data.mongo;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}

@ -0,0 +1,67 @@
/*
* Copyright 2013 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.sample.data.mongo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SampleMongoApplication implements CommandLineRunner {
@Autowired
private CustomerRepository repository;
@Override
public void run(String... args) throws Exception {
repository.deleteAll();
// save a couple of customers
repository.save(new Customer("Alice", "Smith"));
repository.save(new Customer("Bob", "Smith"));
// fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
// fetch an individual customer
System.out.println("Customer found with findByFirstName('Alice'):");
System.out.println("--------------------------------");
System.out.println(repository.findByFirstName("Alice"));
System.out.println("Customers found with findByLastName('Smith'):");
System.out.println("--------------------------------");
for (Customer customer : repository.findByLastName("Smith")) {
System.out.println(customer);
}
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleMongoApplication.class, args);
}
}

@ -0,0 +1,42 @@
/*
* Copyright 2012-2013 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.sample.data.mongo;
import static org.junit.Assert.assertTrue;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.OutputCapture;
/**
* Tests for {@link SampleMongoApplication}.
*
* @author Dave Syer
*/
public class SampleMongoApplicationTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();
@Test
public void testDefaultSettings() throws Exception {
SampleMongoApplication.main(new String[0]);
String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("firstName='Alice', lastName='Smith'"));
}
}

@ -110,11 +110,16 @@ public abstract class AnsiOutput {
} }
private static boolean detectIfEnabled() { private static boolean detectIfEnabled() {
try {
if (System.console() == null) { if (System.console() == null) {
return false; return false;
} }
return !(OPERATING_SYSTEM_NAME.indexOf("win") >= 0); return !(OPERATING_SYSTEM_NAME.indexOf("win") >= 0);
} }
catch (Throwable e) {
return false;
}
}
public static enum Enabled { public static enum Enabled {
DETECT, ALWAYS, NEVER DETECT, ALWAYS, NEVER

Loading…
Cancel
Save