提交 4bd47ecd 编写于 作者: 微笑很纯洁's avatar 微笑很纯洁

add spring-boot-multi-mongodb

上级 6c3e9832
......@@ -14,6 +14,7 @@ Spring boot使用的各种示例,以最简单、最实用为标准
- [spring-boot-web](https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-web):web开发综合使用案例
- [spring-boot-mail](https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-mail):spring boot和邮件服务
- [spring-boot-mongodb](https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-mongodb):spring boot和mongodb的使用
- [spring-boot-multi-mongodb](https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-multi-mongodb):spring boot和mongodb多数据源的使用
- [Favorites-web](https://github.com/cloudfavorites/favorites-web):云收藏(springboot实战开源软件)
......@@ -34,8 +35,9 @@ Spring boot使用的各种示例,以最简单、最实用为标准
- [springboot实战:我们的第一款开源软件](http://www.ityouknow.com/springboot/2016/09/26/springboot%E5%AE%9E%E6%88%98-%E6%88%91%E4%BB%AC%E7%9A%84%E7%AC%AC%E4%B8%80%E6%AC%BE%E5%BC%80%E6%BA%90%E8%BD%AF%E4%BB%B6.html)
> 如果大家想了解关于springboot的其它方面应用,也可以以issues的形式反馈给我,我后续来完善。
关注公众号:纯洁的微笑,回复"springboot"进群交流
![](http://www.ityouknow.com/assets/images/keeppuresmile_430.jpg)
......
<?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>
<groupId>com.neo</groupId>
<artifactId>spring-boot-multi-mongodb</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>spring-boot-multi-mongodb</name>
<description>Demo project for Spring Boot and multi mongodb</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.neo;
import com.neo.config.props.MultipleMongoProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableConfigurationProperties(MultipleMongoProperties.class)
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.neo.config;
import com.mongodb.MongoClient;
import com.neo.config.props.MultipleMongoProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
/**
* @author neo
*/
@Configuration
public class MultipleMongoConfig {
@Autowired
private MultipleMongoProperties mongoProperties;
@Primary
@Bean(name = PrimaryMongoConfig.MONGO_TEMPLATE)
public MongoTemplate primaryMongoTemplate() throws Exception {
return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
}
@Bean
@Qualifier(SecondaryMongoConfig.MONGO_TEMPLATE)
public MongoTemplate secondaryMongoTemplate() throws Exception {
return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
}
@Bean
@Primary
public MongoDbFactory primaryFactory(MongoProperties mongo) throws Exception {
return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
mongo.getDatabase());
}
@Bean
public MongoDbFactory secondaryFactory(MongoProperties mongo) throws Exception {
return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
mongo.getDatabase());
}
}
package com.neo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
/**
* @author neo
*/
@Configuration
@EnableMongoRepositories(basePackages = "com.neo.model.repository.primary",
mongoTemplateRef = PrimaryMongoConfig.MONGO_TEMPLATE)
public class PrimaryMongoConfig {
protected static final String MONGO_TEMPLATE = "primaryMongoTemplate";
}
package com.neo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
/**
* @author neo
*/
@Configuration
@EnableMongoRepositories(basePackages = "com.neo.model.repository.secondary",
mongoTemplateRef = SecondaryMongoConfig.MONGO_TEMPLATE)
public class SecondaryMongoConfig {
protected static final String MONGO_TEMPLATE = "secondaryMongoTemplate";
}
package com.neo.config.props;
import lombok.Data;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author neo
*/
@Data
@ConfigurationProperties(prefix = "mongodb")
public class MultipleMongoProperties {
private MongoProperties primary = new MongoProperties();
private MongoProperties secondary = new MongoProperties();
}
package com.neo.model.repository.primary;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author neo
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "first_mongo")
public class PrimaryMongoObject {
@Id
private String id;
private String value;
@Override
public String toString() {
return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''
+ '}';
}
}
package com.neo.model.repository.primary;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* @author neo
*/
public interface PrimaryRepository extends MongoRepository<PrimaryMongoObject, String> {
}
package com.neo.model.repository.secondary;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author neo
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "second_mongo")
public class SecondaryMongoObject {
@Id
private String id;
private String value;
@Override
public String toString() {
return "SecondaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''
+ '}';
}
}
package com.neo.model.repository.secondary;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* @author neo
*/
public interface SecondaryRepository extends MongoRepository<SecondaryMongoObject, String> {
}
mongodb:
primary:
host: 192.168.9.60
port: 20000
database: test
secondary:
host: 192.168.9.61
port: 20000
database: test1
\ No newline at end of file
package com.neo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void contextLoads() {
System.out.println("hello world");
}
}
package com.neo.model.repository;
import com.neo.model.repository.primary.PrimaryMongoObject;
import com.neo.model.repository.primary.PrimaryRepository;
import com.neo.model.repository.secondary.SecondaryMongoObject;
import com.neo.model.repository.secondary.SecondaryRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* Created by neo on 2017/5/6.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MuliDatabaseTest {
@Autowired
private PrimaryRepository primaryRepository;
@Autowired
private SecondaryRepository secondaryRepository;
@Test
public void TestSave() {
System.out.println("************************************************************");
System.out.println("测试开始");
System.out.println("************************************************************");
this.primaryRepository
.save(new PrimaryMongoObject(null, "第一个库的对象"));
this.secondaryRepository
.save(new SecondaryMongoObject(null, "第二个库的对象"));
List<PrimaryMongoObject> primaries = this.primaryRepository.findAll();
for (PrimaryMongoObject primary : primaries) {
System.out.println(primary.toString());
}
List<SecondaryMongoObject> secondaries = this.secondaryRepository.findAll();
for (SecondaryMongoObject secondary : secondaries) {
System.out.println(secondary.toString());
}
System.out.println("************************************************************");
System.out.println("测试完成");
System.out.println("************************************************************");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册