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,75 +16,86 @@ | ||||||
| 
 | 
 | ||||||
| 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 { | ||||||
| 
 | 
 | ||||||
| 	private String host; |     private String host; | ||||||
| 
 | 
 | ||||||
| 	private int port = DBPort.PORT; |     private int port = DBPort.PORT; | ||||||
| 
 | 
 | ||||||
| 	private String uri = "mongodb://localhost/test"; |     private String uri = "mongodb://localhost/test"; | ||||||
| 
 | 
 | ||||||
| 	private String database; |     private String database; | ||||||
| 
 | 
 | ||||||
| 	public String getHost() { |     private String gridFsDatabase; | ||||||
| 		return this.host; |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	public void setHost(String host) { |  | ||||||
| 		this.host = host; |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	public String getDatabase() { |     public String getHost() { | ||||||
| 		return this.database; |         return this.host; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	public void setDatabase(String database) { |     public void setHost(String host) { | ||||||
| 		this.database = database; |         this.host = host; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	public String getUri() { |     public String getDatabase() { | ||||||
| 		return this.uri; |         return this.database; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	public void setUri(String uri) { |     public void setDatabase(String database) { | ||||||
| 		this.uri = uri; |         this.database = database; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	public int getPort() { |     public String getUri() { | ||||||
| 		return this.port; |         return this.uri; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	public void setPort(int port) { |     public void setUri(String uri) { | ||||||
| 		this.port = port; |         this.uri = uri; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	public String getMongoClientDatabase() { |     public int getPort() { | ||||||
| 		if (this.database != null) { |         return this.port; | ||||||
| 			return this.database; |     } | ||||||
| 		} |  | ||||||
| 		return new MongoClientURI(this.uri).getDatabase(); |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	public MongoClient createMongoClient() throws UnknownHostException { |     public void setPort(int port) { | ||||||
| 		if (this.host != null) { |         this.port = port; | ||||||
| 			return new MongoClient(this.host, this.port); |     } | ||||||
| 		} | 
 | ||||||
| 		return new MongoClient(new MongoClientURI(this.uri)); |     public String getGridFsDatabase() { | ||||||
| 	} |         return gridFsDatabase; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setGridFsDatabase(String gridFsDatabase) { | ||||||
|  |         this.gridFsDatabase = gridFsDatabase; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     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)); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -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