Spring方面:指定要编织哪些包

Spring Aspects: specify which packages to weave

我有一个 Spring/Hibernate 应用程序。 Hibernate 创建的自定义类型需要 Spring 上下文,所以我使用 Spring 方面来提供它。

@Configurable(preConstruction = true)
public class EncryptedStringUserType implements EnhancedUserType {
...

@EnableSpringConfigured
@EnableLoadTimeWeaving
public class RootConfiguration {
...

添加 Spring 安全性后,我在 stderr 中得到了这样的消息数:

[AppClassLoader@14dad5dc] error can't determine implemented interfaces of missing type org.springframework.security.ldap.authentication.LdapAuthenticationProvider
when weaving type org.springframework.security.config.annotation.authentication.configurers.ldap.LdapAuthenticationProviderConfigurer
when weaving classes 
when weaving 
 [Xlint:cantFindType]

是否可以指定应该编织的 类 个包并避免尝试编织其他包?

解决方案

将META-INF/aop.xml放入资源根目录并排除不需要的包:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver>
        <exclude within="org.springframework.security.config.annotation.authentication.configurers.ldap.*"/>
        <exclude within="org.springframework.security.config.annotation.web.configurers.openid.*"/>
    </weaver>
</aspectj>

您可以通过将 <include within="your.package.here.*"/> 标记添加到类路径中的 META-INF/aop.xml 文件来限制编织范围。这是取自 Spring documentation:

META-INF/aop.xml 的完整示例
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>

    <weaver>
        <!-- only weave classes in our application-specific packages -->
        <include within="foo.*"/>
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="foo.ProfilingAspect"/>
    </aspects>

</aspectj>