'How to create an aspect class that will implement the logging functionality
How to create an aspect class that will implement the logging functionality. 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
remove.packageName = org.epam.operations
remove.className = Operations
remove.methodName = removeStudentFromList
add.packageName = org.epam.operations
add.className = Operations
add.methodName = addStudent
Solution 1:[1]
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
- Implement EnvironmentAware interface
public class YourAspectClass implements EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
- Using of @Value annotation to access property .
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lunatic |
