public interface CustomerService {
void addCustomer(String name);
String getCustomer(String name);
}
public class CustomerServiceImpl implements CustomerService {
private Map<String, String> customers = new HashMap<>();
@Override
public void addCustomer(String name) {
customers.put(name, name);
System.out.println("Customer added: " + name);
}
@Override
public String getCustomer(String name) {
return customers.get(name);
}
}
@Aspect
public class LoggingAspect {
@Before("execution(* com.example.CustomerService.addCustomer(..))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("Before method: " + joinPoint.getSignature().getName());
}
@After("execution(* com.example.CustomerService.addCustomer(..))")
public void logAfter(JoinPoint joinPoint) {
System.out.println("After method: " + joinPoint.getSignature().getName());
}
}
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.example")
public class AppConfig {
@Bean
public CustomerService customerService() {
return new CustomerServiceImpl();
}
@Bean
public LoggingAspect loggingAspect() {
return new LoggingAspect();
}
}
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
CustomerService customerService = context.getBean(CustomerService.class);
customerService.addCustomer("John Doe");
context.close();
}
}