HI我正在使用以下基于注释的配置,连接我在弹簧控制器上查看日志和其他交叉方法调用的方面。但似乎我正在使用的代码没有被调用。
@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan({"com.pumpkinsafari.api"})
public class WebConfig extends WebMvcConfigurerAdapter {
/** The Constant DD_MM_YYYY. */
private static final String DD_MM_YYYY = "yyyy-MM-dd";
/** The Constant DATE_FORMAT. */
private static final DateFormat DATE_FORMAT = new SimpleDateFormat(DD_MM_YYYY);
/**
* Instantiates a new web config.
*/
public WebConfig() {
super();
}
@Bean
public RestControllerAspect controllerAspect(){
return new RestControllerAspect();
}
// beans
/**
* Xstream marshaller.
*
* @return the x stream marshaller
*/
public XStreamMarshaller xstreamMarshaller() {
final XStreamMarshaller xStreamMarshaller = new XStreamMarshaller();
xStreamMarshaller.setAutodetectAnnotations(true);
xStreamMarshaller.setAnnotatedClasses(new Class[] { Principal.class, Customer.class, Role.class,
Privilege.class, SocialUser.class, SearchRequest.class });
xStreamMarshaller.getXStream().addDefaultImplementation(java.sql.Timestamp.class, java.util.Date.class);
return xStreamMarshaller;
}
/**
* Marshalling http message converter.
*
* @return the marshalling http message converter
*/
public MarshallingHttpMessageConverter marshallingHttpMessageConverter() {
final MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter();
final XStreamMarshaller xstreamMarshaller = xstreamMarshaller();
marshallingHttpMessageConverter.setMarshaller(xstreamMarshaller);
marshallingHttpMessageConverter.setUnmarshaller(xstreamMarshaller);
return marshallingHttpMessageConverter;
}
// template
/*
* (non-Javadoc)
*
* @see
* org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
* #configureMessageConverters(java.util.List)
*/
@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
messageConverters.add(marshallingHttpMessageConverter());
final ClassLoader classLoader = getClass().getClassLoader();
if (ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader)) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
jackson2HttpMessageConverter.getObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// Register date format for marshalling unmarshalling dates
jackson2HttpMessageConverter.getObjectMapper().setDateFormat(DATE_FORMAT);
messageConverters.add(jackson2HttpMessageConverter);
} else if (ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", classLoader)) {
MappingJacksonHttpMessageConverter jacksonHttpMessageConverter = new MappingJacksonHttpMessageConverter();
jacksonHttpMessageConverter.getObjectMapper().disable(
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
// Register date format for marshalling unmarshalling dates
jacksonHttpMessageConverter.getObjectMapper().setDateFormat(DATE_FORMAT);
messageConverters.add(jacksonHttpMessageConverter);
}
super.configureMessageConverters(messageConverters);
}
}
但我的方面没有被调用..方面类如下
@Aspect
public class RestControllerAspect {
@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controller() {
}
@Pointcut("execution(* *(..))")
public void methodPointcut() {
System.out.println("Invoked: ");
}
@Pointcut("within(@org.springframework.web.bind.annotation.RequestMapping *)")
public void requestMapping() {
System.out.println("Invoked: ");
}
@Before("controller() && methodPointcut() && requestMapping()")
public void aroundControllerMethod(JoinPoint joinPoint) throws Throwable {
System.out.println("Invoked: " + niceName(joinPoint));
}
@AfterReturning("controller() && methodPointcut() && requestMapping()")
public void afterControllerMethod(JoinPoint joinPoint) {
System.out.println("Finished: " + niceName(joinPoint));
}
private String niceName(JoinPoint joinPoint) {
return joinPoint.getTarget().getClass() + "#" + joinPoint.getSignature().getName() + "\n\targs:"
+ Arrays.toString(joinPoint.getArgs());
}
}
请帮助某人!!!
答案 0 :(得分:1)
更改您的@Pointcut
定义。对于类型而不是within(@....
使用@within(....
,对于方法而不是within(@....
使用@annotation(....
有关更多信息,请参阅spring referene有关切入点的信息