spring security,spring with jsf project user activity tracke



我想为我的项目添加一个用户活动跟踪器。 该项目使用 Spring 和 JSF 作为视图。知道如何在 JSF 支持豆中添加弹簧 AOP 吗?

我有一个基于 Spring 的应用程序,我目前正在编写一个自定义注释来跟踪用户活动,包括时间。我从自定义注释@Timed开始跟踪方法执行时间。下面是代码片段:

自定义注释

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
}

伐木方面

@Aspect
@Component
public class LoggingAspect  {

@Around("@annotation(LogExecutionTime)")
public Object logExcecutionTime(ProceedingJoinPoint joinPoint) throws Throwable{
Object value=null;
long currentTimeInMillisec=Instant.now().toEpochMilli();
value=joinPoint.proceed();
Long executionTime=(Instant.now().toEpochMilli())-currentTimeInMillisec;
System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
return value;
}

测试主类

@Component
public class TestRunAnnotation {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"file:WebContent/WEB-INF/spring-aspect-config.xml");

test();
}

@LogExecutionTime
private static void test() {
for (int i=0;i<5;i++) {
i++;
System.out.println("print value of i"+i);
}
}
}

在 xml 配置中

<context:component-scan base-package="...">
</context:component-scan>
<!-- Enable @AspectJ annotation support  -->
<aop:aspectj-autoproxy />

<!-- Logging Aspect -->
<bean id="loggingAspect" class="......LoggingAspect" />
<bean id="testRunAnnotation" class="....TestRunAnnotation" />
<aop:aspectj-autoproxy proxy-target-class="true"/>
<tx:annotation-driven/>
</beans>

运行main方法时,我没有从日志记录拦截器收到任何消息来跟踪执行时间。

我得到了解决方案,它现在对我有用。

我的代码中的问题:在上面的代码中,我在私有方法中调用了注释,并使用 Spring 管理的 bean 来调用此方法。我做了以下更改以使其工作,

@Component
public class TestRunAnnotation {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"file:WebContent/WEB-INF/spring-aspect-config.xml");

TestRunAnnotation test=(TestRunAnnotation) context.getBean("testRunAnnotation");
test.test();
}

@LogExecutionTime
public  void test() {
for (int i=0;i<6;i++) {
i++;
System.out.println("print value of i"+i);
}
}
}

相关内容

最新更新