【问题标题】:Spring AOP @DeclareParents conditionallySpring AOP @DeclareParents 有条件地
【发布时间】:2014-08-20 16:42:55
【问题描述】:

有条件创建一个方面介绍的方法吗?我想要的是有条件地使用 Spring AOP 扩展一个类:

@Aspect
public class Test1Aspect {
    @DeclareParents(value="com.test.testClass",defaultImpl=Test1Impl.class)
    public ITest iTest;
}

@Aspect
public class Test2Aspect {
    @DeclareParents(value="com.test.testClass",defaultImpl=Test2Impl.class)
    public ITest iTest;
}

所以 testClass 扩展 Test1Impl 或 Test2Impl 取决于我设置该选项的属性文件,它可能吗?我如何排除被调用的方面,我尝试使用 aspectj-maven-plugin 但它不排除我的方面:

pom.xml

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.5</version>
    <configuration>
        <sources>
            <source>
                <basedir>src/main/java</basedir>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </source>
        </sources>
    </configuration>
    <executions>
        <execution>
            <goals>
                <!-- use this goal to weave all your main classes -->
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

编辑

我删除了 aspectj-maven-plugin 并仅使用 Spring AOP,以下是配置和测试方面:

应用程序.java

@Configuration
@ComponentScan(basePackages= {
        "demo"
        //"demo.aspect"
})
@EnableAutoConfiguration(exclude=AopAutoConfiguration.class)
//@EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED)
@EnableAspectJAutoProxy
public class Application {

    public static final Logger LOGGER = LogManager.getLogger(Application.class);

    @Bean
    public testService testService() {
        return new testService();
    }

    @Bean
    @Conditional(TestCondition.class) //CLASS THAT ONLY RETURNS TRUE OR FALSE
    public TestAspect testAspect() {
        LOGGER.info("TEST ASPECT BEAN");
        TestAspect aspect = Aspects.aspectOf(TestAspect.class);
        return aspect;
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

TestAspect

//@Component
//@Profile("asdasd")
//@Configurable
//@Configuration
@Aspect
public class TestAspect{
    public static final Logger LOGGER = LogManager.getLogger(TestAspect.class);

    @Autowired
    private testService testService;

    public TestAspect() {
        LOGGER.info("TEST ASPECT INITIALIZED");
    }

    @Around("execution(* demo.testControllerEX.test(*))")
    public String prevent(ProceedingJoinPoint point) throws Throwable{
        LOGGER.info("ASPECT AROUND " + testService); // ALWAYS CALLED NO MATTER IF THE CONDITION IS FALSE, THE ONLY DIFFERENCE IS THAT testService IS NULL WHEN THE CONDITION IS FALSE.
        String result = (String)point.proceed();
        return result;
    }

    /*@DeclareParents(value="(demo.testControllerEX)",defaultImpl=TestControllersImpl.class)
    private ITestControllerEX itestControllerEX;*/
}

【问题讨论】:

    标签: java spring aspectj spring-aop aspectj-maven-plugin


    【解决方案1】:

    最后我找到了解决方案,主要问题是在我的 Eclipse 项目中,我在 Spring 工具的选项菜单中 Enable Spring Aspects tooling(右键单击项目)并且以某种方式正在编译我在 Spring AOP 之前使用传统 Aspectj 的切面,因此解释了为什么无论我在切面上使用哪个 Conditional 总是被应用。

    所以解决方案是不启用 Spring Aspectj Tools。或者如果启用,请右键单击项目 AspectJ Tools -> Remove AspectJ Capability。

    【讨论】:

      【解决方案2】:

      您可以在 xml bean 定义文件中使用 @Conditional 注释或使用 PropertySourcesPlaceholderConfigurer

      例如对于xml

      test.aspect = org.example.Test1Aspect
      
      <context:property-placeholder location="configuration.properties" /> 
      <bean id="testAspect" class="${test.aspect}" />
      

      Spring AOP 不需要 maven aspectj 插件。

      【讨论】:

      • 我忘了告诉我我只使用 java config,我项目中唯一的 xml 是我的 pom.xml,我可以使用 java config 来做到这一点吗?
      • 我使用@Conditional,条件被称为返回false,但方面被调用并且类被扩展,也许是一个Spring错误?我用@Component 注释配置我的方面。
      • @DiegoFernandoMurilloValenci 你还在用 aspectj 插件编织吗?
      • 不,我删除它并在我的配置类中使用@EnableAspectJAutoProxy,然后我创建我的方面的@Bean 并添加条件,条件让我们禁用方面 bean 声明(我把方面构造函数中的记录器用于测试是否被调用)但是当 bean 未初始化时,方面中的建议继续工作,我测试 Spring 是否编译方面自动装配服务并将其记录在建议中(如果条件为 false,则为 null ),就像我的项目无论如何编译使用或不使用 Spring AOP 的方面
      猜你喜欢
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多