基于SpEL实现springboot 集成mongo动态集合存储

基于SpEL实现springboot 集成mongo动态集合存储_第1张图片
608152513.png

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 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:[email protected]:27017/log

你可能感兴趣的:(基于SpEL实现springboot 集成mongo动态集合存储)