0

条件付きでアスペクト紹介を作成する方法はありますか? 私が欲しいのは、条件付きで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);
    }
}

テストアスペクト

//@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;*/
}
4

2 に答える 2

1

最後に解決策を見つけました。主な問題は、私のEclipseプロジェクトで、SpringツールのオプションメニューでSpring Aspectsツールを有効にし(プロジェクトを右クリック)、何らかの方法でSpring AOPの前に従来のAspectjでアスペクトをコンパイルしていたことです。それは、私がアスペクトに対して使用していた条件が常に適用された理由を説明しています。

したがって、解決策は、Spring Aspectj ツールを有効にしないことです。または有効になっている場合は、プロジェクト AspectJ Tools -> Remove AspectJ Capability で右クリックします。

于 2014-08-21T14:55:49.627 に答える
0

@Conditional注釈を使用するかPropertySourcesPlaceholderConfigurer、xml Bean 定義ファイルで使用できます。

たとえば、xml の場合

test.aspect = org.example.Test1Aspect

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

Spring AOP 用の maven アスペクトプラグインは必要ありません。

于 2014-08-20T21:26:03.833 に答える