如何在Web请求之外访问请求范围的bean

时间:2018-06-01 10:03:02

标签: spring-boot apache-kafka

我正在使用spring boot和kafka。我在Web请求之外访问请求范围的bean时遇到问题。当请求来自浏览器时,我正在调用生产者发送方法(基于条件)从一个服务发送字符串列表。现在请求结束,因此当Consumer尝试调用其他服务时,RequestContext bean不可用于Consumer。那我怎么做 ApplicationContext bean在Application上下文中可用,以便在AOP方面类中 它不是空的。请在下面找到代码。

RequestContext bean

@Component
@Scope(scopeName = "request")
public class RequestContext {
    private Table table;
}

AOP方面课

@Component
@Aspect
public class ServiceTransactionAspect{
@Autowired
    private ApplicationContext appContext;
@Around("execution(* com.demo..*Service.*(..))")
    public Object beforeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        String tableName = getTableName(joinPoint);
        Table tableConnection = hbaseConnectionUtil.getTableConnection(tableName);
        RequestContext requestContext = (RequestContext) appContext.getBean("requestContext");
        requestContext.setTable(tableConnection);
}

卡夫卡消费者

    @Service
    public class Consumer{

    @Autowired
    public ExportServiceImpl exportServiceImpl;
    @Autowired
    public Service2 service2;

     @KafkaListener(topics = "${app.topic.kafka}")
        public void listen(@Payload List<String> names){
        exportServiceImpl.writeFile();
    }
}

生产者发送消息的服务

@Service
@HTransaction(Demo.TABLE_NAME)
public class ExportServiceImpl implements ExportService {

    @Autowired
    public Producer producer; 

     Public FileModel export(ViewObject view){

     if(fileSize<50MB){
       directly write files
    }else{
       producer.send(view.getIds());
    }    

}
}

0 个答案:

没有答案
相关问题