简体   繁体   English

如何创建将实现日志记录功能的方面 class

[英]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容器中的初始化顺序不同,其中两种如下

  • Implement EnvironmentAware interface实现 EnvironmentAware 接口
public class YourAspectClass implements EnvironmentAware {

    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

}
  • Using of @Value annotation to access property.使用@Value 注释访问属性。
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.

相关问题 如何将属性注入Aspect日志类? - How to inject property into Aspect logging class? 如何在Java中的Aspect类内实现线程安全缓存? - How to implement thread-safety cache inside Aspect class in Java? 如何在类上创建一个方面,这不是使用Spring AOP的bean? - How to create an aspect on class, that is not a bean using Spring AOP? 如何在 Go 中使用/实现面向方面? - How to use/implement aspect orientation in Go? 如何有效地实现数据库功能? - How to implement database functionality effectively? 在Java中,如何在Deque类的子类中实现新功能而不访问Deque类中的私有字段? - In Java, how can I implement new functionality in a subclass of a Deque class without access to the private fields in the Deque class? 如何实现扩展具有方面的字段的抽象方面的具体方面,该字段具有可在方面本身中进行加载时编织的注释 - How to implement concrete aspect extending abstract aspect having field with annotations eligible for load time weaving in aspect itself 如何实现本地日志系统? - How to implement a local logging system? 如何直接从用户的电子邮件中实现创建新密码功能 - how to implement the create new password functionality directly from user's email log4j和Logging Util类之间的性能和功能差异 - Performance and Functionality Difference between log4j and Logging Util class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM