SpEL概念
SpEL全称为“Spring Expression Language”,简写为“SpEL”,Spring表达式语言。这里不多做介绍,请参考如下spring 官方文档:https://docs.spring.io/spring/docs/3.0.x/reference/expressions.html
1.动态mongo collection
@Document(collection = "t_login_log_#{@tenantProvider.get()}")
public class LoginLog extends BaseDomain{
public String userId;
public String userName;
//登陆设备:chrome,safari,firefox,android,ios...
public String device;
//app或者web
public String type;
}
TenantProvider
@Component("tenantProvider")
public class TenantProvider {
private final ThreadLocal<String> holder= new ThreadLocal<>();
public String get(){
return holder.get();
}
public void set(String tenantId){
if(tenantId==null){
throw new RuntimeException("tenantId not null.");
}
holder.set(tenantId);
}
public void clear(){
holder.remove();
}
}
2.mongo数据源配置
@Configuration
public class MongoConfig {
@Value("${mongodb.uri}")
private String mongoURI;
@Bean
public MongoDbFactory mongoDbFactory() throws UnknownHostException {
MongoClientURI mongoClientURI= new MongoClientURI(mongoURI);
return new SimpleMongoDbFactory(mongoClientURI);
}
}
3.配置application.properties
server.port= 9900
spring.application.name=service-log
mongodb.uri=mongodb://log:123456@10.0.0.222:27017/log