[英]How to create an aspect class that will implement the logging functionality
How to create an aspect class that will implement the logging functionality.如何创建将实现日志记录功能的方面 class。 Logged classes/packages/methods need to be defined in the configuration file.需要在配置文件中定义记录的类/包/方法。
@Aspect
public class LoggingAspect {
private static final Logger log = LoggerFactory.getLogger(LoggingAspect.class);
@Autowired
private List<PackageProperties> packageProperties;
@Pointcut("execution(* org.epam.operations.Operations.removeStudentFromList())")
public void removeStudentLog() {
}
@After("removeStudentLog()")
public void applicationLogger() {
log.info("Student deleted");
}
}
application.properties应用程序.properties
remove.packageName = org.epam.operations
remove.className = Operations
remove.methodName = removeStudentFromList
add.packageName = org.epam.operations
add.className = Operations
add.methodName = addStudent
For loading the value from properties file you have many ways, which mostly are different in initialization order in IOC container, two of them are as follow从属性文件中加载值有很多种方法,主要是IOC容器中的初始化顺序不同,其中两种如下
public class YourAspectClass implements EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
package org.springframework.beans.factory.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {
String value();
}
@Value("#{pointer_of_property}") String value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.