[英]Spring AOP with Around advice and @annotation not working
I am using Spring AOP for logging in my application.我正在使用 Spring AOP 登录我的应用程序。 Here is the applicationContext.xml file这是 applicationContext.xml 文件
<mvc:annotation-driven />
<context:component-scan base-package="com.template" />
<context:annotation-config />
<jpa:repositories base-package="com.template.repository"/>
<tx:annotation-driven />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/template?autoReconnect=true"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
<property name="persistenceUnitName" value="template"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
and my aopLogging.xml is我的 aopLogging.xml 是
<bean id="aopLogging" class="com.template.log.AopLoggingAspect" />
<aop:aspectj-autoproxy proxy-target-class="false"/>
and my Aspect class is我的 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;
}
}
and my Custom annotation is我的自定义注释是
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 {
}
and my Service class is我的服务类是
public class UserService {
@Transactional(readOnly=true)
@Loggable
public User getUserByUserId(Long userId){
return userRepository.findOne(userId);
}
}
The problem is that the logs are not getting printed.问题是日志没有打印出来。 Please help me.请帮我。 Thanks in advance.提前致谢。 Please do let me know if any other info is needed.如果需要任何其他信息,请告诉我。
It seems to me you have forgot to import in your applicationContext.xml
file the aopLogging.xml
file.在我看来,您忘记在applicationContext.xml
文件中导入aopLogging.xml
文件。 Try to add this to your applicationContext.xml
file:尝试将其添加到您的applicationContext.xml
文件中:
<!-- Import your AspectJ config -->
<import resource="classpath:aopLogging.xml" />
Try this..尝试这个..
@Around(value="@annotation(Loggable)")
public Object logAround(final ProceedingJoinPoint joinPoint) throws Throwable{
Create a Annotation创建注释
@Target(ElementType.METHOD) @目标(元素类型。方法)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)public @interface LogExecutionTime { }公共@interface LogExecutionTime { }
Then an Aspect那么一个方面
@Aspect @方面
@Component @成分
public class LoggingAspect {公共类 LoggingAspect {
Then a method in the Aspect那么Aspect中的一个方法
@Around("@annotation(LogExecutionTime)") @Around("@annotation(LogExecutionTime)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {公共对象 logExecutionTime(ProceedingJoinPoint joinPoint) 抛出 Throwable {long start = System.currentTimeMillis(); Object proceed = joinPoint.proceed(); long executionTime = System.currentTimeMillis() - start; LOGGER.info(joinPoint.getSignature() + " executed in " + executionTime + "ms"); return proceed; }
Then use the Annotation Anywhere然后使用 Annotation Anywhere
@PostMapping(value="/signup") @PostMapping(值=“/注册”)
@LogExecutionTime @日志执行时间
public @ResponseBody ResponseObject register(User user){ }公共@ResponseBody ResponseObject 注册(用户用户){ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.