繁体   English   中英

使用注释的Spring 3的AOP

[英]AOP with Spring 3 using Annotations

我正在尝试让Aspect使用Spring 3和注释。

@Aspect
public class AttributeAspect {

  @Pointcut("@annotation(com.mak.selective.annotation.Attribute)")
  public void process(){
    System.out.println("Inside Process ....");
  }

  @Around("process()")
  public void processAttribute(){
    System.out.println("Inside Actual Aspect ..");
  }
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans                 http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 <aop:aspectj-autoproxy proxy-target-class="false" />
<context:component-scan base-package="com.mak.selective.annotation.*" />
<bean name="attribute" class="com.mak.selective.annotation.AttributeAspect"/>
</beans>

我的测试来测试方面:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/springcontext/*.xml")
public class AttributeTest {

@Attribute(tableName = "firstTable", columnName = "New Column")
private void getAttribute() {
    System.out.println("Inside Attribute call...");
}

@Test
public void testAttributeAspect() {
    getAttribute();
}

}

使用此代码,我只能看到“内部属性调用...”,但是从Aspect中看不到任何东西。 请指导。

通过制作一个新的对象(组件)并注入到Junit测试类中来完成这项工作。

很高兴看到您可以通过XML使用它,但是也可以通过注释来完成它。

问题在于@Aspect注释不是Spring原型,因此扫描程序未将方面注册为Spring Bean。 只需在@Aspect上方或下方添加@Service@Component ,它将被注册。

同样,按照标准Spring设计,直接命名bean(例如, @Service("myNamedService") )或使其实现接口(例如, public class AttributeAspect implements IAspect { )。

如果要在调用它的同一bean形式中拦截方法的调用,则需要使用真实的AspectJ。 (如果方法testAttributeAspect()位于另一个bean中,则完成的工作应该可以。)


如何做真正的AspectJ?

使用AspectJ编译器和weaver可以使用完整的AspectJ语言,将在第7.8节“在Spring应用程序中使用AspectJ”中进行讨论。

@请参阅Spring参考

一些东西:

首先,当您处理建议时,您需要编写如下建议方法:

@Around(...)
public void aroundAdviceMethod(ProceedingJoinPoint pjp) throws Throwable {
    try {
        System.out.println("before...");
        pjp.proceed();
    }
    finally {
        System.out.println("After...");
    }
}

而且(至少在您使用代理时(这并不完全适用)),您提供建议的方法必须是公开的(您的不是),春季管理的(通过@Component或其他方式) ),并从该类外部调用,因此代理可以生效(在您的示例中也是如此)。 所以你需要这样的东西:

@Component
public class SomeClass {
    @Attribute
    public void someMethodCall() {
        System.out.println("In method call");
    }
}

public class SomeUnitTest {
    @Autowired SomeClass someClass;
    @Test 
    public void testAspect() {
        someClass.someMethodCall();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM