JavaSpringAOP:我可以忽略Xlint:InvalidaBSolutionPename错误吗?该错误是由我不知道的类的切入点引起的';我没用?
我有一个关于通用组件和一个(十几个)应用程序的问题。我的组件有许多注释的切入点,可以在我的应用程序的类和方法中使用。当所有注释都出现在类路径上时,一切正常。但并非在我所有的应用程序中,我都有这些依赖项。当然,快速修复方法是添加它们,但这给了我的应用程序很多代码,而我在该应用程序中不需要这些代码。我正在寻找一种方法来忽略如下所述的JavaSpringAOP:我可以忽略Xlint:InvalidaBSolutionPename错误吗?该错误是由我不知道的类的切入点引起的';我没用?,java,classpath,spring-aop,Java,Classpath,Spring Aop,我有一个关于通用组件和一个(十几个)应用程序的问题。我的组件有许多注释的切入点,可以在我的应用程序的类和方法中使用。当所有注释都出现在类路径上时,一切正常。但并非在我所有的应用程序中,我都有这些依赖项。当然,快速修复方法是添加它们,但这给了我的应用程序很多代码,而我在该应用程序中不需要这些代码。我正在寻找一种方法来忽略如下所述的Xlint:invalidabsolutionypename错误: 所以我有: 我有许多使用Soap/JMS连接的应用程序,所有应用程序都使用@Annotationor
Xlint:invalidabsolutionypename
错误:
所以我有:
- 我有许多使用Soap/JMS连接的应用程序,所有应用程序都使用@Annotation
进行注释李>org.springframework.ws.server.endpoint.Annotation.endpoint
- 我的通用组件(jar)中有切入点:
@Around(“内部(@org.springframework.ws.server.endpoint.annotation.endpoint*)”
- 所有具有Spring WS依赖项和我的通用组件的应用程序都没有问题
- 没有注释的应用程序无法启动,原因是
(很明显,请参见链接)java.lang.IllegalArgumentException:警告与此类型名称不匹配:org.springframework.ws.server.endpoint.annotation.endpoint[Xlint:invalidabsolutionpename]
@Aspect
public class PerformanceLoggingAspect {
private LogWriter logWriter;
@Inject
public PerformanceLoggingAspect(LogWriter logWriter) {
this.logWriter = logWriter;
}
@Around("within(@org.springframework.web.bind.annotation.RestController *)")
public Object withinARestController(ProceedingJoinPoint pjp) throws Throwable {
return proceedWithLogging(pjp, MetingType.REST);
}
@Around("within(@org.springframework.ws.server.endpoint.annotation.Endpoint *)")
public Object withinAnEndpoint(ProceedingJoinPoint pjp) throws Throwable {
return proceedWithLogging(pjp, MetingType.BERICHT);
}
@Around("within(@javax.inject.Named *)")
public Object withinAService(ProceedingJoinPoint pjp) throws Throwable {
return proceedWithLogging(pjp, MetingType.SERVICE);
}
private Object proceedWithLogging(ProceedingJoinPoint pjp, String metingType) throws Throwable {
(... Working code (performance logging) if the annotation is on the classpath...)
}
}
更新:我尝试创建一个@NeedsClass(“any.package.Class”)
,这是一个来自spring上下文的@Conditional
注释。condition类是一个ClasspathCondition
,它检查类加载器是否可以加载给定的类。但是错误发生在条件被评估之前,所以我担心这是一条死胡同。但如果你好奇的话:
我尝试的@NeedsClass
注释
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional(ClasspathCondition.class)
public @interface NeedsClass {
String[] value();
}
条件
实现。我在这里写过日志,但从来没有写过
public class ClasspathCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
try {
String[] classes = (String[]) metadata.getAnnotationAttributes(NeedsClass.class.getName()).get("classes");
for (String clazz : classes) {
ClassUtils.resolveClassName(clazz, context.getClassLoader());
}
return true;
} catch (Throwable t) { /* noOp() */}
return false;
}
}
目前,我有一个解决办法:
- 我使用以下方法创建了一个超类:
protected Object proceedWithLogging(ProceedingJoinPoint pjp, String metingType) throws Throwable { (... code which adds performance logging ...) }
- 我创建了4个子类,每个子类都带有
注释,还有1个方法调用super。例如,这一个目标是JMS:@Aspect
@Aspect public class JmsPerformanceLogger extends PerformanceLoggingAspect { @Inject private LogWriter logWriter; @Around("within(@org.springframework.ws.server.endpoint.annotation.Endpoint *)") public Object withinAnEndpoint(ProceedingJoinPoint pjp) throws Throwable { return proceedWithLogging(pjp, MetingType.BERICHT); } }
- 缺点是,我必须在我的应用程序中配置我需要的所有不同的bean,并且我无法添加一个简单的配置文件,如下所示,所有bean都已预配置:
因此也不是自动配置类的方便注释:@Configuration public class PerformanceloggingConfig { @Bean public LogWriter performanceLogWriter(){ return new DefaultLogWriter(); } @Bean public JmsPerformanceLogger jmsPerformanceLogger(){ return new JmsPerformanceLogger(); } @Bean public RestPerformanceLogger restPerformanceLogger(){ return new RestPerformanceLogger(); } @Bean public ServicesPerformanceLogger servicesPerformanceLogger(){ return new ServicesPerformanceLogger(); } @Bean public DaoPerformanceLogger daoPerformanceLogger(){ return new DaoPerformanceLogger(); } }
但就目前而言,在我需要的时候添加这4个bean,可以区分每个应用程序。当然,这仍然是一个解决方法,因为我想使用@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Import(PerformanceloggingConfig.class) public @interface EnablePerformanceLogging { }
并完成它。如果有人有更好的答案,请告诉我@EnablePerformanceLogging