2011-09-21 82 views
1

我正在尝试使用Spring 3和注解来获取Aspect。使用注释的Spring 3的AOP

@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(); 
} 

}

有了这个代码,我只能看到 “内部属性打电话......”但从方面没有。 请指导。

通过制造新的对象(组件)得到这个工作,并注入到JUnit测试类。

回答

0

你需要的,如果你想拦截的同一个bean的形式,然后调用内部方法的调用使用真正的AspectJ。 (你做了什么,应该如果方法testAttributeAspect()坐落在一个其他的bean工作。)


如何做到真正的AspectJ?

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

@see Spring Reference

0

有几件事情:

首先,当你周围的建议你需要编写的建议方法是这样的:

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

而且(此至少在你使用代理时适用,在你的情况下不能完全确定),你提供建议的方法需要公开(你的不是),弹簧托管(通过@Component或其他方式),并称为外部cl所以代理可以生效(在你的例子中也不是这种情况)。所以,你需要的东西是这样的:

@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(); 
    } 
} 
1

高兴地看到,你得到了它从XML的工作,但你可以也从注释做了。

问题是,@Aspect注释不是Spring的原型,所以扫描器没有注册为Spring Bean的方面。只需添加@Service@Component高于或低于@Aspect,它将被注册。按照标准Spring设计,还可以直接命名该bean(例如,@Service("myNamedService"))或使其实现接口(例如,public class AttributeAspect implements IAspect {)。