public interface UserService {
void addUser(String name);
}
public class UserServiceImpl implements UserService {
public void addUser(String name) {
System.out.println("Add user: " + name);
}
}
public class LoggingAspect {
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("Before method: " + joinPoint.getSignature().getName());
}
}
<bean id="userService" class="com.example.UserServiceImpl" />
<bean id="loggingAspect" class="com.example.LoggingAspect" />
<aop:config>
<aop:aspect ref="loggingAspect">
<aop:pointcut expression="execution(* com.example.UserService.addUser(..))" id="addUserPointcut" />
<aop:before method="beforeAdvice" pointcut-ref="addUserPointcut" />
</aop:aspect>
</aop:config>
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.addUser("Alice");
}
}
Before method: addUser
Add user: Alice