adding initial GridFS integration.
This commit is contained in:
parent
4ffc95dc7f
commit
0d53b9d9fa
|
|
@ -0,0 +1,56 @@
|
||||||
|
package org.springframework.boot.autoconfigure.mongo;
|
||||||
|
|
||||||
|
import com.mongodb.Mongo;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
|
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 org.springframework.data.mongodb.MongoDbFactory;
|
||||||
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
|
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
||||||
|
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <P>
|
||||||
|
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration} for
|
||||||
|
* Mongo's {@link com.mongodb.gridfs.GridFS Grid FS} Spring Data's
|
||||||
|
* {@link org.springframework.data.mongodb.gridfs.GridFsTemplate GridFsTemplate}.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* You can override which GridFS database is used by specifying {@code spring.data.mongodb.gridFsDatabase},
|
||||||
|
* otherwise it defaults to the general {@code spring.data.mongodb.database} parameter.
|
||||||
|
*
|
||||||
|
* @author Josh Long
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass({Mongo.class, GridFsTemplate.class})
|
||||||
|
@EnableConfigurationProperties(MongoProperties.class)
|
||||||
|
@AutoConfigureAfter(MongoTemplateAutoConfiguration.class)
|
||||||
|
public class GridFsTemplateAutoConfiguration {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MongoProperties mongoProperties;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public MongoDbFactory mongoDbFactory(Mongo mongo) throws Exception {
|
||||||
|
|
||||||
|
String db = StringUtils.hasText(this.mongoProperties.getGridFsDatabase()) ?
|
||||||
|
this.mongoProperties.getGridFsDatabase() : this.mongoProperties.getMongoClientDatabase() ;
|
||||||
|
|
||||||
|
return new SimpleMongoDbFactory(mongo, db );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public GridFsTemplate gridFsTemplate(MongoDbFactory mongoDbFactory,
|
||||||
|
MongoTemplate mongoTemplate) {
|
||||||
|
return new GridFsTemplate(mongoDbFactory, mongoTemplate.getConverter());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -16,19 +16,19 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.mongo;
|
package org.springframework.boot.autoconfigure.mongo;
|
||||||
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
|
|
||||||
import com.mongodb.DBPort;
|
import com.mongodb.DBPort;
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
import com.mongodb.MongoClientURI;
|
import com.mongodb.MongoClientURI;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration properties for Mongo.
|
* Configuration properties for Mongo.
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Josh Long
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties(prefix = "spring.data.mongodb")
|
@ConfigurationProperties(prefix = "spring.data.mongodb")
|
||||||
public class MongoProperties {
|
public class MongoProperties {
|
||||||
|
|
@ -41,6 +41,9 @@ public class MongoProperties {
|
||||||
|
|
||||||
private String database;
|
private String database;
|
||||||
|
|
||||||
|
private String gridFsDatabase;
|
||||||
|
|
||||||
|
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
return this.host;
|
return this.host;
|
||||||
}
|
}
|
||||||
|
|
@ -73,6 +76,14 @@ public class MongoProperties {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getGridFsDatabase() {
|
||||||
|
return gridFsDatabase;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGridFsDatabase(String gridFsDatabase) {
|
||||||
|
this.gridFsDatabase = gridFsDatabase;
|
||||||
|
}
|
||||||
|
|
||||||
public String getMongoClientDatabase() {
|
public String getMongoClientDatabase() {
|
||||||
if (this.database != null) {
|
if (this.database != null) {
|
||||||
return this.database;
|
return this.database;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
package org.springframework.boot.autoconfigure.mongo;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
|
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class GridFsTemplateAutoConfigurationTests {
|
||||||
|
|
||||||
|
private AnnotationConfigApplicationContext context;
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void close() {
|
||||||
|
if (this.context != null) {
|
||||||
|
this.context.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void templateExists() {
|
||||||
|
this.context = new AnnotationConfigApplicationContext(
|
||||||
|
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
|
||||||
|
MongoTemplateAutoConfiguration.class , GridFsTemplateAutoConfiguration.class);
|
||||||
|
assertEquals(1, this.context.getBeanNamesForType(GridFsTemplate.class).length);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue