12
我在訪問某個特定軟件包的類的方法時,遇到了問題,使我的日誌記錄方面記錄信息。換句話說,「不」記錄發生。我甚至感到絕望,並添加了System.out.println語句,但沒有運氣。Spring Boot Logger Aspects
我的課全部都位於org.my.package包下,即org.my.package.controller,org.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問題和在線教程之後,我找不到任何錯誤。
請指教。
您目前只在'org.my.package'類的子包中匹配類的方法。你可能想要的是'execution(* org.my.package .. *。*(..))'注意'..'而不是'''。 –
*臉掌*感謝!就是這樣。請回答一個答案,所以我可以給你這個問題的功勞。 – Rick