Spring Boot Mongodb
Spring注解学习,有助于更好的理解下面代码:
@ConditionOnClass
表明该@Configuration
仅仅在一定条件下才会被加载,这里的条件是Mongo.class
位于类路径上@EnableConfigurationProperties
将Spring Boot的配置文件(application.properties
)中的spring.data.mongodb.*
属性映射为MongoProperties
并注入到MongoAutoConfiguration
中。@ConditionalOnMissingBean
说明Spring Boot仅仅在当前上下文中不存在Mongo
对象时,才会实例化一个Bean。这个逻辑也体现了Spring
Boot的另外一个特性——自定义的Bean优先于框架的默认配置,我们如果显式的在业务代码中定义了一个Mongo
对象,那么Spring Boot就不再创建。
一、先看看spring boot mongo部分源码片段
/*
* Copyright 2012-2015 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 org.springframework.core.env.Environment; import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions; /**
* {@link EnableAutoConfiguration Auto-configuration} for Mongo.
*
* @author Dave Syer
* @author Oliver Gierke
* @author Phillip Webb
*/
@Configuration
@ConditionalOnClass(MongoClient.class)
@EnableConfigurationProperties(MongoProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
public class MongoAutoConfiguration { @Autowired
private MongoProperties properties; @Autowired(required = false)
private MongoClientOptions options; @Autowired
private Environment environment; private MongoClient mongo; @PreDestroy
public void close() {
if (this.mongo != null) {
this.mongo.close();
}
} @Bean
@ConditionalOnMissingBean
public MongoClient mongo() throws UnknownHostException {
this.mongo = this.properties.createMongoClient(this.options, this.environment);
return this.mongo;
} }
/*
* Copyright 2012-2015 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 java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
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.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.mapping.model.FieldNamingStrategy;
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.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient; /**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's mongo support.
* <p>
* Registers a {@link MongoTemplate} and {@link GridFsTemplate} beans if no other beans of
* the same type are configured.
* <P>
* Honors the {@literal spring.data.mongodb.database} property if set, otherwise connects
* to the {@literal test} database.
*
* @author Dave Syer
* @author Oliver Gierke
* @author Josh Long
* @author Phillip Webb
* @author Eddú Meléndez
* @since 1.1.0
*/
@Configuration
@ConditionalOnClass({ Mongo.class, MongoTemplate.class })
@EnableConfigurationProperties(MongoProperties.class)
@AutoConfigureAfter(MongoAutoConfiguration.class)
public class MongoDataAutoConfiguration implements BeanClassLoaderAware { @Autowired
private MongoProperties properties; @Autowired
private Environment environment; @Autowired
private ResourceLoader resourceLoader; private ClassLoader classLoader; @Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
} @Bean
@ConditionalOnMissingBean(MongoDbFactory.class)
public SimpleMongoDbFactory mongoDbFactory(MongoClient mongo) throws Exception {
String database = this.properties.getMongoClientDatabase();
return new SimpleMongoDbFactory(mongo, database);
} @Bean
@ConditionalOnMissingBean
public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory,
MongoConverter converter) throws UnknownHostException {
return new MongoTemplate(mongoDbFactory, converter);
} @Bean
@ConditionalOnMissingBean(MongoConverter.class)
public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory,
MongoMappingContext context, BeanFactory beanFactory) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver,
context);
try {
mappingConverter.setCustomConversions(beanFactory
.getBean(CustomConversions.class));
}
catch (NoSuchBeanDefinitionException ex) {
// Ignore
}
return mappingConverter;
} @Bean
@ConditionalOnMissingBean
public MongoMappingContext mongoMappingContext(BeanFactory beanFactory)
throws ClassNotFoundException {
MongoMappingContext context = new MongoMappingContext();
context.setInitialEntitySet(getInitialEntitySet(beanFactory));
Class<? extends FieldNamingStrategy> strategyClass = this.properties
.getFieldNamingStrategy();
if (strategyClass != null) {
context.setFieldNamingStrategy(BeanUtils.instantiate(strategyClass));
}
return context;
} private Set<Class<?>> getInitialEntitySet(BeanFactory beanFactory)
throws ClassNotFoundException {
Set<Class<?>> entitySet = new HashSet<Class<?>>();
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
false);
scanner.setEnvironment(this.environment);
scanner.setResourceLoader(this.resourceLoader);
scanner.addIncludeFilter(new AnnotationTypeFilter(Document.class));
scanner.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));
for (String basePackage : getMappingBasePackages(beanFactory)) {
if (StringUtils.hasText(basePackage)) {
for (BeanDefinition candidate : scanner
.findCandidateComponents(basePackage)) {
entitySet.add(ClassUtils.forName(candidate.getBeanClassName(),
this.classLoader));
}
}
}
return entitySet;
} private static Collection<String> getMappingBasePackages(BeanFactory beanFactory) {
try {
return AutoConfigurationPackages.get(beanFactory);
}
catch (IllegalStateException ex) {
// no auto-configuration package registered yet
return Collections.emptyList();
}
} @Bean
@ConditionalOnMissingBean
public GridFsTemplate gridFsTemplate(MongoDbFactory mongoDbFactory,
MongoTemplate mongoTemplate) {
return new GridFsTemplate(new GridFsMongoDbFactory(mongoDbFactory,
this.properties), mongoTemplate.getConverter());
} /**
* {@link MongoDbFactory} decorator to respect
* {@link MongoProperties#getGridFsDatabase()} if set.
*/
private static class GridFsMongoDbFactory implements MongoDbFactory { private final MongoDbFactory mongoDbFactory; private final MongoProperties properties; public GridFsMongoDbFactory(MongoDbFactory mongoDbFactory,
MongoProperties properties) {
Assert.notNull(mongoDbFactory, "MongoDbFactory must not be null");
Assert.notNull(properties, "Properties must not be null");
this.mongoDbFactory = mongoDbFactory;
this.properties = properties;
} @Override
public DB getDb() throws DataAccessException {
String gridFsDatabase = this.properties.getGridFsDatabase();
if (StringUtils.hasText(gridFsDatabase)) {
return this.mongoDbFactory.getDb(gridFsDatabase);
}
return this.mongoDbFactory.getDb();
} @Override
public DB getDb(String dbName) throws DataAccessException {
return this.mongoDbFactory.getDb(dbName);
} @Override
public PersistenceExceptionTranslator getExceptionTranslator() {
return this.mongoDbFactory.getExceptionTranslator();
} } }
/*
* Copyright 2012-2015 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 java.util.Arrays;
import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.data.mapping.model.FieldNamingStrategy; import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientOptions.Builder;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress; /**
* Configuration properties for Mongo.
*
* @author Dave Syer
* @author Phillip Webb
* @author Josh Long
* @author Andy Wilkinson
* @author Eddú Meléndez
*/
@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties { /**
* Default port used when the configured port is {@code null}.
*/
public static final int DEFAULT_PORT = 27017; /**
* Mongo server host.
*/
private String host; /**
* Mongo server port.
*/
private Integer port = null; /**
* Mongo database URI. When set, host and port are ignored.
*/
private String uri = "mongodb://localhost/test"; /**
* Database name.
*/
private String database; /**
* Authentication database name.
*/
private String authenticationDatabase; /**
* GridFS database name.
*/
private String gridFsDatabase; /**
* Login user of the mongo server.
*/
private String username; /**
* Login password of the mongo server.
*/
private char[] password; /**
* Fully qualified name of the FieldNamingStrategy to use.
*/
private Class<? extends FieldNamingStrategy> fieldNamingStrategy; 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 getAuthenticationDatabase() {
return this.authenticationDatabase;
} public void setAuthenticationDatabase(String authenticationDatabase) {
this.authenticationDatabase = authenticationDatabase;
} public String getUsername() {
return this.username;
} public void setUsername(String username) {
this.username = username;
} public char[] getPassword() {
return this.password;
} public void setPassword(char[] password) {
this.password = password;
} public Class<? extends FieldNamingStrategy> getFieldNamingStrategy() {
return this.fieldNamingStrategy;
} public void setFieldNamingStrategy(
Class<? extends FieldNamingStrategy> fieldNamingStrategy) {
this.fieldNamingStrategy = fieldNamingStrategy;
} public void clearPassword() {
if (this.password == null) {
return;
}
for (int i = 0; i < this.password.length; i++) {
this.password[i] = 0;
}
} public String getUri() {
return this.uri;
} public void setUri(String uri) {
this.uri = uri;
} public Integer getPort() {
return this.port;
} public void setPort(Integer port) {
this.port = port;
} public String getGridFsDatabase() {
return this.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();
} /**
* Creates a {@link MongoClient} using the given {@code options}
*
* @param options the options
* @return the Mongo client
* @throws UnknownHostException if the configured host is unknown
* @deprecated Since 1.3.0 in favour of
* {@link #createMongoClient(MongoClientOptions, Environment)}
*/
@Deprecated
public MongoClient createMongoClient(MongoClientOptions options)
throws UnknownHostException {
return this.createMongoClient(options, null);
} /**
* Creates a {@link MongoClient} using the given {@code options} and
* {@code environment}. If the configured port is zero, the value of the
* {@code local.mongo.port} property retrieved from the {@code environment} is used
* to configure the client.
*
* @param options the options
* @param environment the environment
* @return the Mongo client
* @throws UnknownHostException if the configured host is unknown
*/
public MongoClient createMongoClient(MongoClientOptions options,
Environment environment) throws UnknownHostException {
try {
if (hasCustomAddress() || hasCustomCredentials()) {
if (options == null) {
options = MongoClientOptions.builder().build();
}
List<MongoCredential> credentials = null;
if (hasCustomCredentials()) {
String database = this.authenticationDatabase == null ? getMongoClientDatabase()
: this.authenticationDatabase;
credentials = Arrays.asList(MongoCredential.createMongoCRCredential(
this.username, database, this.password));
}
String host = this.host == null ? "localhost" : this.host;
int port = determinePort(environment);
return new MongoClient(Arrays.asList(new ServerAddress(host, port)),
credentials, options);
}
// The options and credentials are in the URI
return new MongoClient(new MongoClientURI(this.uri, builder(options)));
}
finally {
clearPassword();
}
} private boolean hasCustomAddress() {
return this.host != null || this.port != null;
} private boolean hasCustomCredentials() {
return this.username != null && this.password != null;
} private int determinePort(Environment environment) {
if (this.port == null) {
return DEFAULT_PORT;
}
if (this.port == 0) {
if (environment != null) {
String localPort = environment.getProperty("local.mongo.port");
if (localPort != null) {
return Integer.valueOf(localPort);
}
}
throw new IllegalStateException(
"spring.data.mongodb.port=0 and no local mongo port configuration "
+ "is available");
}
return this.port;
} private Builder builder(MongoClientOptions options) {
Builder builder = MongoClientOptions.builder();
if (options != null) {
builder.alwaysUseMBeans(options.isAlwaysUseMBeans());
builder.connectionsPerHost(options.getConnectionsPerHost());
builder.connectTimeout(options.getConnectTimeout());
builder.cursorFinalizerEnabled(options.isCursorFinalizerEnabled());
builder.dbDecoderFactory(options.getDbDecoderFactory());
builder.dbEncoderFactory(options.getDbEncoderFactory());
builder.description(options.getDescription());
builder.maxWaitTime(options.getMaxWaitTime());
builder.readPreference(options.getReadPreference());
builder.socketFactory(options.getSocketFactory());
builder.socketKeepAlive(options.isSocketKeepAlive());
builder.socketTimeout(options.getSocketTimeout());
builder.threadsAllowedToBlockForConnectionMultiplier(options
.getThreadsAllowedToBlockForConnectionMultiplier());
builder.writeConcern(options.getWriteConcern());
}
return builder;
} }
------------------------------------------------------------------------------------------------------------------------------------------------------
以上为spring boot mongo的片段,可以看出spring boot已经配置好了,也就是说springboot启动后@Bean创建的对象可以直接使用@Autowrite直接使用
关注点(spring boot启动会直接读取application.yml或者是application.properties,这里我用的是yml)
@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties
启动入口
Applicaiton.java
package com.modou.weixin; 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;
import org.springframework.data.mongodb.core.MongoTemplate; /**
* Created by chababa on 15/8/22.
*/
@Configuration
@ComponentScan(basePackages={"com.modou.conf","com.modou.weixin"})
@EnableAutoConfiguration
public class Application implements CommandLineRunner{
@Autowired
MongoTemplate template; public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} @Override
public void run(String... args) throws Exception {
System.err.println(template != null);
}
}
注入MongoTemplate结果
# SPRING PROFILES
spring:
# HTTP ENCODING
http:
encoding.charset: UTF-8
encoding.enable: true
encoding.force: true
data:
mongodb:
host: 127.0.0.1
port: 27017
username:
password:
database: xx
authenticationDatabase:
Spring Boot Mongodb的更多相关文章
- spring boot MongoDB的集成和使用
前言 上一章节,简单讲解了如何集成Spring-data-jpa.本章节,我们来看看如何集成NoSQL的Mongodb.mongodb是最早热门非关系数据库的之一,使用也比较普遍.最适合来存储一些非结 ...
- Spring Boot MongoDB JPA 简化开发
使用SpringBoot提供的@Repository接口,可以完成曾经需要大量代码编写和配置文件定制工作.这些以前让新手程序员头疼,让有经验的程序员引以为傲的配置,由于框架的不断完善,变得不那么重要, ...
- Spring Boot + MongoDB 使用示例
本文分别使用 MongoRepository 和 MongoTemplate 实现 MongoDB 的简单的增删改查 本文使用 docker 安装 MongoDB: 使用示例 application. ...
- Spring Boot MongoDB 查询操作 (BasicQuery ,BSON)
MongoDB 查询有四种方式:Query,TextQuery,BasicQuery 和 Bson ,网上太多关于 Query 的查询方式,本文只记录 BasicQuery和Bson 的方式,Basi ...
- spring boot MongoDb配置和多数据源
配置文件: # MongoDB配置项 mongodb.base.host: 192.168.1.204 mongodb. mongodb.base.database: xxx mongodb.base ...
- Spring boot整合Mongodb
最近的项目用了Mongodb,网上的用法大多都是七零八落的没有一个统一性,自己大概整理了下,项目中的相关配置就不叙述了,由于spring boot的快捷开发方式,所以spring boot项目中要使用 ...
- Spring Boot Logback应用日志
e Spring Boot Logback应用日志 2015-09-08 19:57 7673人阅读 评论(0) 收藏 举报 . 分类: Spring Boot(51) . 目录(?)[+] 日志对于 ...
- 从零部署Spring boot项目到云服务器(准备工作)
自己的博客终于成功部署上线了,回过头来总结记录一下整个项目的部署过程! 测试地址:47.94.154.205:8084 注:文末有福利! 一.Linux下应用Shell通过SSH连接云服务器 //ss ...
- 一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事
微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物.互联网时代的产品通常有两类特点:需求变化快和用户群体庞大,在这种情况 ...
随机推荐
- 一个RPC的demo
从下面的例子中可以看到,Consumer(client)的代码中引用了Provider部分的class,本例中是 com.provider.EchoServiceImpl和com.provider.E ...
- 基于visual Studio2013解决算法导论之022队列实现(基于链表)
题目 基于链表的队列实现 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> ...
- OnPaint()函数的作用原理
WM_PAINT是窗口每次重绘都会产生的一个消息. OnPaint是对这个消息的反应函数 mfc 的 CWnd::OnPaint 没做什么,只是丢给系统处理. 一 : 先执行OnEraseBkgnd, ...
- Android中的Audio播放:控制Audio输出通道切换
Audio 输出通道有很多,Speaker.headset.bluetooth A2DP等.通话或播放音乐等使用Audio输出过程中,可能发生Audio输出通道的切换.比如,插入有线耳机播放音乐时,声 ...
- 第一个hibernate文件 xml配置方法
package com.entity; public class User { private String username; private String password; private In ...
- html:打开新的页面
在html页面中,打开一个新的页面,有两种方式: 一.利用超链接 <a href="newurl">新页面</a> 上面代码添加了一个新链接,点击链接时会打 ...
- 修改 Mac 默认 PHP 运行环境,给 MAMP 配置全局 Composer
在没有配置全局性的 Composer 的时候,如果你在没有安装 Composer 的目录下运行 Composer 命令,比如:create-project 系统会返回: Could not open ...
- JPA相关知识点滴--持续更新中.....
Java 持久化(JPA) •Java EE 5 在EJB 3.0 中包含JPA 1.0 •参考实现:TopLink Essentials •Java EE 6 包含JPA 2.0 •参考实现:Ec ...
- 【通信框架】Apache的开源通信框架thrift概述
在阅读的过程中有不论什么问题.欢迎一起交流 邮箱:1494713801@qq.com QQ:1494713801 一.作用 Thrift("Scalable Cross-Languag ...
- VS2010对C++11的支持列表(感觉大部分都不支持)
c++11,就是之前的c++0x,已经成为了最新的c++标准.像咱这样天天用c++的,就赶紧follow一下.学习成果,放在这里,不说分享,至少自己增强下记忆. 首先,给出一些有用的链接. http: ...