Программисты JAVA общаются здесь
Anonymous
Spring AOP с советами Вокруг и @annotation не работает
Сообщение
Anonymous » 10 дек 2025, 10:02
Я использую Spring AOP для входа в свое приложение. Вот файл applicationContext.xml
и мой aopLogging.xml —
и мой класс Aspect
Код: Выделить всё
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
@Aspect
@Component
public class AopLoggingAspect {
private static final Logger logger = Logger.getLogger(AopLoggingAspect.class);
@Around(value="@annotation(com.template.log.Loggable)")
public Object logAround(final ProceedingJoinPoint joinPoint) throws Throwable{
Object retVal = null;
try {
StringBuffer startMessageStringBuffer = new StringBuffer();
startMessageStringBuffer.append("Start method execution :: ");
startMessageStringBuffer.append(joinPoint.getSignature().getName());
startMessageStringBuffer.append("(");
Object[] args = joinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
startMessageStringBuffer.append(args[i]).append(",");
}
if (args.length > 0) {
startMessageStringBuffer.deleteCharAt(startMessageStringBuffer.length() - 1);
}
startMessageStringBuffer.append(")");
logger.info(startMessageStringBuffer.toString());
StopWatch stopWatch = new StopWatch();
stopWatch.start();
retVal = joinPoint.proceed();
stopWatch.stop();
StringBuffer endMessageStringBuffer = new StringBuffer();
endMessageStringBuffer.append("Finish method ");
endMessageStringBuffer.append(joinPoint.getSignature().getName());
endMessageStringBuffer.append("(..); execution time: ");
endMessageStringBuffer.append(stopWatch.getTotalTimeMillis());
endMessageStringBuffer.append(" ms;");
logger.info(endMessageStringBuffer.toString());
} catch(Exception ex) {
StringBuffer errorMessageStringBuffer = new StringBuffer();
logger.error(errorMessageStringBuffer.toString(), ex);
throw ex;
}
return retVal;
}
}
и моя пользовательская аннотация
Код: Выделить всё
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Loggable {
}
и мой класс обслуживания
Код: Выделить всё
public class UserService {
@Transactional(readOnly=true)
@Loggable
public User getUserByUserId(Long userId){
return userRepository.findOne(userId);
}
}
Проблема в том, что журналы не распечатываются. Пожалуйста, помогите мне. Заранее спасибо. Пожалуйста, дайте мне знать, если понадобится какая-либо другая информация.
Подробнее здесь:
https://stackoverflow.com/questions/198 ... ot-working
1765350135
Anonymous
Я использую Spring AOP для входа в свое приложение. Вот файл applicationContext.xml [code] [/code] и мой aopLogging.xml — [code] [/code] и мой класс Aspect [code]import org.apache.log4j.Logger; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; @Aspect @Component public class AopLoggingAspect { private static final Logger logger = Logger.getLogger(AopLoggingAspect.class); @Around(value="@annotation(com.template.log.Loggable)") public Object logAround(final ProceedingJoinPoint joinPoint) throws Throwable{ Object retVal = null; try { StringBuffer startMessageStringBuffer = new StringBuffer(); startMessageStringBuffer.append("Start method execution :: "); startMessageStringBuffer.append(joinPoint.getSignature().getName()); startMessageStringBuffer.append("("); Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { startMessageStringBuffer.append(args[i]).append(","); } if (args.length > 0) { startMessageStringBuffer.deleteCharAt(startMessageStringBuffer.length() - 1); } startMessageStringBuffer.append(")"); logger.info(startMessageStringBuffer.toString()); StopWatch stopWatch = new StopWatch(); stopWatch.start(); retVal = joinPoint.proceed(); stopWatch.stop(); StringBuffer endMessageStringBuffer = new StringBuffer(); endMessageStringBuffer.append("Finish method "); endMessageStringBuffer.append(joinPoint.getSignature().getName()); endMessageStringBuffer.append("(..); execution time: "); endMessageStringBuffer.append(stopWatch.getTotalTimeMillis()); endMessageStringBuffer.append(" ms;"); logger.info(endMessageStringBuffer.toString()); } catch(Exception ex) { StringBuffer errorMessageStringBuffer = new StringBuffer(); logger.error(errorMessageStringBuffer.toString(), ex); throw ex; } return retVal; } } [/code] и моя пользовательская аннотация [code]import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface Loggable { } [/code] и мой класс обслуживания [code]public class UserService { @Transactional(readOnly=true) @Loggable public User getUserByUserId(Long userId){ return userRepository.findOne(userId); } } [/code] Проблема в том, что журналы не распечатываются. Пожалуйста, помогите мне. Заранее спасибо. Пожалуйста, дайте мне знать, если понадобится какая-либо другая информация. Подробнее здесь: [url]https://stackoverflow.com/questions/19833482/spring-aop-with-around-advice-and-annotation-not-working[/url]