2013-03-27 49 views
2

我看點類將是,使用AspectJ不能在春季工作的AOP?

@Configuration 
@EnableAspectJAutoProxy 
@Component 
@Aspect 
public class AspectClass { 

    @Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())") 
    public void logBefore(JoinPoint joinPoint) { 

     System.out.println("Before running the beforeAspect() in the AopTest.java class!"); 
     System.out.println("Hijacked Method name : " + joinPoint.getSignature().getName()); 
     System.out.println("************************"); 
    } 

} 

我的其他Java類

public class AopTest { 

    public void beforeAspect() { 
     System.out.println("This is beforeAspect() !"); 
    } 
} 

我的主類是

public class MainMethod { 

    public static void main(String[] args) {  
     ApplicationContext context = new FileSystemXmlApplicationContext("ApplicationContext/applicationContext.xml"); 
     AopTest test = (AopTest)context.getBean("bean1"); 
     test.beforeAspect(); 
    } 
} 

我applicationContext.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> 

    <bean id="bean1" class="com.pointel.aop.test1.AopTest" /> 

</beans> 

在此@Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())")中的AspectClass將不會在beforeAspect()之前執行AopTest,此時運行Main方法。

好的答案是肯定讚賞的。

+2

不要讓你的配置類與你的Aspect類相同。另外,你需要'@ ComponentScan'你的Aspect所在的包。 – 2013-03-27 14:57:57

+0

我刪除了'@ Configuration'幷包含'@ ComponentScan',但仍然無法工作。 – 2013-03-27 15:04:25

+0

請看下面,有幾件事情你錯過了。 – 2013-03-27 15:12:30

回答

3

首先,如果您打算使用基於註釋的配置,請使用AnnotationConfigApplicationContext而不是FileSystemXmlApplicationContext。擺脫applicationContext.xml文件,並簡單地在您的配置類中添加@Bean方法。事情是這樣的:

@Configuration 
@EnableAspectJAutoProxy 
@ComponentScan(basePackages = "your.aspect.package") 
public class AspectConfig { 
    @Bean 
    public AopTest aopTest() { 
     return new AopTest(); 
    } 
} 

在你的主

public class MainMethod { 

    public static void main(String[] args) {  
     AnnotationConfigApplicationContextcontext = new AnnotationConfigApplicationContext(AspectConfig.class); 
     // don't forget to refresh 
     context.refresh(); 
     AopTest test = (AopTest)context.getBean("aopTest"); 
     test.beforeAspect(); 
    } 
} 

AspectClass你應該有@Component@Aspect,你的方法應該有通知或切入點註解像@Before。它需要是@Component,所以Spring知道要掃描它。

+0

感謝您的回覆......我是否想要刪除'AspectClass'中的四個註釋? – 2013-03-27 15:25:40

+0

在'AspectClass'中你應該有'@ Component','@ Aspect',並且你的方法應該有'@ Before'之類的建議或切入點註釋。它需要是一個'@ Component',所以Spring知道要掃描它。更新我的回答 – 2013-03-27 15:26:48

+0

'context.refresh();'需要什麼?沒有''context.refresh();''的解決方案。 – 2013-03-27 15:28:52

1

這裏有些代碼需要在xml中添加才能使用批註-1.for @component註釋。

xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd" 

2.after使用組件掃描來獲得其使用@Component註釋所有註解bean類,並使用AOP autoproxy-

<context:annotation-config/> 
<context:component-scan base-package="mypackage"></context:component-scan> 
<aop:aspectj-autoproxy> 
</aop:aspectj-autoproxy> 

的例子visit- www.technicaltoday.com/p/spring.html

+0

感謝您的回答...我不需要用'xml'來完成它,並純粹尋找基於'AspectJ'的註解。 – 2013-03-28 06:30:26

0

你缺少您的方面課程中的切入點定義。

例如;

@Pointcut("execution(* *.advice(..))") 
public void logBefore(){} 

@Before("logBefore()") 
public void beforeAdvicing(){ 
System.out.println("Listen Up!!!!"); 
} 

您首先必須確定要編織您的方面。你可以通過使用Point cut來做到這一點。它是你在@Before註釋中給出的切點名稱。看看我的博客文章瞭解更多信息@http://dinukaroshan.blogspot.com/2010/06/aop-with-spring.html

0

我在beans配置中看不到您的AspectClass。你也應該聲明它是一個Bean。