【问题标题】:spring security,spring with jsf project user activity trackerspring security,spring with jsf 项目用户活动跟踪器
【发布时间】:2018-12-06 18:48:16
【问题描述】:

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

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

自定义注释

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

}

LoggingAspect

@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 方法时,我没有从日志拦截器收到任何消息来跟踪执行时间。

【问题讨论】:

  • JSF 可以使用 spring 管理的 beans 而不是 jsf 管理的。在前者中,您可以轻松使用我猜的 spring aop

标签: java spring spring-security jsf-2


【解决方案1】:

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

我的代码中的问题:在我上面的代码中,我在私有方法中调用了注解,并使用 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);
        }

    }

}

【讨论】:

    猜你喜欢
    • 2017-06-08
    • 2015-03-29
    • 2011-03-27
    • 1970-01-01
    • 2019-12-24
    • 2020-08-27
    • 1970-01-01
    • 2016-09-01
    • 2012-03-04
    相关资源
    最近更新 更多