我想根据从属性文件读取的值有条件地创建一个方面。 (它与分析有关,这就是为什么要选择性使用)。
我尝试创建一个实现 Condition 的类,因此使用 matches() 方法,该方法允许我执行必要的验证并将输出设置为相应的布尔值,从而启用或禁用它。
@Aspect
@Component
@Conditional(AuthFilterCondition.class)
public class AuthTokenFilter {
@Before("@annotation(org.company.annotations.ValidateAuthToken)")
public void doBefore(JoinPoint joinPoint) {
System.out.println("Loading auth token filter");
}
}
验证过滤条件
public class AuthFilterCondition implements Condition {
private static final String IS_AUTH_FILTER_FEATURE_ENABLED = "org.company.load.auth.filter";
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return (context.getEnvironment().getProperty(IS_AUTH_FILTER_FEATURE_ENABLED).equalsIgnoreCase("true"));
}
}
控制器
@Path("/v1")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public interface HealthService {
@GET
@Path("/check/health")
Response checkHealth();
}
服务实现
public class HealthServiceImpl implements HealthService {
@ValidateAuthToken
@Override
public Response checkHealth() {
return Response.ok().build();
}
}
服务器上下文.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan
base-package="org.company" />
<import resource="classpath:/META-INF/cxf/cxf.xml" />
<import resource="classpath:/META-INF/cxf/cxf-servlet.xml" />
<import
resource="classpath*:META-INF/rcp/soa-interceptor-context.xml" />
<bean id="healthService"
class="org.company.services.impl.HealthServiceImpl" />
<bean class="org.company.services.filter.AuthTokenFilter" lazy-init="true">
<jaxrs:server address="/services">
<jaxrs:serviceBeans>
<ref bean="healthService" />
</jaxrs:serviceBeans>
</jaxrs:server>
<aop:aspectj-autoproxy />
</beans>
我已经在组件扫描期间使用带有组件的方面进行检测,并且如果不匹配则跳过建议,但它仍在执行建议。如果我在这里做错了什么,请提出建议。
在这里,我在运行时传递一个 VM 参数,该参数具有以下值,这将使 AuthFilterCondition 工作。
-Dorg.company.load.auth.filter=true
像您一样切换到基于注释的 bean 定义可能是最简单的解决方案。但如果您有理由坚持使用 XML,您可以只使用 Spring 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<beans profile="auth-filter">
<bean class="org.company.service.filter.AuthTokenFilter" lazy-init="true"/>
</beans>
<beans>
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<context:component-scan base-package="org.company"/>
<bean id="helloWorldService" class="org.company.service.impl.HelloWordServiceImpl"/>
<jaxrs:server id="restContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="helloWorldService"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
</beans>
请注意
<beans profile="auth-filter">
。
然后,从方面删除
@Component
和 @Conditional
注释:
@Aspect
//@Component
//@Conditional(AuthFilterCondition.class)
public class AuthTokenFilter {
public static final String ACCESS_TOKEN_HEADER = "accessToken";
@Before("@annotation(org.company.service.annotation.ValidateAuthToken)")
public void doBefore(JoinPoint joinPoint) {
System.out.println("Reaching Aspect");
}
}
接下来,例如在 Windows 上启动 Tomcat 时,您可以执行以下操作来激活该方面:
xxx> set SPRING_PROFILES_ACTIVE=auth-filter
xxx> "%CATALINA_HOME%\bin\startup.bat"
要停用它,请永远不要设置环境变量或取消设置它(如果已定义):
xxx> set SPRING_PROFILES_ACTIVE=
xxx> "%CATALINA_HOME%\bin\startup.bat"