我正在编写一个方面来记录控制器中每个 API 调用的请求和响应。我希望能够在类上使用此注释,因此使用 @Target(ElementType.TYPE)
之前我添加了 @Target(ElementType.Method) 并且我在方法上使用了这个注释并且它工作正常。现在我想将其更改为 @Target(ElementType.TYPE)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReLogger {}
Run Code Online (Sandbox Code Playgroud)
@Aspect
@Component
public class ReLoggerAspect {
public static final Logger log = LoggerFactory.getLogger("ReLoggerAspect");
@PostConstruct
private void postConstruct() {
log.info("ReLoggerAspect Created");
}
@Around("@annotation(ReLogger)")
private Object reqLoggingAspect(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("Request {}",jointPoint.getArgs()[0);
}
}
Run Code Online (Sandbox Code Playgroud)
在类上使用 @ReLoggerAspect
@RestController
@RequestMapping(value = "....", produces = { "application/json" })
@ReLogger
public class Samplecontroller {
/** Some logic here**/.....
}
Run Code Online (Sandbox Code Playgroud)
调用 API SampleController 时,它不会打印请求
什么是使用的优点@Configurable相比于其上,不是由豆做迪托管bean通过applicationcontext.getbean?任何人列出赞成和缺点?
我正在使用编译时编织使用maven,spring和aspectj
我的aspectj顾问看起来像这样
@Aspect
public class LoggingInterceptor {
private LogManager logManager;
public void setLogManager(LogManager logManager) {
this.logManager = logManager;
}
.....
}
Run Code Online (Sandbox Code Playgroud)
我的applicationContext.xml看起来像这样
<!--configures the AspectJ aspect and indicates which Spring context should be used when giving advice-->
<context:spring-configured />
<aop:aspectj-autoproxy/>
<!--<context:component-scan base-package="com.reverb" />-->
<bean id="loggingInterceptor" class="com.myapp.interceptor.LoggingInterceptor">
<property name="logManager" ref="logManager" />
</bean>
Run Code Online (Sandbox Code Playgroud)
logManager始终为null ....
我有通用的方法Foo.foo():
class Foo {
static native T <T> foo();
}
Bar bar = Foo.foo();
Run Code Online (Sandbox Code Playgroud)
我需要的是使用AspectJ替换对此方法的调用.问题是要从方面返回类型T的值,我需要知道T是什么.我如何使用AspectJ做到这一点?
这是我尝试过的一个解决方案:
Object around() : call(* Foo.foo(..)) {
Class target = ((MethodSignature) thisJoinPoint.getSignature()).getReturnType();
System.out.println("class = " + class);
}
Run Code Online (Sandbox Code Playgroud)
它Object作为返回类型返回.如何确定该调用foo()应该实际返回实例Bar?
我正在尝试使用Spring @Secured注释和AspectJ自动代理来使用我的Spring MVC应用程序,但它似乎没有代理或识别我的@Secured注释.我有一个像这样的控制器:
@Controller
@RequestMapping("/")
public class ApplicationController {
private ApplicationFactory applicationFactory;
@Inject
public ApplicationController(ApplicationFactory applicationFactory) {
super();
this.applicationFactory = applicationFactory;
}
@Secured("ROLE_USER")
@ResponseBody
@RequestMapping(method = GET)
public Application getApplicationInfo() {
return applicationFactory.buildApplication(this);
}
}
Run Code Online (Sandbox Code Playgroud)
一个Spring安全XML看起来像这样:
码:
<security:global-method-security secured-annotations="enabled" mode="aspectj" proxy-target-class="true" />
<security:http auto-config="true" use-expressions="true">
<security:http-basic/>
</security:http>
Run Code Online (Sandbox Code Playgroud)
上面是由no-xml Spring @Configuration组件加载的,如下所示:
@Configuration
@ComponentScan(basePackages = {"com.example"})
@EnableWebMvc
@ImportResource("classpath:security.xml")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
}
Run Code Online (Sandbox Code Playgroud)
反过来使用Servlet 3.0 WebApplicationInitializer加载:
public class SpringMvcInitializer implements WebApplicationInitializer {
private final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
public …Run Code Online (Sandbox Code Playgroud) 我正在使用AspectJ,我已经定义了一个切入点来捕获几个类的构造函数方法的执行,如下所示:
pointcut newobject(): execution(class1.new(..) || class2.new(..) || class3.new(..));
Run Code Online (Sandbox Code Playgroud)
然后我有
after()returning():newobject(){
Run Code Online (Sandbox Code Playgroud)
在这里我想做不同的事情取决于刚创建的对象是class1,class2还是class3,如果有一种方法我可以在这一点上引用对象的类型而不必分割切入点?
有没有一种方法可以通过AspectJ编译来获取源文件?因此,ajc不会获取字节码,而是用作生成可以用javac编译的Java源的预处理器?
我写了一个方面,我试图用junit测试.该方面对第三方方法有一个@Around建议setQuery.在编译时,它抱怨:Can't find referenced pointcut setQuery
这是我的方面:
@Component
@Aspect
public class ElasticsearchQuerySecurityAspect {
@Around("org.elasticsearch.action.search.SearchRequestBuilder.setQuery() && args(queryBuilder)")
public void addFilter(final ProceedingJoinPoint pjp, QueryBuilder queryBuilder) throws Throwable {
Object[] args = pjp.getArgs();
// Set the filter to use our plugin
FilterBuilder securityFilter = FilterBuilders.scriptFilter("visibility-filter")
.lang("native")
.addParam("visibility-field", "visibility")
.addParam("parameter", "default");
// Re-create original query with the filter applied
QueryBuilder newQuery = QueryBuilders.filteredQuery(queryBuilder,securityFilter);
log.info("Adding filter to search request");
// Tell the method to run with the modified parameter
args[0] = newQuery;
pjp.proceed(args); …Run Code Online (Sandbox Code Playgroud) 我对如何在AOP中使用Feign客户感兴趣.例如:
API:
public interface LoanClient {
@RequestLine("GET /loans/{loanId}")
@MeteredRemoteCall("loans")
Loan getLoan(@Param("loanId") Long loanId);
}
Run Code Online (Sandbox Code Playgroud)
配置:
@Aspect
@Component // Spring Component annotation
public class MetricAspect {
@Around(value = "@annotation(annotation)", argNames = "joinPoint, annotation")
public Object meterRemoteCall(ProceedingJoinPoint joinPoint,
MeteredRemoteCall annotation) throws Throwable {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何"拦截"api方法调用.我哪里做错了?
更新:
我的Spring类注释:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MeteredRemoteCall {
String serviceName();
}
Run Code Online (Sandbox Code Playgroud) 我正在关注Custom Spring AOP Annotation的给定文档
以下是我在项目中编写的源代码,具有与本文中提到的类似的依赖关系.
注释定义:
package com.sell.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CommitServiceAdvice {}
Run Code Online (Sandbox Code Playgroud)
方面定义:
package com.sell.business.service.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.sell.aggregator.MessageFlowAggregator;
@Aspect
@Component
public class CommitServiceAspect {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Around(value="@annotation(CommitServiceAdvice)",argNames="mfa")
public MessageFlowAggregator advice(ProceedingJoinPoint joinPoint,MessageFlowAggregator mfa) throws Throwable {
log.debug(">>> matching advice on {}",joinPoint);
mfa= (MessageFlowAggregator) joinPoint.proceed();
log.debug("<<< returning advice on {}",joinPoint);
return mfa;
}
}
Run Code Online (Sandbox Code Playgroud)
联合点:
package …Run Code Online (Sandbox Code Playgroud) aspectj ×10
java ×7
spring-aop ×5
spring ×4
aop ×2
annotations ×1
feign ×1
pointcut ×1
preprocessor ×1
spring-boot ×1