2015-05-19 55 views
12

我在访问某个特定软件包的类的方法时,遇到了问题,使我的日志记录方面记录信息。换句话说,“不”记录发生。我甚至感到绝望,并添加了System.out.println语句,但没有运气。Spring Boot Logger Aspects

我的课全部都位于org.my.package包下,即org.my.package.controllerorg.my.package.model

这里我的应用程序类:

package org.my.package; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.EnableAspectJAutoProxy; 

@Configuration 
@ComponentScan(basePackages = {"org.my.package.config"}) 
@EnableAutoConfiguration 
@EnableAspectJAutoProxy 
public class FirstWebAppApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(FirstWebAppApplication.class, args); 
    } 
} 

这是我的配置类:

package org.my.package.config; 

import org.deloitte.javatraining.daythree.utilities.MyLogger; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.EnableAspectJAutoProxy; 

@Configuration 
@EnableAspectJAutoProxy 
@ComponentScan(basePackages = {"org.my.package.utilities"}) 
public class AssetConfig { 

    //----------------------------------------------------------------------------------------------------------------------- 
    @Bean 
    public MyLogger myLogger(){ 
     return new MyLogger(); 
    } 
} 

这是我的看点类:

package org.my.package.utilities; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 

import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.AfterReturning; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.springframework.stereotype.Component; 

@Aspect 
@Component 
public class MyLogger { 

    /** Handle to the log file */ 
    private final Log log = LogFactory.getLog(getClass()); 

    public MyLogger() {} 

    @AfterReturning("execution(* org.my.package.*.*(..))") 
    public void logMethodAccessAfter(JoinPoint joinPoint) { 
     log.info("***** Completed: " + joinPoint.getSignature().getName() + " *****"); 
     System.out.println("***** Completed: " + joinPoint.getSignature().getName() + " *****"); 
    } 

    @Before("execution(* org.my.package.*.*(..))") 
    public void logMethodAccessBefore(JoinPoint joinPoint) { 
     log.info("***** Starting: " + joinPoint.getSignature().getName() + " *****"); 
     System.out.println("***** Starting: " + joinPoint.getSignature().getName() + " *****"); 
    } 
} 

这是我的摇篮建立依赖关系:

dependencies { 
    compile("org.springframework.boot:spring-boot-starter-data-jpa") 
    compile("org.springframework.boot:spring-boot-starter-web") 
    compile('com.h2database:h2:1.3.156') 
    compile('javax.servlet:jstl:1.2') 
    compile('org.springframework.boot:spring-boot-starter-aop') 
    providedRuntime("org.apache.tomcat.embed:tomcat-embed-jasper") 
    testCompile("org.springframework.boot:spring-boot-starter-test") 
} 

我缺少的东西或其他错误配置我的看点类?在验证了其他类似的Stack Overflow问题和在线教程之后,我找不到任何错误。

请指教。

+1

您目前只在'org.my.package'类的子包中匹配类的方法。你可能想要的是'execution(* org.my.package .. *。*(..))'注意'..'而不是'''。 –

+0

*脸掌*感谢!就是这样。请回答一个答案,所以我可以给你这个问题的功劳。 – Rick

回答

12

您的减分0123,仅匹配在org.my.package包中的类的方法的执行而不是子包。

你可能想要的是execution(* org.my.package..*.*(..))注意..而不是.