Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring AOP中拦截log4j2的线程上下文_Java_Spring_Spring Boot_Log4j2_Spring Aop - Fatal编程技术网

Java 在Spring AOP中拦截log4j2的线程上下文

Java 在Spring AOP中拦截log4j2的线程上下文,java,spring,spring-boot,log4j2,spring-aop,Java,Spring,Spring Boot,Log4j2,Spring Aop,我有一个SpringWeb应用程序,它从UI接收一些URI请求,并调用服务类中的业务逻辑来执行特定操作 我使用log4j2jdbc日志来登录数据库 现在,我想将会话Id记录在数据库的另一列中。为此,我使用ThreadContext映射传递要保存的会话Id,如下所述: 下面是我如何使用的快照: log4j配置: <Appenders> <Jdbc ignoreExceptions="true" name="db-appender" buffe

我有一个SpringWeb应用程序,它从UI接收一些URI请求,并调用服务类中的业务逻辑来执行特定操作

  • 我使用log4j2jdbc日志来登录数据库
  • 现在,我想将会话Id记录在数据库的另一列中。为此,我使用ThreadContext映射传递要保存的会话Id,如下所述:
下面是我如何使用的快照:

log4j配置:

<Appenders>
        <Jdbc ignoreExceptions="true" name="db-appender"
        bufferSize="${env:LOG_DB_BUFFER_SIZE}" tableName="test.&quot;LOGS&quot;">
        <ConnectionFactory class="com.pritam.logging.ConnectionFactory" method="getDatabaseConnection" />
        <Column name="dated" isUnicode="false" isEventTimestamp="true" />
        <Column name="logger" isUnicode="false" pattern="%logger" />
        <Column name="level" isUnicode="false" pattern="%level" />
        <Column name="message" isUnicode="false" pattern="%message" />
        <Column name="exception" isUnicode="false" pattern="%ex{full}" />
        <Column name="session_id" isUnicode="false" pattern="%X{session_id}"/>
    </Jdbc>
</Appenders>
这个很好用。并将会话id记录在另一列中

现在,我有n个doSomething方法,它们将在具有各种会话ID的独立线程中执行。 我不想一遍又一遍地写ThreadContext语句,所以我考虑使用SpringAOP建议(@Around建议)来完成这项任务

有人能给我解释一下如何在SpringAOP的@Around建议中注入ThreadContext吗


谢谢你

我是按照这些思路做的

假设我有一个组件

@Component
public class MyService { 
    public void doSomething() { ... } 
}
然后,我将其定义为方面:

@Component
@Aspect
public class MyServiceAspect { 
    @Before("execution(* <packages>.MyService.doSomething())")
    public void beforeDoSomething() {
        ThreadContext.put("key", "value");
    }

    @After("execution(* <packages>.MyService.doSomething())")
    public void afterDoSomething() {
        ThreadContext.clearAll(); 
    }
}
@组件
@面貌
公共类MyServiceSpect{
@在(“执行(*.MyService.doSomething())之前)
某事物之前的公共无效(){
ThreadContext.put(“键”、“值”);
}
@之后(“执行(*.MyService.doSomething())”)
公共无效后剂量法(){
clearAll();
}
}

希望能有所帮助

我会按照这些思路做些事情

假设我有一个组件

@Component
public class MyService { 
    public void doSomething() { ... } 
}
然后,我将其定义为方面:

@Component
@Aspect
public class MyServiceAspect { 
    @Before("execution(* <packages>.MyService.doSomething())")
    public void beforeDoSomething() {
        ThreadContext.put("key", "value");
    }

    @After("execution(* <packages>.MyService.doSomething())")
    public void afterDoSomething() {
        ThreadContext.clearAll(); 
    }
}
@组件
@面貌
公共类MyServiceSpect{
@在(“执行(*.MyService.doSomething())之前)
某事物之前的公共无效(){
ThreadContext.put(“键”、“值”);
}
@之后(“执行(*.MyService.doSomething())”)
公共无效后剂量法(){
clearAll();
}
}

希望能有所帮助

Hi@Radu,谢谢你的回答。我现在也在用同样的方法。嗨@Radu,谢谢你的回答。我现在也在用同样的方法。